05 - 나무 8 파일 전송

#include 
#include 

int Find(int x, int s[]);
void Union(int c1, int c2, int s[]);

int main(int argc, char const *argv[])
{
	int *s;
	int N, c1, c2;
	char ch;
	scanf("%d", &N);
	s = (int*)malloc(sizeof(int)*N);
	for (int i = 0; i < N; ++i){
		s[i] = i;
	}
	while(1){
		scanf("%c", &ch);
		if(ch == 'S')
			break;
		scanf("%d %d", &c1, &c2);
		if(ch == 'C'){
			if(Find(c1, s) == Find(c2, s))
				printf("yes
"); else printf("no
"); } if(ch == 'I') Union(c1, c2, s); } int count = 0; for (int i = 0; i < N; ++i){ if(s[i] == i) count++; } if(count == 1) printf("The network is connected.
"); else printf("There are %d components.
", count); return 0; } int Find(int x, int s[]) { if(s[x] == x) return x; else s[x] = Find(s[x], s); } void Union(int c1, int c2, int s[]) { int root1, root2; root1 = Find(c1, s); root2 = Find(c2, s); if(root1 != root2) s[root2] = root1; }

좋은 웹페이지 즐겨찾기