답안 #207632

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
207632 2020-03-08T08:41:01 Z nvmdava Mergers (JOI19_mergers) C++17
100 / 100
1202 ms 149852 KB
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define ll long long
#define ff first
#define ss second
#define N 500005
#define MOD 1000000007
#define INF 0x3f3f3f3f

int col[N];
int cnt[N];
int dsu[N];
vector<pair<int, int> > e;
vector<int> adj[N];

int find(int x){
    return x == dsu[x] ? x : dsu[x] = find(dsu[x]);
}

void merge(int v, int u){
    v = find(v);
    u = find(u);
    dsu[v] = u;
}

map<int, int> in[N];

void dfs(int v, int p){
    if(cnt[col[v]] != 1)
        in[v][col[v]] = 1;
    for(auto& x : adj[v]){
        if(x == p) continue;
        dfs(x, v);
        if(in[x].size() > in[v].size())
            swap(in[x], in[v]);
    }
    for(auto& x : adj[v]){
        if(x == p) continue;
        for(auto& y : in[x])
            if( (in[v][y.ff] += y.ss) == cnt[y.ff])
                in[v].erase(y.ff);
    }
    if(!in[v].empty())
        merge(col[v], col[p]);
}

int deg[N];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, k;
    cin>>n>>k;
    for(int i = 1; i <= k; ++i)
        dsu[i] = i;
    for(int a, b, i = 1; i < n; ++i){
        cin>>a>>b;
        e.push_back({a, b});
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    for(int i = 1; i <= n; ++i){
        cin>>col[i];
        ++cnt[col[i]];
    }

    dfs(1, 0);
    for(int i = 1; i <= n; ++i)
        col[i] = find(col[i]);
    
    for(auto& x : e){
        x.ff = col[x.ff];
        x.ss = col[x.ss];
        if(x.ff != x.ss){
            ++deg[x.ff];
            ++deg[x.ss];
        }
    }
    int ans = 0;
    for(int i = 1; i <= k; ++i)
        if(deg[i] == 1)
            ++ans;
    cout<<(ans + 1) / 2;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 35576 KB Output is correct
2 Correct 26 ms 35576 KB Output is correct
3 Correct 27 ms 35576 KB Output is correct
4 Correct 27 ms 35576 KB Output is correct
5 Correct 26 ms 35576 KB Output is correct
6 Correct 27 ms 35576 KB Output is correct
7 Correct 26 ms 35580 KB Output is correct
8 Correct 27 ms 35576 KB Output is correct
9 Correct 26 ms 35576 KB Output is correct
10 Correct 26 ms 35576 KB Output is correct
11 Correct 29 ms 35576 KB Output is correct
12 Correct 27 ms 35448 KB Output is correct
13 Correct 26 ms 35576 KB Output is correct
14 Correct 26 ms 35576 KB Output is correct
15 Correct 27 ms 35704 KB Output is correct
16 Correct 27 ms 35576 KB Output is correct
17 Correct 26 ms 35576 KB Output is correct
18 Correct 27 ms 35576 KB Output is correct
19 Correct 27 ms 35576 KB Output is correct
20 Correct 27 ms 35576 KB Output is correct
21 Correct 27 ms 35580 KB Output is correct
22 Correct 27 ms 35576 KB Output is correct
23 Correct 27 ms 35704 KB Output is correct
24 Correct 27 ms 35576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 35576 KB Output is correct
2 Correct 26 ms 35576 KB Output is correct
3 Correct 27 ms 35576 KB Output is correct
4 Correct 27 ms 35576 KB Output is correct
5 Correct 26 ms 35576 KB Output is correct
6 Correct 27 ms 35576 KB Output is correct
7 Correct 26 ms 35580 KB Output is correct
8 Correct 27 ms 35576 KB Output is correct
9 Correct 26 ms 35576 KB Output is correct
10 Correct 26 ms 35576 KB Output is correct
11 Correct 29 ms 35576 KB Output is correct
12 Correct 27 ms 35448 KB Output is correct
13 Correct 26 ms 35576 KB Output is correct
14 Correct 26 ms 35576 KB Output is correct
15 Correct 27 ms 35704 KB Output is correct
16 Correct 27 ms 35576 KB Output is correct
17 Correct 26 ms 35576 KB Output is correct
18 Correct 27 ms 35576 KB Output is correct
19 Correct 27 ms 35576 KB Output is correct
20 Correct 27 ms 35576 KB Output is correct
21 Correct 27 ms 35580 KB Output is correct
22 Correct 27 ms 35576 KB Output is correct
23 Correct 27 ms 35704 KB Output is correct
24 Correct 27 ms 35576 KB Output is correct
25 Correct 26 ms 35576 KB Output is correct
26 Correct 28 ms 35832 KB Output is correct
27 Correct 29 ms 35960 KB Output is correct
28 Correct 28 ms 35964 KB Output is correct
29 Correct 28 ms 35832 KB Output is correct
30 Correct 30 ms 36092 KB Output is correct
31 Correct 27 ms 35576 KB Output is correct
32 Correct 29 ms 36088 KB Output is correct
33 Correct 27 ms 35576 KB Output is correct
34 Correct 30 ms 35960 KB Output is correct
35 Correct 28 ms 35832 KB Output is correct
36 Correct 27 ms 35832 KB Output is correct
37 Correct 33 ms 35960 KB Output is correct
38 Correct 28 ms 35576 KB Output is correct
39 Correct 30 ms 36216 KB Output is correct
40 Correct 29 ms 35960 KB Output is correct
41 Correct 28 ms 35832 KB Output is correct
42 Correct 29 ms 35960 KB Output is correct
43 Correct 34 ms 36284 KB Output is correct
44 Correct 25 ms 35580 KB Output is correct
45 Correct 26 ms 35832 KB Output is correct
46 Correct 28 ms 35960 KB Output is correct
47 Correct 27 ms 35576 KB Output is correct
48 Correct 29 ms 35960 KB Output is correct
49 Correct 31 ms 35832 KB Output is correct
50 Correct 29 ms 35832 KB Output is correct
51 Correct 29 ms 35960 KB Output is correct
52 Correct 29 ms 35964 KB Output is correct
53 Correct 28 ms 35960 KB Output is correct
54 Correct 30 ms 36088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 35576 KB Output is correct
2 Correct 26 ms 35576 KB Output is correct
3 Correct 27 ms 35576 KB Output is correct
4 Correct 27 ms 35576 KB Output is correct
5 Correct 26 ms 35576 KB Output is correct
6 Correct 27 ms 35576 KB Output is correct
7 Correct 26 ms 35580 KB Output is correct
8 Correct 27 ms 35576 KB Output is correct
9 Correct 26 ms 35576 KB Output is correct
10 Correct 26 ms 35576 KB Output is correct
11 Correct 29 ms 35576 KB Output is correct
12 Correct 27 ms 35448 KB Output is correct
13 Correct 26 ms 35576 KB Output is correct
14 Correct 26 ms 35576 KB Output is correct
15 Correct 27 ms 35704 KB Output is correct
16 Correct 27 ms 35576 KB Output is correct
17 Correct 26 ms 35576 KB Output is correct
18 Correct 27 ms 35576 KB Output is correct
19 Correct 27 ms 35576 KB Output is correct
20 Correct 27 ms 35576 KB Output is correct
21 Correct 27 ms 35580 KB Output is correct
22 Correct 27 ms 35576 KB Output is correct
23 Correct 27 ms 35704 KB Output is correct
24 Correct 27 ms 35576 KB Output is correct
25 Correct 27 ms 35704 KB Output is correct
26 Correct 110 ms 46440 KB Output is correct
27 Correct 154 ms 51688 KB Output is correct
28 Correct 28 ms 35960 KB Output is correct
29 Correct 26 ms 35576 KB Output is correct
30 Correct 27 ms 35576 KB Output is correct
31 Correct 135 ms 46188 KB Output is correct
32 Correct 29 ms 35960 KB Output is correct
33 Correct 148 ms 57704 KB Output is correct
34 Correct 152 ms 50668 KB Output is correct
35 Correct 28 ms 35960 KB Output is correct
36 Correct 129 ms 47340 KB Output is correct
37 Correct 28 ms 35832 KB Output is correct
38 Correct 28 ms 35832 KB Output is correct
39 Correct 111 ms 46444 KB Output is correct
40 Correct 27 ms 36216 KB Output is correct
41 Correct 120 ms 46108 KB Output is correct
42 Correct 150 ms 49904 KB Output is correct
43 Correct 27 ms 35576 KB Output is correct
44 Correct 153 ms 58216 KB Output is correct
45 Correct 154 ms 52076 KB Output is correct
46 Correct 27 ms 35960 KB Output is correct
47 Correct 29 ms 36088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 104 ms 45036 KB Output is correct
2 Correct 90 ms 41580 KB Output is correct
3 Correct 29 ms 35828 KB Output is correct
4 Correct 29 ms 35960 KB Output is correct
5 Correct 27 ms 35576 KB Output is correct
6 Correct 27 ms 35576 KB Output is correct
7 Correct 28 ms 35960 KB Output is correct
8 Correct 147 ms 48616 KB Output is correct
9 Correct 29 ms 35960 KB Output is correct
10 Correct 127 ms 46188 KB Output is correct
11 Correct 27 ms 35576 KB Output is correct
12 Correct 129 ms 46216 KB Output is correct
13 Correct 132 ms 45800 KB Output is correct
14 Correct 106 ms 42860 KB Output is correct
15 Correct 115 ms 46444 KB Output is correct
16 Correct 29 ms 35960 KB Output is correct
17 Correct 27 ms 35576 KB Output is correct
18 Correct 104 ms 44268 KB Output is correct
19 Correct 115 ms 49388 KB Output is correct
20 Correct 29 ms 35956 KB Output is correct
21 Correct 26 ms 35576 KB Output is correct
22 Correct 143 ms 47212 KB Output is correct
23 Correct 32 ms 35960 KB Output is correct
24 Correct 124 ms 45928 KB Output is correct
25 Correct 114 ms 47084 KB Output is correct
26 Correct 28 ms 35832 KB Output is correct
27 Correct 27 ms 35832 KB Output is correct
28 Correct 30 ms 35960 KB Output is correct
29 Correct 29 ms 35960 KB Output is correct
30 Correct 29 ms 35964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 35576 KB Output is correct
2 Correct 26 ms 35576 KB Output is correct
3 Correct 27 ms 35576 KB Output is correct
4 Correct 27 ms 35576 KB Output is correct
5 Correct 26 ms 35576 KB Output is correct
6 Correct 27 ms 35576 KB Output is correct
7 Correct 26 ms 35580 KB Output is correct
8 Correct 27 ms 35576 KB Output is correct
9 Correct 26 ms 35576 KB Output is correct
10 Correct 26 ms 35576 KB Output is correct
11 Correct 29 ms 35576 KB Output is correct
12 Correct 27 ms 35448 KB Output is correct
13 Correct 26 ms 35576 KB Output is correct
14 Correct 26 ms 35576 KB Output is correct
15 Correct 27 ms 35704 KB Output is correct
16 Correct 27 ms 35576 KB Output is correct
17 Correct 26 ms 35576 KB Output is correct
18 Correct 27 ms 35576 KB Output is correct
19 Correct 27 ms 35576 KB Output is correct
20 Correct 27 ms 35576 KB Output is correct
21 Correct 27 ms 35580 KB Output is correct
22 Correct 27 ms 35576 KB Output is correct
23 Correct 27 ms 35704 KB Output is correct
24 Correct 27 ms 35576 KB Output is correct
25 Correct 26 ms 35576 KB Output is correct
26 Correct 28 ms 35832 KB Output is correct
27 Correct 29 ms 35960 KB Output is correct
28 Correct 28 ms 35964 KB Output is correct
29 Correct 28 ms 35832 KB Output is correct
30 Correct 30 ms 36092 KB Output is correct
31 Correct 27 ms 35576 KB Output is correct
32 Correct 29 ms 36088 KB Output is correct
33 Correct 27 ms 35576 KB Output is correct
34 Correct 30 ms 35960 KB Output is correct
35 Correct 28 ms 35832 KB Output is correct
36 Correct 27 ms 35832 KB Output is correct
37 Correct 33 ms 35960 KB Output is correct
38 Correct 28 ms 35576 KB Output is correct
39 Correct 30 ms 36216 KB Output is correct
40 Correct 29 ms 35960 KB Output is correct
41 Correct 28 ms 35832 KB Output is correct
42 Correct 29 ms 35960 KB Output is correct
43 Correct 34 ms 36284 KB Output is correct
44 Correct 25 ms 35580 KB Output is correct
45 Correct 26 ms 35832 KB Output is correct
46 Correct 28 ms 35960 KB Output is correct
47 Correct 27 ms 35576 KB Output is correct
48 Correct 29 ms 35960 KB Output is correct
49 Correct 31 ms 35832 KB Output is correct
50 Correct 29 ms 35832 KB Output is correct
51 Correct 29 ms 35960 KB Output is correct
52 Correct 29 ms 35964 KB Output is correct
53 Correct 28 ms 35960 KB Output is correct
54 Correct 30 ms 36088 KB Output is correct
55 Correct 27 ms 35704 KB Output is correct
56 Correct 110 ms 46440 KB Output is correct
57 Correct 154 ms 51688 KB Output is correct
58 Correct 28 ms 35960 KB Output is correct
59 Correct 26 ms 35576 KB Output is correct
60 Correct 27 ms 35576 KB Output is correct
61 Correct 135 ms 46188 KB Output is correct
62 Correct 29 ms 35960 KB Output is correct
63 Correct 148 ms 57704 KB Output is correct
64 Correct 152 ms 50668 KB Output is correct
65 Correct 28 ms 35960 KB Output is correct
66 Correct 129 ms 47340 KB Output is correct
67 Correct 28 ms 35832 KB Output is correct
68 Correct 28 ms 35832 KB Output is correct
69 Correct 111 ms 46444 KB Output is correct
70 Correct 27 ms 36216 KB Output is correct
71 Correct 120 ms 46108 KB Output is correct
72 Correct 150 ms 49904 KB Output is correct
73 Correct 27 ms 35576 KB Output is correct
74 Correct 153 ms 58216 KB Output is correct
75 Correct 154 ms 52076 KB Output is correct
76 Correct 27 ms 35960 KB Output is correct
77 Correct 29 ms 36088 KB Output is correct
78 Correct 104 ms 45036 KB Output is correct
79 Correct 90 ms 41580 KB Output is correct
80 Correct 29 ms 35828 KB Output is correct
81 Correct 29 ms 35960 KB Output is correct
82 Correct 27 ms 35576 KB Output is correct
83 Correct 27 ms 35576 KB Output is correct
84 Correct 28 ms 35960 KB Output is correct
85 Correct 147 ms 48616 KB Output is correct
86 Correct 29 ms 35960 KB Output is correct
87 Correct 127 ms 46188 KB Output is correct
88 Correct 27 ms 35576 KB Output is correct
89 Correct 129 ms 46216 KB Output is correct
90 Correct 132 ms 45800 KB Output is correct
91 Correct 106 ms 42860 KB Output is correct
92 Correct 115 ms 46444 KB Output is correct
93 Correct 29 ms 35960 KB Output is correct
94 Correct 27 ms 35576 KB Output is correct
95 Correct 104 ms 44268 KB Output is correct
96 Correct 115 ms 49388 KB Output is correct
97 Correct 29 ms 35956 KB Output is correct
98 Correct 26 ms 35576 KB Output is correct
99 Correct 143 ms 47212 KB Output is correct
100 Correct 32 ms 35960 KB Output is correct
101 Correct 124 ms 45928 KB Output is correct
102 Correct 114 ms 47084 KB Output is correct
103 Correct 28 ms 35832 KB Output is correct
104 Correct 27 ms 35832 KB Output is correct
105 Correct 30 ms 35960 KB Output is correct
106 Correct 29 ms 35960 KB Output is correct
107 Correct 29 ms 35964 KB Output is correct
108 Correct 901 ms 97756 KB Output is correct
109 Correct 847 ms 117960 KB Output is correct
110 Correct 883 ms 135956 KB Output is correct
111 Correct 715 ms 110812 KB Output is correct
112 Correct 721 ms 95088 KB Output is correct
113 Correct 545 ms 80220 KB Output is correct
114 Correct 731 ms 88924 KB Output is correct
115 Correct 727 ms 88924 KB Output is correct
116 Correct 1083 ms 139612 KB Output is correct
117 Correct 632 ms 73564 KB Output is correct
118 Correct 727 ms 88544 KB Output is correct
119 Correct 633 ms 73568 KB Output is correct
120 Correct 676 ms 96100 KB Output is correct
121 Correct 636 ms 73564 KB Output is correct
122 Correct 1202 ms 149852 KB Output is correct
123 Correct 466 ms 75040 KB Output is correct
124 Correct 687 ms 72924 KB Output is correct