답안 #129917

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
129917 2019-07-13T14:27:39 Z doowey Mergers (JOI19_mergers) C++14
100 / 100
913 ms 82492 KB
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ld, ld> pdd;
 
#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int N = (int)5e5 + 9;

int pi[N];
int fin(int x){
    if(pi[x] == x)
        return x;
    return pi[x]=fin(pi[x]);
}

void unite(int a, int b){
    a=fin(a);
    b=fin(b);
    if(a==b)
        return;
    pi[a] = b;
}

vector<int> T[N];
int dep[N];
int par[N];

void dfs(int u, int pr){
    par[u] = pr;
    for(auto p : T[u]){
        if(p == pr) continue;
        dep[p] = dep[u] + 1;
        dfs(p, u);
    }
}

vector<int> col[N];

void path(int a, int b){
    a = fin(a);
    b = fin(b);
    while(a != b){
        if(dep[a] < dep[b]) swap(a, b);
        unite(a, par[a]);
        a = fin(a);
        b = fin(b);
    }
}

int deg[N];

int main(){
    fastIO;
    int n,k;
    cin >> n >> k;
    int u, v;
    for(int i = 2; i <= n; i ++ ){
        cin >> u >> v;
        T[u].push_back(v);
        T[v].push_back(u);
    }
    dfs(1, -1);
    int cl;
    for(int i = 1; i <= n; i ++ ){
        cin >> cl;
        col[cl].push_back(i);
    }
    for(int i = 1; i <= n; i ++ )
        pi[i] = i;
    for(int i = 1; i <= k ; i ++ ){
        for(auto p : col[i]){
            path(col[i][0], p);
        }
    }
    for(int i = 1; i <= n; i ++ )
        for(auto p : T[i])
            if(fin(i) != fin(p))
                deg[fin(p)] ++ ;
    int sz = 0;
    for(int i = 1; i <= n; i ++ )
        if(deg[i] == 1)
            sz ++ ;
    cout << (sz + 1) / 2;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 23928 KB Output is correct
2 Correct 25 ms 23932 KB Output is correct
3 Correct 25 ms 23800 KB Output is correct
4 Correct 25 ms 23800 KB Output is correct
5 Correct 25 ms 23800 KB Output is correct
6 Correct 26 ms 23928 KB Output is correct
7 Correct 25 ms 23800 KB Output is correct
8 Correct 26 ms 23928 KB Output is correct
9 Correct 26 ms 23928 KB Output is correct
10 Correct 25 ms 23900 KB Output is correct
11 Correct 25 ms 23800 KB Output is correct
12 Correct 26 ms 23928 KB Output is correct
13 Correct 25 ms 23800 KB Output is correct
14 Correct 25 ms 23928 KB Output is correct
15 Correct 25 ms 23928 KB Output is correct
16 Correct 25 ms 23928 KB Output is correct
17 Correct 25 ms 23804 KB Output is correct
18 Correct 26 ms 23928 KB Output is correct
19 Correct 25 ms 23800 KB Output is correct
20 Correct 26 ms 23800 KB Output is correct
21 Correct 25 ms 23852 KB Output is correct
22 Correct 32 ms 23928 KB Output is correct
23 Correct 25 ms 23800 KB Output is correct
24 Correct 26 ms 23800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 23928 KB Output is correct
2 Correct 25 ms 23932 KB Output is correct
3 Correct 25 ms 23800 KB Output is correct
4 Correct 25 ms 23800 KB Output is correct
5 Correct 25 ms 23800 KB Output is correct
6 Correct 26 ms 23928 KB Output is correct
7 Correct 25 ms 23800 KB Output is correct
8 Correct 26 ms 23928 KB Output is correct
9 Correct 26 ms 23928 KB Output is correct
10 Correct 25 ms 23900 KB Output is correct
11 Correct 25 ms 23800 KB Output is correct
12 Correct 26 ms 23928 KB Output is correct
13 Correct 25 ms 23800 KB Output is correct
14 Correct 25 ms 23928 KB Output is correct
15 Correct 25 ms 23928 KB Output is correct
16 Correct 25 ms 23928 KB Output is correct
17 Correct 25 ms 23804 KB Output is correct
18 Correct 26 ms 23928 KB Output is correct
19 Correct 25 ms 23800 KB Output is correct
20 Correct 26 ms 23800 KB Output is correct
21 Correct 25 ms 23852 KB Output is correct
22 Correct 32 ms 23928 KB Output is correct
23 Correct 25 ms 23800 KB Output is correct
24 Correct 26 ms 23800 KB Output is correct
25 Correct 31 ms 23928 KB Output is correct
26 Correct 27 ms 24056 KB Output is correct
27 Correct 27 ms 24056 KB Output is correct
28 Correct 28 ms 24184 KB Output is correct
29 Correct 28 ms 24184 KB Output is correct
30 Correct 27 ms 24056 KB Output is correct
31 Correct 26 ms 23928 KB Output is correct
32 Correct 27 ms 24184 KB Output is correct
33 Correct 25 ms 23928 KB Output is correct
34 Correct 26 ms 24056 KB Output is correct
35 Correct 27 ms 24056 KB Output is correct
36 Correct 27 ms 24056 KB Output is correct
37 Correct 28 ms 24056 KB Output is correct
38 Correct 26 ms 23928 KB Output is correct
39 Correct 27 ms 24056 KB Output is correct
40 Correct 33 ms 24060 KB Output is correct
41 Correct 27 ms 24056 KB Output is correct
42 Correct 27 ms 24056 KB Output is correct
43 Correct 27 ms 24184 KB Output is correct
44 Correct 25 ms 23800 KB Output is correct
45 Correct 28 ms 24184 KB Output is correct
46 Correct 33 ms 24056 KB Output is correct
47 Correct 25 ms 23800 KB Output is correct
48 Correct 27 ms 23988 KB Output is correct
49 Correct 33 ms 24184 KB Output is correct
50 Correct 28 ms 24184 KB Output is correct
51 Correct 28 ms 24056 KB Output is correct
52 Correct 27 ms 24056 KB Output is correct
53 Correct 32 ms 24056 KB Output is correct
54 Correct 28 ms 24056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 23928 KB Output is correct
2 Correct 25 ms 23932 KB Output is correct
3 Correct 25 ms 23800 KB Output is correct
4 Correct 25 ms 23800 KB Output is correct
5 Correct 25 ms 23800 KB Output is correct
6 Correct 26 ms 23928 KB Output is correct
7 Correct 25 ms 23800 KB Output is correct
8 Correct 26 ms 23928 KB Output is correct
9 Correct 26 ms 23928 KB Output is correct
10 Correct 25 ms 23900 KB Output is correct
11 Correct 25 ms 23800 KB Output is correct
12 Correct 26 ms 23928 KB Output is correct
13 Correct 25 ms 23800 KB Output is correct
14 Correct 25 ms 23928 KB Output is correct
15 Correct 25 ms 23928 KB Output is correct
16 Correct 25 ms 23928 KB Output is correct
17 Correct 25 ms 23804 KB Output is correct
18 Correct 26 ms 23928 KB Output is correct
19 Correct 25 ms 23800 KB Output is correct
20 Correct 26 ms 23800 KB Output is correct
21 Correct 25 ms 23852 KB Output is correct
22 Correct 32 ms 23928 KB Output is correct
23 Correct 25 ms 23800 KB Output is correct
24 Correct 26 ms 23800 KB Output is correct
25 Correct 26 ms 23928 KB Output is correct
26 Correct 87 ms 30572 KB Output is correct
27 Correct 103 ms 30200 KB Output is correct
28 Correct 28 ms 24056 KB Output is correct
29 Correct 25 ms 23800 KB Output is correct
30 Correct 26 ms 23800 KB Output is correct
31 Correct 108 ms 30200 KB Output is correct
32 Correct 28 ms 24028 KB Output is correct
33 Correct 113 ms 33016 KB Output is correct
34 Correct 111 ms 30384 KB Output is correct
35 Correct 27 ms 24056 KB Output is correct
36 Correct 114 ms 30456 KB Output is correct
37 Correct 27 ms 24056 KB Output is correct
38 Correct 28 ms 24056 KB Output is correct
39 Correct 89 ms 30444 KB Output is correct
40 Correct 27 ms 24056 KB Output is correct
41 Correct 99 ms 30188 KB Output is correct
42 Correct 120 ms 31104 KB Output is correct
43 Correct 26 ms 23928 KB Output is correct
44 Correct 123 ms 33052 KB Output is correct
45 Correct 107 ms 31608 KB Output is correct
46 Correct 27 ms 24056 KB Output is correct
47 Correct 27 ms 24056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 89 ms 30444 KB Output is correct
2 Correct 108 ms 33744 KB Output is correct
3 Correct 32 ms 24060 KB Output is correct
4 Correct 27 ms 24184 KB Output is correct
5 Correct 26 ms 23928 KB Output is correct
6 Correct 25 ms 23928 KB Output is correct
7 Correct 26 ms 24056 KB Output is correct
8 Correct 122 ms 31864 KB Output is correct
9 Correct 28 ms 24056 KB Output is correct
10 Correct 112 ms 30712 KB Output is correct
11 Correct 26 ms 23928 KB Output is correct
12 Correct 119 ms 30584 KB Output is correct
13 Correct 126 ms 31860 KB Output is correct
14 Correct 127 ms 33548 KB Output is correct
15 Correct 89 ms 30456 KB Output is correct
16 Correct 33 ms 24132 KB Output is correct
17 Correct 25 ms 23800 KB Output is correct
18 Correct 108 ms 33516 KB Output is correct
19 Correct 138 ms 35072 KB Output is correct
20 Correct 28 ms 24056 KB Output is correct
21 Correct 25 ms 23804 KB Output is correct
22 Correct 108 ms 32112 KB Output is correct
23 Correct 33 ms 24056 KB Output is correct
24 Correct 139 ms 31060 KB Output is correct
25 Correct 146 ms 34604 KB Output is correct
26 Correct 27 ms 24184 KB Output is correct
27 Correct 27 ms 24184 KB Output is correct
28 Correct 27 ms 24056 KB Output is correct
29 Correct 27 ms 24056 KB Output is correct
30 Correct 27 ms 24060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 23928 KB Output is correct
2 Correct 25 ms 23932 KB Output is correct
3 Correct 25 ms 23800 KB Output is correct
4 Correct 25 ms 23800 KB Output is correct
5 Correct 25 ms 23800 KB Output is correct
6 Correct 26 ms 23928 KB Output is correct
7 Correct 25 ms 23800 KB Output is correct
8 Correct 26 ms 23928 KB Output is correct
9 Correct 26 ms 23928 KB Output is correct
10 Correct 25 ms 23900 KB Output is correct
11 Correct 25 ms 23800 KB Output is correct
12 Correct 26 ms 23928 KB Output is correct
13 Correct 25 ms 23800 KB Output is correct
14 Correct 25 ms 23928 KB Output is correct
15 Correct 25 ms 23928 KB Output is correct
16 Correct 25 ms 23928 KB Output is correct
17 Correct 25 ms 23804 KB Output is correct
18 Correct 26 ms 23928 KB Output is correct
19 Correct 25 ms 23800 KB Output is correct
20 Correct 26 ms 23800 KB Output is correct
21 Correct 25 ms 23852 KB Output is correct
22 Correct 32 ms 23928 KB Output is correct
23 Correct 25 ms 23800 KB Output is correct
24 Correct 26 ms 23800 KB Output is correct
25 Correct 31 ms 23928 KB Output is correct
26 Correct 27 ms 24056 KB Output is correct
27 Correct 27 ms 24056 KB Output is correct
28 Correct 28 ms 24184 KB Output is correct
29 Correct 28 ms 24184 KB Output is correct
30 Correct 27 ms 24056 KB Output is correct
31 Correct 26 ms 23928 KB Output is correct
32 Correct 27 ms 24184 KB Output is correct
33 Correct 25 ms 23928 KB Output is correct
34 Correct 26 ms 24056 KB Output is correct
35 Correct 27 ms 24056 KB Output is correct
36 Correct 27 ms 24056 KB Output is correct
37 Correct 28 ms 24056 KB Output is correct
38 Correct 26 ms 23928 KB Output is correct
39 Correct 27 ms 24056 KB Output is correct
40 Correct 33 ms 24060 KB Output is correct
41 Correct 27 ms 24056 KB Output is correct
42 Correct 27 ms 24056 KB Output is correct
43 Correct 27 ms 24184 KB Output is correct
44 Correct 25 ms 23800 KB Output is correct
45 Correct 28 ms 24184 KB Output is correct
46 Correct 33 ms 24056 KB Output is correct
47 Correct 25 ms 23800 KB Output is correct
48 Correct 27 ms 23988 KB Output is correct
49 Correct 33 ms 24184 KB Output is correct
50 Correct 28 ms 24184 KB Output is correct
51 Correct 28 ms 24056 KB Output is correct
52 Correct 27 ms 24056 KB Output is correct
53 Correct 32 ms 24056 KB Output is correct
54 Correct 28 ms 24056 KB Output is correct
55 Correct 26 ms 23928 KB Output is correct
56 Correct 87 ms 30572 KB Output is correct
57 Correct 103 ms 30200 KB Output is correct
58 Correct 28 ms 24056 KB Output is correct
59 Correct 25 ms 23800 KB Output is correct
60 Correct 26 ms 23800 KB Output is correct
61 Correct 108 ms 30200 KB Output is correct
62 Correct 28 ms 24028 KB Output is correct
63 Correct 113 ms 33016 KB Output is correct
64 Correct 111 ms 30384 KB Output is correct
65 Correct 27 ms 24056 KB Output is correct
66 Correct 114 ms 30456 KB Output is correct
67 Correct 27 ms 24056 KB Output is correct
68 Correct 28 ms 24056 KB Output is correct
69 Correct 89 ms 30444 KB Output is correct
70 Correct 27 ms 24056 KB Output is correct
71 Correct 99 ms 30188 KB Output is correct
72 Correct 120 ms 31104 KB Output is correct
73 Correct 26 ms 23928 KB Output is correct
74 Correct 123 ms 33052 KB Output is correct
75 Correct 107 ms 31608 KB Output is correct
76 Correct 27 ms 24056 KB Output is correct
77 Correct 27 ms 24056 KB Output is correct
78 Correct 89 ms 30444 KB Output is correct
79 Correct 108 ms 33744 KB Output is correct
80 Correct 32 ms 24060 KB Output is correct
81 Correct 27 ms 24184 KB Output is correct
82 Correct 26 ms 23928 KB Output is correct
83 Correct 25 ms 23928 KB Output is correct
84 Correct 26 ms 24056 KB Output is correct
85 Correct 122 ms 31864 KB Output is correct
86 Correct 28 ms 24056 KB Output is correct
87 Correct 112 ms 30712 KB Output is correct
88 Correct 26 ms 23928 KB Output is correct
89 Correct 119 ms 30584 KB Output is correct
90 Correct 126 ms 31860 KB Output is correct
91 Correct 127 ms 33548 KB Output is correct
92 Correct 89 ms 30456 KB Output is correct
93 Correct 33 ms 24132 KB Output is correct
94 Correct 25 ms 23800 KB Output is correct
95 Correct 108 ms 33516 KB Output is correct
96 Correct 138 ms 35072 KB Output is correct
97 Correct 28 ms 24056 KB Output is correct
98 Correct 25 ms 23804 KB Output is correct
99 Correct 108 ms 32112 KB Output is correct
100 Correct 33 ms 24056 KB Output is correct
101 Correct 139 ms 31060 KB Output is correct
102 Correct 146 ms 34604 KB Output is correct
103 Correct 27 ms 24184 KB Output is correct
104 Correct 27 ms 24184 KB Output is correct
105 Correct 27 ms 24056 KB Output is correct
106 Correct 27 ms 24056 KB Output is correct
107 Correct 27 ms 24060 KB Output is correct
108 Correct 586 ms 65564 KB Output is correct
109 Correct 701 ms 62640 KB Output is correct
110 Correct 805 ms 66916 KB Output is correct
111 Correct 913 ms 82492 KB Output is correct
112 Correct 836 ms 78456 KB Output is correct
113 Correct 606 ms 73476 KB Output is correct
114 Correct 600 ms 55868 KB Output is correct
115 Correct 611 ms 55652 KB Output is correct
116 Correct 762 ms 62116 KB Output is correct
117 Correct 838 ms 73652 KB Output is correct
118 Correct 661 ms 58440 KB Output is correct
119 Correct 809 ms 73548 KB Output is correct
120 Correct 852 ms 78840 KB Output is correct
121 Correct 857 ms 73464 KB Output is correct
122 Correct 834 ms 67192 KB Output is correct
123 Correct 612 ms 74908 KB Output is correct
124 Correct 853 ms 72180 KB Output is correct