답안 #359153

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
359153 2021-01-26T11:48:48 Z Nima_Naderi Mergers (JOI19_mergers) C++14
100 / 100
1402 ms 128096 KB
///In the name of GOD
//#pragma GCC optimize("O2")
#include<bits/stdc++.h>
using namespace std;

//typedef long long ll;
typedef int ll;
const ll MXN = 5e5 + 10;
ll n, k, leaf;
ll A[MXN], has[MXN], dad[MXN], hd[MXN];
ll Par[MXN], Sz[MXN], dis[MXN];
ll sub[MXN], hvs[MXN];
vector<ll> adj[MXN], G[MXN], Col[MXN];
vector<pair<ll, ll>> E;
ll Find(ll x){
    return (x == Par[x] ? x : Par[x] = Find(Par[x]));
}
void Union(ll x, ll y){
    x = Find(x), y = Find(y);
    if(x == y) return;
    if(Sz[x] < Sz[y]) swap(x, y);
    Sz[x] += Sz[y], Par[y] = x;
}
void prep(ll u, ll par){
    sub[u] ++, dad[u] = par;
    if(A[u]) Col[A[u]].push_back(u);
    for(auto v : adj[u]){
        if(v == par) continue;
        dis[v] = dis[u] + 1;
        prep(v, u);
        sub[u] += sub[v];
        if(sub[v] > sub[hvs[u]]) hvs[u] = v;
    }
}
void dfs(ll u, ll par, ll head){
    hd[u] = head;
    if(hvs[u]) dfs(hvs[u], u, head);
    for(auto v : adj[u]){
        if(v == par || v == hvs[u]) continue;
        dfs(v, u, v);
    }
}
void flood(ll u, ll par){
    for(auto v : adj[u]){
        if(v == par) continue;
        flood(v, u), has[u] += has[v];
        if(has[v]) Union(u, v);
    }
}
ll LCA(ll u, ll v){
    while(hd[u] != hd[v]){
        if(dis[hd[u]] > dis[hd[v]]) swap(u, v);
        v = dad[hd[v]];
    }
    if(dis[u] > dis[v]) swap(u, v);
    return u;
}
int main(){
    ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
    cin >> n >> k;
    iota(Par + 1, Par + n + 1, 1), fill(Sz + 1, Sz + n + 1, 1);
    for(int i = 1; i < n; i ++){
        ll u, v; cin >> u >> v;
        adj[u].push_back(v), adj[v].push_back(u);
        E.push_back({u, v});
    }
    for(int i = 1; i <= n; i ++) cin >> A[i];
    prep(1, 0), dfs(1, 0, 1);
    for(int i = 1; i <= k; i ++){
        for(int j = 1; j < Col[i].size(); j ++){
            ll u = Col[i][j - 1], v = Col[i][j];
            has[u] ++, has[v] ++, has[LCA(u, v)] -= 2;
        }
    }
    flood(1, 0);
    for(auto e : E){
        ll u, v; tie(u, v) = e;
        u = Find(u), v = Find(v);
        if(u == v) continue;
        G[u].push_back(v), G[v].push_back(u);
    }
    for(int i = 1; i <= n; i ++) leaf += (Find(i) == i && G[i].size() == 1);
    cout << (leaf + 1) / 2 << '\n';
    return 0;
}
/*!
    HE'S AN INSTIGATOR,
    ENEMY ELIMINATOR,
    AND WHEN HE KNOCKS YOU BETTER
    YOU BETTER LET HIM IN.
*/
//! N.N

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:70:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |         for(int j = 1; j < Col[i].size(); j ++){
      |                        ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 35564 KB Output is correct
2 Correct 25 ms 35692 KB Output is correct
3 Correct 25 ms 35692 KB Output is correct
4 Correct 25 ms 35564 KB Output is correct
5 Correct 25 ms 35564 KB Output is correct
6 Correct 26 ms 35692 KB Output is correct
7 Correct 25 ms 35564 KB Output is correct
8 Correct 26 ms 35692 KB Output is correct
9 Correct 25 ms 35564 KB Output is correct
10 Correct 25 ms 35564 KB Output is correct
11 Correct 25 ms 35692 KB Output is correct
12 Correct 25 ms 35692 KB Output is correct
13 Correct 43 ms 35564 KB Output is correct
14 Correct 46 ms 35616 KB Output is correct
15 Correct 25 ms 35692 KB Output is correct
16 Correct 24 ms 35564 KB Output is correct
17 Correct 26 ms 35564 KB Output is correct
18 Correct 44 ms 35692 KB Output is correct
19 Correct 25 ms 35564 KB Output is correct
20 Correct 25 ms 35564 KB Output is correct
21 Correct 27 ms 35692 KB Output is correct
22 Correct 25 ms 35692 KB Output is correct
23 Correct 26 ms 35564 KB Output is correct
24 Correct 25 ms 35660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 35564 KB Output is correct
2 Correct 25 ms 35692 KB Output is correct
3 Correct 25 ms 35692 KB Output is correct
4 Correct 25 ms 35564 KB Output is correct
5 Correct 25 ms 35564 KB Output is correct
6 Correct 26 ms 35692 KB Output is correct
7 Correct 25 ms 35564 KB Output is correct
8 Correct 26 ms 35692 KB Output is correct
9 Correct 25 ms 35564 KB Output is correct
10 Correct 25 ms 35564 KB Output is correct
11 Correct 25 ms 35692 KB Output is correct
12 Correct 25 ms 35692 KB Output is correct
13 Correct 43 ms 35564 KB Output is correct
14 Correct 46 ms 35616 KB Output is correct
15 Correct 25 ms 35692 KB Output is correct
16 Correct 24 ms 35564 KB Output is correct
17 Correct 26 ms 35564 KB Output is correct
18 Correct 44 ms 35692 KB Output is correct
19 Correct 25 ms 35564 KB Output is correct
20 Correct 25 ms 35564 KB Output is correct
21 Correct 27 ms 35692 KB Output is correct
22 Correct 25 ms 35692 KB Output is correct
23 Correct 26 ms 35564 KB Output is correct
24 Correct 25 ms 35660 KB Output is correct
25 Correct 27 ms 35692 KB Output is correct
26 Correct 49 ms 36076 KB Output is correct
27 Correct 27 ms 35948 KB Output is correct
28 Correct 27 ms 36204 KB Output is correct
29 Correct 28 ms 36076 KB Output is correct
30 Correct 28 ms 35948 KB Output is correct
31 Correct 26 ms 35692 KB Output is correct
32 Correct 27 ms 36204 KB Output is correct
33 Correct 25 ms 35692 KB Output is correct
34 Correct 26 ms 35948 KB Output is correct
35 Correct 28 ms 36076 KB Output is correct
36 Correct 28 ms 35948 KB Output is correct
37 Correct 27 ms 35948 KB Output is correct
38 Correct 25 ms 35816 KB Output is correct
39 Correct 27 ms 35948 KB Output is correct
40 Correct 30 ms 35948 KB Output is correct
41 Correct 72 ms 35948 KB Output is correct
42 Correct 63 ms 35948 KB Output is correct
43 Correct 62 ms 36204 KB Output is correct
44 Correct 29 ms 35692 KB Output is correct
45 Correct 31 ms 36076 KB Output is correct
46 Correct 27 ms 35948 KB Output is correct
47 Correct 29 ms 35588 KB Output is correct
48 Correct 30 ms 35948 KB Output is correct
49 Correct 49 ms 36064 KB Output is correct
50 Correct 28 ms 36204 KB Output is correct
51 Correct 51 ms 35948 KB Output is correct
52 Correct 30 ms 35948 KB Output is correct
53 Correct 28 ms 35948 KB Output is correct
54 Correct 29 ms 35948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 35564 KB Output is correct
2 Correct 25 ms 35692 KB Output is correct
3 Correct 25 ms 35692 KB Output is correct
4 Correct 25 ms 35564 KB Output is correct
5 Correct 25 ms 35564 KB Output is correct
6 Correct 26 ms 35692 KB Output is correct
7 Correct 25 ms 35564 KB Output is correct
8 Correct 26 ms 35692 KB Output is correct
9 Correct 25 ms 35564 KB Output is correct
10 Correct 25 ms 35564 KB Output is correct
11 Correct 25 ms 35692 KB Output is correct
12 Correct 25 ms 35692 KB Output is correct
13 Correct 43 ms 35564 KB Output is correct
14 Correct 46 ms 35616 KB Output is correct
15 Correct 25 ms 35692 KB Output is correct
16 Correct 24 ms 35564 KB Output is correct
17 Correct 26 ms 35564 KB Output is correct
18 Correct 44 ms 35692 KB Output is correct
19 Correct 25 ms 35564 KB Output is correct
20 Correct 25 ms 35564 KB Output is correct
21 Correct 27 ms 35692 KB Output is correct
22 Correct 25 ms 35692 KB Output is correct
23 Correct 26 ms 35564 KB Output is correct
24 Correct 25 ms 35660 KB Output is correct
25 Correct 29 ms 35692 KB Output is correct
26 Correct 89 ms 45020 KB Output is correct
27 Correct 138 ms 45152 KB Output is correct
28 Correct 27 ms 35948 KB Output is correct
29 Correct 25 ms 35692 KB Output is correct
30 Correct 25 ms 35692 KB Output is correct
31 Correct 150 ms 45024 KB Output is correct
32 Correct 26 ms 35948 KB Output is correct
33 Correct 166 ms 52372 KB Output is correct
34 Correct 145 ms 45152 KB Output is correct
35 Correct 26 ms 35948 KB Output is correct
36 Correct 147 ms 45920 KB Output is correct
37 Correct 30 ms 35948 KB Output is correct
38 Correct 28 ms 35948 KB Output is correct
39 Correct 128 ms 45024 KB Output is correct
40 Correct 27 ms 36204 KB Output is correct
41 Correct 137 ms 45408 KB Output is correct
42 Correct 151 ms 47456 KB Output is correct
43 Correct 25 ms 35692 KB Output is correct
44 Correct 140 ms 52832 KB Output is correct
45 Correct 136 ms 48864 KB Output is correct
46 Correct 29 ms 35948 KB Output is correct
47 Correct 27 ms 35948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 92 ms 45020 KB Output is correct
2 Correct 154 ms 51164 KB Output is correct
3 Correct 27 ms 36076 KB Output is correct
4 Correct 28 ms 35948 KB Output is correct
5 Correct 32 ms 35692 KB Output is correct
6 Correct 30 ms 35692 KB Output is correct
7 Correct 26 ms 35868 KB Output is correct
8 Correct 198 ms 46944 KB Output is correct
9 Correct 28 ms 35948 KB Output is correct
10 Correct 146 ms 45416 KB Output is correct
11 Correct 26 ms 35692 KB Output is correct
12 Correct 173 ms 45416 KB Output is correct
13 Correct 168 ms 47072 KB Output is correct
14 Correct 195 ms 51296 KB Output is correct
15 Correct 100 ms 45024 KB Output is correct
16 Correct 27 ms 35948 KB Output is correct
17 Correct 25 ms 35692 KB Output is correct
18 Correct 128 ms 50528 KB Output is correct
19 Correct 212 ms 55136 KB Output is correct
20 Correct 29 ms 35948 KB Output is correct
21 Correct 26 ms 35692 KB Output is correct
22 Correct 114 ms 46940 KB Output is correct
23 Correct 28 ms 35948 KB Output is correct
24 Correct 152 ms 45792 KB Output is correct
25 Correct 229 ms 53728 KB Output is correct
26 Correct 29 ms 36152 KB Output is correct
27 Correct 32 ms 36204 KB Output is correct
28 Correct 26 ms 35948 KB Output is correct
29 Correct 28 ms 35948 KB Output is correct
30 Correct 26 ms 35948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 35564 KB Output is correct
2 Correct 25 ms 35692 KB Output is correct
3 Correct 25 ms 35692 KB Output is correct
4 Correct 25 ms 35564 KB Output is correct
5 Correct 25 ms 35564 KB Output is correct
6 Correct 26 ms 35692 KB Output is correct
7 Correct 25 ms 35564 KB Output is correct
8 Correct 26 ms 35692 KB Output is correct
9 Correct 25 ms 35564 KB Output is correct
10 Correct 25 ms 35564 KB Output is correct
11 Correct 25 ms 35692 KB Output is correct
12 Correct 25 ms 35692 KB Output is correct
13 Correct 43 ms 35564 KB Output is correct
14 Correct 46 ms 35616 KB Output is correct
15 Correct 25 ms 35692 KB Output is correct
16 Correct 24 ms 35564 KB Output is correct
17 Correct 26 ms 35564 KB Output is correct
18 Correct 44 ms 35692 KB Output is correct
19 Correct 25 ms 35564 KB Output is correct
20 Correct 25 ms 35564 KB Output is correct
21 Correct 27 ms 35692 KB Output is correct
22 Correct 25 ms 35692 KB Output is correct
23 Correct 26 ms 35564 KB Output is correct
24 Correct 25 ms 35660 KB Output is correct
25 Correct 27 ms 35692 KB Output is correct
26 Correct 49 ms 36076 KB Output is correct
27 Correct 27 ms 35948 KB Output is correct
28 Correct 27 ms 36204 KB Output is correct
29 Correct 28 ms 36076 KB Output is correct
30 Correct 28 ms 35948 KB Output is correct
31 Correct 26 ms 35692 KB Output is correct
32 Correct 27 ms 36204 KB Output is correct
33 Correct 25 ms 35692 KB Output is correct
34 Correct 26 ms 35948 KB Output is correct
35 Correct 28 ms 36076 KB Output is correct
36 Correct 28 ms 35948 KB Output is correct
37 Correct 27 ms 35948 KB Output is correct
38 Correct 25 ms 35816 KB Output is correct
39 Correct 27 ms 35948 KB Output is correct
40 Correct 30 ms 35948 KB Output is correct
41 Correct 72 ms 35948 KB Output is correct
42 Correct 63 ms 35948 KB Output is correct
43 Correct 62 ms 36204 KB Output is correct
44 Correct 29 ms 35692 KB Output is correct
45 Correct 31 ms 36076 KB Output is correct
46 Correct 27 ms 35948 KB Output is correct
47 Correct 29 ms 35588 KB Output is correct
48 Correct 30 ms 35948 KB Output is correct
49 Correct 49 ms 36064 KB Output is correct
50 Correct 28 ms 36204 KB Output is correct
51 Correct 51 ms 35948 KB Output is correct
52 Correct 30 ms 35948 KB Output is correct
53 Correct 28 ms 35948 KB Output is correct
54 Correct 29 ms 35948 KB Output is correct
55 Correct 29 ms 35692 KB Output is correct
56 Correct 89 ms 45020 KB Output is correct
57 Correct 138 ms 45152 KB Output is correct
58 Correct 27 ms 35948 KB Output is correct
59 Correct 25 ms 35692 KB Output is correct
60 Correct 25 ms 35692 KB Output is correct
61 Correct 150 ms 45024 KB Output is correct
62 Correct 26 ms 35948 KB Output is correct
63 Correct 166 ms 52372 KB Output is correct
64 Correct 145 ms 45152 KB Output is correct
65 Correct 26 ms 35948 KB Output is correct
66 Correct 147 ms 45920 KB Output is correct
67 Correct 30 ms 35948 KB Output is correct
68 Correct 28 ms 35948 KB Output is correct
69 Correct 128 ms 45024 KB Output is correct
70 Correct 27 ms 36204 KB Output is correct
71 Correct 137 ms 45408 KB Output is correct
72 Correct 151 ms 47456 KB Output is correct
73 Correct 25 ms 35692 KB Output is correct
74 Correct 140 ms 52832 KB Output is correct
75 Correct 136 ms 48864 KB Output is correct
76 Correct 29 ms 35948 KB Output is correct
77 Correct 27 ms 35948 KB Output is correct
78 Correct 92 ms 45020 KB Output is correct
79 Correct 154 ms 51164 KB Output is correct
80 Correct 27 ms 36076 KB Output is correct
81 Correct 28 ms 35948 KB Output is correct
82 Correct 32 ms 35692 KB Output is correct
83 Correct 30 ms 35692 KB Output is correct
84 Correct 26 ms 35868 KB Output is correct
85 Correct 198 ms 46944 KB Output is correct
86 Correct 28 ms 35948 KB Output is correct
87 Correct 146 ms 45416 KB Output is correct
88 Correct 26 ms 35692 KB Output is correct
89 Correct 173 ms 45416 KB Output is correct
90 Correct 168 ms 47072 KB Output is correct
91 Correct 195 ms 51296 KB Output is correct
92 Correct 100 ms 45024 KB Output is correct
93 Correct 27 ms 35948 KB Output is correct
94 Correct 25 ms 35692 KB Output is correct
95 Correct 128 ms 50528 KB Output is correct
96 Correct 212 ms 55136 KB Output is correct
97 Correct 29 ms 35948 KB Output is correct
98 Correct 26 ms 35692 KB Output is correct
99 Correct 114 ms 46940 KB Output is correct
100 Correct 28 ms 35948 KB Output is correct
101 Correct 152 ms 45792 KB Output is correct
102 Correct 229 ms 53728 KB Output is correct
103 Correct 29 ms 36152 KB Output is correct
104 Correct 32 ms 36204 KB Output is correct
105 Correct 26 ms 35948 KB Output is correct
106 Correct 28 ms 35948 KB Output is correct
107 Correct 26 ms 35948 KB Output is correct
108 Correct 767 ms 82732 KB Output is correct
109 Correct 971 ms 96596 KB Output is correct
110 Correct 937 ms 108616 KB Output is correct
111 Correct 1352 ms 128096 KB Output is correct
112 Correct 1231 ms 113360 KB Output is correct
113 Correct 945 ms 102712 KB Output is correct
114 Correct 922 ms 78512 KB Output is correct
115 Correct 892 ms 79444 KB Output is correct
116 Correct 1074 ms 79516 KB Output is correct
117 Correct 1231 ms 106812 KB Output is correct
118 Correct 878 ms 77660 KB Output is correct
119 Correct 1253 ms 106924 KB Output is correct
120 Correct 1402 ms 120256 KB Output is correct
121 Correct 1267 ms 106844 KB Output is correct
122 Correct 1178 ms 87640 KB Output is correct
123 Correct 944 ms 105800 KB Output is correct
124 Correct 1237 ms 99668 KB Output is correct