관리 메뉴

꿈꾸는 개발자

2660번 [백준] 회장뽑기 c++ (다른 사람 코드 분석 필요!) 본문

백준(BOJ)

2660번 [백준] 회장뽑기 c++ (다른 사람 코드 분석 필요!)

rickysin 2022. 5. 18. 11:18
시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 128 MB 6509 3492 2735 54.094%

문제

월드컵 축구의 응원을 위한 모임에서 회장을 선출하려고 한다. 이 모임은 만들어진지 얼마 되지 않았기 때문에 회원 사이에 서로 모르는 사람도 있지만, 몇 사람을 통하면 모두가 서로 알 수 있다. 각 회원은 다른 회원들과 가까운 정도에 따라 점수를 받게 된다.

예를 들어 어느 회원이 다른 모든 회원과 친구이면, 이 회원의 점수는 1점이다. 어느 회원의 점수가 2점이면, 다른 모든 회원이 친구이거나 친구의 친구임을 말한다. 또한 어느 회원의 점수가 3점이면, 다른 모든 회원이 친구이거나, 친구의 친구이거나, 친구의 친구의 친구임을 말한다.

4점, 5점 등은 같은 방법으로 정해진다. 각 회원의 점수를 정할 때 주의할 점은 어떤 두 회원이 친구사이이면서 동시에 친구의 친구사이이면, 이 두사람은 친구사이라고 본다.

회장은 회원들 중에서 점수가 가장 작은 사람이 된다. 회장의 점수와 회장이 될 수 있는 모든 사람을 찾는 프로그램을 작성하시오.

입력

입력의 첫째 줄에는 회원의 수가 있다. 단, 회원의 수는 50명을 넘지 않는다. 둘째 줄 이후로는 한 줄에 두 개의 회원번호가 있는데, 이것은 두 회원이 서로 친구임을 나타낸다. 회원번호는 1부터 회원의 수만큼 붙어 있다. 마지막 줄에는 -1이 두 개 들어있다.

출력

첫째 줄에는 회장 후보의 점수와 후보의 수를 출력하고, 두 번째 줄에는 회장 후보를 오름차순으로 모두 출력한다.

예제 입력 1 복사

5
1 2
2 3
3 4
4 5
2 4
5 3
-1 -1

예제 출력 1 복사

2 3
2 3 4

풀이: BFS를 이용한 거리 계산 중 가장 큰 값을 점수로 지정하면 된다!

 

 

내가 작성한 코드:

#include<bits/stdc++.h>
using namespace std;
vector<int> adj[51];
vector<int> result;
int point[51],dist[51];
void bfs(int start) {
	queue<int> q;
	q.push(start);
	dist[start] = 0;
	while (!q.empty()) {
		int cur = q.front(); q.pop();
		for (auto& nxt : adj[cur]) {
			if (dist[nxt] >=0)continue;
			dist[nxt] = dist[cur] + 1;
			q.push(nxt);

		}
	}

}
int main(void) {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int v;
	cin >> v;
	int v1 = 1, u = 1;
	while (v1 + u > 0) {
		cin >> v1 >> u;
		if (v1 > 0 && u > 0) {
			adj[v1].push_back(u);
			adj[u].push_back(v1);
		}
	}
	int mins = 100;
	for (int i = 1; i <= v; i++) {
		fill(dist, dist + 51, -1);
		bfs(i);	
		//for (int i = 1; i <= v; i++)cout<<dist[i] << " ";
		//cout << "\n";
		sort(dist + 1, dist + v + 1, greater<>());
		point[i] = dist[1];
		mins = min(mins, point[i]);
	}

	//point에 가장 작은 애들부터 모임!
	//for (int i = 1; i <= v; i++)cout << point[i]<<" ";
	for (int i = 1; i <= v; i++)if(mins==point[i]) result.push_back(i);
	cout << mins << " " << result.size() << "\n";
	for (auto& c : result)cout << c << " ";
}

개선점: 굳이 dist를 내림차순으로 정렬해 가장 큰 값을 찾는 수고보단, 위에서 bfs를 돌면서 계산되는 값에서 즉시 max함수를 통해 최댓값을 찾는 것이 오히려 더 가독성 등 여러 장점이 있어 보인다. 


위 사항을 참고해 개선한 코드: 

#include<bits/stdc++.h>
using namespace std;
vector<int> adj[51];
vector<int> result;
int point[51],dist[51];
int bfs(int start) {
	queue<int> q;
	q.push(start);
	dist[start] = 0;
	int ret = 0;
	while (!q.empty()) {
		int cur = q.front(); q.pop();
		for (auto& nxt : adj[cur]) {
			if (dist[nxt] >=0)continue;
			dist[nxt] = dist[cur] + 1;
			ret = max(ret,dist[nxt]);
			q.push(nxt);

		}
	}
	return ret;
}
int main(void) {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int v;
	cin >> v;
	int v1 = 1, u = 1;
	while (v1 + u > 0) {
		cin >> v1 >> u;
		if (v1 > 0 && u > 0) {
			adj[v1].push_back(u);
			adj[u].push_back(v1);
		}
	}
	int mins = 100;
	for (int i = 1; i <= v; i++) {
		fill(dist, dist + 51, -1);
		point[i]=bfs(i);	
		mins = min(mins, point[i]);
	}
	//point에 가장 작은 애들부터 모임!
	for (int i = 1; i <= v; i++)if(mins==point[i]) result.push_back(i);
	cout << mins << " " << result.size() << "\n";
	for (auto& c : result)cout << c << " ";
}

다른 사람 코드: 추후에 꼭 분석해보기!

#include <stdio.h>

int main()
{
	int testCase,num1,num2,i,j,start;
	int head=0,tail=0,arr[51][51]={0,},v[51]={0,},temp[51]={0,},queue[51]={0,};
	
	scanf("%d",&testCase);
	
	while(1)
	{
		scanf("%d %d",&num1,&num2);
		if(num1 == -1 && num2 == -1) break;
		arr[num1][num2] = arr[num2][num1] = 1;
	}
	
	for(i=1; i<=testCase; i++)
	{
		queue[tail++] = i;
		v[i] = 1;
		while(head != tail)
		{
			start = queue[head++];
			for(j=1; j<=testCase; j++)
				if(arr[start][j] && !v[j])
				{
					queue[tail++] = j;
					v[j] = v[start] + 1;
				}
		}
		
		for(j=1; j<=testCase; j++)
			if(temp[i] < v[j]) temp[i] = v[j];
		
		for(j=0; j<51; j++)
		{
			queue[j] = 0;
			v[j] = 0;
		}
		head = tail = 0;
	}
	int min = 100,count = 0,result[51]={0,};
	for(j=1; j<=testCase; j++)
		if(min > temp[j]) 
			min = temp[j];
		
	for(j=1; j<=testCase; j++)
		if(min == temp[j]) result[count++] = j;
		
	printf("%d %d\n",min-1,count);
	for(i=0; i<count; i++)
		printf("%d ",result[i]);	
		
}