이름이 문자열이고 최대 20글자까지라 배열로 표현하기에는 답도없어보여서 map을 사용했다.


<코드>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdio.h>
#include <string>
#include <map>
using namespace std;
 
int v[200001], gc[200001];
int c;
char a[22], b[22];
 
int f(int x) {
    return x == v[x] ? x : v[x] = f(v[x]);
}
 
int u(int x, int y) {
    x = f(x);
    y = f(y);
    if (x != y) {
        v[y] = x;
        gc[x] += gc[y];
        gc[y] = 1;
    }
    return gc[x];
}
 
int main() {
    int tc;
    scanf("%d"&tc);
    while (tc--) {
        int F, cnt = 0;
        map<stringint> m;
        scanf("%d"&F);
        for (int i = 0; i < 2*F; i++) v[i] = i, gc[i] = 1;
        while (F--) {
            scanf(" %s %s"&a, &b);
            if (!m.count(a)) m[a]=cnt++;
            if (!m.count(b)) m[b]=cnt++;
            printf("%d\n", u(m[a], m[b]));
        }
    }
    return 0;
}
cs


'BOJ' 카테고리의 다른 글

10775 공항  (0) 2017.09.06
1976 여행 가자  (0) 2017.09.06
1717 집합의 표현  (0) 2017.09.06
1649 택시  (1) 2017.09.06
10319 좀비 아포칼립스  (0) 2017.09.05

+ Recent posts