Submission #1094979

# Submission time Handle Problem Language Result Execution time Memory
1094979 2024-10-01T05:37:31 Z pokmui9909 Mergers (JOI19_mergers) C++17
70 / 100
3000 ms 65876 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define x first
#define y second

ll N, K, A[500005], S[500005], vis[500005];
vector<ll> T[500005];

ll p[500005];
ll Find(ll n) { return (p[n] < 0 ? n : p[n] = Find(p[n])); }
void Union(ll u, ll v) { u = Find(u), v = Find(v); if(u != v) p[u] += p[v], p[v] = u; }

ll Size(ll u, ll p){
    S[u] = 1;
    for(auto v : T[u]){
        if(v != p && !vis[v]) S[u] += Size(v, u);
    }
    return S[u];
}
ll Cent(ll u, ll p, ll n){
    for(auto v : T[u]){
        if(v != p && !vis[v] && S[v] > n / 2) return Cent(v, u, n);
    }
    return u;
}   

ll Cnt[500005];

void dfs(ll u, ll p, ll d){
    Cnt[A[u]] += d;
    for(auto v : T[u]){
        if(v == p || vis[v]) continue;
        dfs(v, u, d);
    }
}
ll Comp(ll u, ll p, ll c){
    ll ok = (Cnt[A[u]] > 0);
    for(auto v : T[u]){
        if(v == p || vis[v]) continue;
        if(Comp(v, u, c)) ok = 1;
    }
    if(ok) Union(A[u], c);
    return ok;
}
void dnc(ll _u){
    ll u = Cent(_u, -1, Size(_u, -1));
    vis[u] = 1;

    Cnt[A[u]]++;
    for(auto v : T[u]){
        if(!vis[v]) dfs(v, u, 1);
    }
    for(auto v : T[u]){
        if(vis[v]) continue;
        dfs(v, u, -1);
        Comp(v, u, A[u]);
        dfs(v, u, 1);
    }
    for(auto v : T[u]){
        if(!vis[v]) dfs(v, u, -1);
    }
    Cnt[A[u]]--;

    for(auto v : T[u]){
        if(!vis[v]) dnc(v);
    }
}

ll deg[500005];

int main(){
    cin.tie(0) -> sync_with_stdio(0);

    cin >> N >> K;
    for(ll i = 1; i < N; i++){
        ll u, v; cin >> u >> v;
        T[u].push_back(v); T[v].push_back(u);
    }
    for(ll i = 1; i <= N; i++){
        cin >> A[i];
    }
    fill(p, p + N + 1, -1LL);
    dnc(1);
    for(ll i = 1; i <= N; i++){
        for(auto j : T[i]){
            if(i > j) continue;
            ll a = Find(A[i]), b = Find(A[j]);
            if(a != b){
                deg[a]++, deg[b]++;
            }
        }
    }
    ll f = 0;
    for(ll i = 1; i <= N; i++){
        if(deg[i] == 1) f++;
    }
    cout << (f + 1) / 2;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 12120 KB Output is correct
2 Correct 4 ms 12056 KB Output is correct
3 Correct 5 ms 12124 KB Output is correct
4 Correct 5 ms 12124 KB Output is correct
5 Correct 5 ms 12232 KB Output is correct
6 Correct 5 ms 12068 KB Output is correct
7 Correct 4 ms 12124 KB Output is correct
8 Correct 5 ms 12124 KB Output is correct
9 Correct 5 ms 12124 KB Output is correct
10 Correct 4 ms 12004 KB Output is correct
11 Correct 5 ms 12120 KB Output is correct
12 Correct 5 ms 12124 KB Output is correct
13 Correct 4 ms 12124 KB Output is correct
14 Correct 5 ms 12124 KB Output is correct
15 Correct 5 ms 12104 KB Output is correct
16 Correct 5 ms 12124 KB Output is correct
17 Correct 5 ms 12124 KB Output is correct
18 Correct 5 ms 12124 KB Output is correct
19 Correct 4 ms 12124 KB Output is correct
20 Correct 5 ms 12124 KB Output is correct
21 Correct 5 ms 12124 KB Output is correct
22 Correct 5 ms 12120 KB Output is correct
23 Correct 5 ms 12124 KB Output is correct
24 Correct 4 ms 12160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 12120 KB Output is correct
2 Correct 4 ms 12056 KB Output is correct
3 Correct 5 ms 12124 KB Output is correct
4 Correct 5 ms 12124 KB Output is correct
5 Correct 5 ms 12232 KB Output is correct
6 Correct 5 ms 12068 KB Output is correct
7 Correct 4 ms 12124 KB Output is correct
8 Correct 5 ms 12124 KB Output is correct
9 Correct 5 ms 12124 KB Output is correct
10 Correct 4 ms 12004 KB Output is correct
11 Correct 5 ms 12120 KB Output is correct
12 Correct 5 ms 12124 KB Output is correct
13 Correct 4 ms 12124 KB Output is correct
14 Correct 5 ms 12124 KB Output is correct
15 Correct 5 ms 12104 KB Output is correct
16 Correct 5 ms 12124 KB Output is correct
17 Correct 5 ms 12124 KB Output is correct
18 Correct 5 ms 12124 KB Output is correct
19 Correct 4 ms 12124 KB Output is correct
20 Correct 5 ms 12124 KB Output is correct
21 Correct 5 ms 12124 KB Output is correct
22 Correct 5 ms 12120 KB Output is correct
23 Correct 5 ms 12124 KB Output is correct
24 Correct 4 ms 12160 KB Output is correct
25 Correct 5 ms 12120 KB Output is correct
26 Correct 8 ms 12632 KB Output is correct
27 Correct 7 ms 12380 KB Output is correct
28 Correct 7 ms 12644 KB Output is correct
29 Correct 7 ms 12388 KB Output is correct
30 Correct 7 ms 12336 KB Output is correct
31 Correct 5 ms 12132 KB Output is correct
32 Correct 8 ms 12644 KB Output is correct
33 Correct 5 ms 12132 KB Output is correct
34 Correct 5 ms 12388 KB Output is correct
35 Correct 7 ms 12484 KB Output is correct
36 Correct 7 ms 12388 KB Output is correct
37 Correct 7 ms 12336 KB Output is correct
38 Correct 6 ms 12136 KB Output is correct
39 Correct 7 ms 12392 KB Output is correct
40 Correct 7 ms 12392 KB Output is correct
41 Correct 7 ms 12460 KB Output is correct
42 Correct 6 ms 12392 KB Output is correct
43 Correct 8 ms 12648 KB Output is correct
44 Correct 5 ms 12136 KB Output is correct
45 Correct 7 ms 12392 KB Output is correct
46 Correct 7 ms 12388 KB Output is correct
47 Correct 5 ms 12212 KB Output is correct
48 Correct 7 ms 12280 KB Output is correct
49 Correct 5 ms 12380 KB Output is correct
50 Correct 7 ms 12380 KB Output is correct
51 Correct 8 ms 12380 KB Output is correct
52 Correct 6 ms 12380 KB Output is correct
53 Correct 5 ms 12380 KB Output is correct
54 Correct 7 ms 12380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 12120 KB Output is correct
2 Correct 4 ms 12056 KB Output is correct
3 Correct 5 ms 12124 KB Output is correct
4 Correct 5 ms 12124 KB Output is correct
5 Correct 5 ms 12232 KB Output is correct
6 Correct 5 ms 12068 KB Output is correct
7 Correct 4 ms 12124 KB Output is correct
8 Correct 5 ms 12124 KB Output is correct
9 Correct 5 ms 12124 KB Output is correct
10 Correct 4 ms 12004 KB Output is correct
11 Correct 5 ms 12120 KB Output is correct
12 Correct 5 ms 12124 KB Output is correct
13 Correct 4 ms 12124 KB Output is correct
14 Correct 5 ms 12124 KB Output is correct
15 Correct 5 ms 12104 KB Output is correct
16 Correct 5 ms 12124 KB Output is correct
17 Correct 5 ms 12124 KB Output is correct
18 Correct 5 ms 12124 KB Output is correct
19 Correct 4 ms 12124 KB Output is correct
20 Correct 5 ms 12124 KB Output is correct
21 Correct 5 ms 12124 KB Output is correct
22 Correct 5 ms 12120 KB Output is correct
23 Correct 5 ms 12124 KB Output is correct
24 Correct 4 ms 12160 KB Output is correct
25 Correct 6 ms 12124 KB Output is correct
26 Correct 34 ms 20656 KB Output is correct
27 Correct 138 ms 20308 KB Output is correct
28 Correct 8 ms 12380 KB Output is correct
29 Correct 5 ms 12124 KB Output is correct
30 Correct 6 ms 12124 KB Output is correct
31 Correct 145 ms 20516 KB Output is correct
32 Correct 5 ms 12380 KB Output is correct
33 Correct 307 ms 25720 KB Output is correct
34 Correct 139 ms 20308 KB Output is correct
35 Correct 8 ms 12488 KB Output is correct
36 Correct 254 ms 20576 KB Output is correct
37 Correct 7 ms 12380 KB Output is correct
38 Correct 7 ms 12468 KB Output is correct
39 Correct 35 ms 20664 KB Output is correct
40 Correct 7 ms 12636 KB Output is correct
41 Correct 128 ms 20288 KB Output is correct
42 Correct 288 ms 22108 KB Output is correct
43 Correct 5 ms 12124 KB Output is correct
44 Correct 287 ms 25840 KB Output is correct
45 Correct 248 ms 23972 KB Output is correct
46 Correct 5 ms 12380 KB Output is correct
47 Correct 6 ms 12380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 19144 KB Output is correct
2 Correct 34 ms 20680 KB Output is correct
3 Correct 6 ms 12380 KB Output is correct
4 Correct 7 ms 12380 KB Output is correct
5 Correct 4 ms 12124 KB Output is correct
6 Correct 5 ms 12124 KB Output is correct
7 Correct 6 ms 12396 KB Output is correct
8 Correct 161 ms 21376 KB Output is correct
9 Correct 7 ms 12376 KB Output is correct
10 Correct 157 ms 20500 KB Output is correct
11 Correct 5 ms 12136 KB Output is correct
12 Correct 225 ms 20108 KB Output is correct
13 Correct 185 ms 21480 KB Output is correct
14 Correct 132 ms 22368 KB Output is correct
15 Correct 40 ms 20568 KB Output is correct
16 Correct 5 ms 12392 KB Output is correct
17 Correct 5 ms 12228 KB Output is correct
18 Correct 40 ms 22304 KB Output is correct
19 Correct 318 ms 25184 KB Output is correct
20 Correct 7 ms 12392 KB Output is correct
21 Correct 5 ms 12136 KB Output is correct
22 Correct 44 ms 21656 KB Output is correct
23 Correct 6 ms 12392 KB Output is correct
24 Correct 206 ms 20508 KB Output is correct
25 Correct 242 ms 25108 KB Output is correct
26 Correct 5 ms 12392 KB Output is correct
27 Correct 7 ms 12392 KB Output is correct
28 Correct 8 ms 12644 KB Output is correct
29 Correct 5 ms 12392 KB Output is correct
30 Correct 6 ms 12484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 12120 KB Output is correct
2 Correct 4 ms 12056 KB Output is correct
3 Correct 5 ms 12124 KB Output is correct
4 Correct 5 ms 12124 KB Output is correct
5 Correct 5 ms 12232 KB Output is correct
6 Correct 5 ms 12068 KB Output is correct
7 Correct 4 ms 12124 KB Output is correct
8 Correct 5 ms 12124 KB Output is correct
9 Correct 5 ms 12124 KB Output is correct
10 Correct 4 ms 12004 KB Output is correct
11 Correct 5 ms 12120 KB Output is correct
12 Correct 5 ms 12124 KB Output is correct
13 Correct 4 ms 12124 KB Output is correct
14 Correct 5 ms 12124 KB Output is correct
15 Correct 5 ms 12104 KB Output is correct
16 Correct 5 ms 12124 KB Output is correct
17 Correct 5 ms 12124 KB Output is correct
18 Correct 5 ms 12124 KB Output is correct
19 Correct 4 ms 12124 KB Output is correct
20 Correct 5 ms 12124 KB Output is correct
21 Correct 5 ms 12124 KB Output is correct
22 Correct 5 ms 12120 KB Output is correct
23 Correct 5 ms 12124 KB Output is correct
24 Correct 4 ms 12160 KB Output is correct
25 Correct 5 ms 12120 KB Output is correct
26 Correct 8 ms 12632 KB Output is correct
27 Correct 7 ms 12380 KB Output is correct
28 Correct 7 ms 12644 KB Output is correct
29 Correct 7 ms 12388 KB Output is correct
30 Correct 7 ms 12336 KB Output is correct
31 Correct 5 ms 12132 KB Output is correct
32 Correct 8 ms 12644 KB Output is correct
33 Correct 5 ms 12132 KB Output is correct
34 Correct 5 ms 12388 KB Output is correct
35 Correct 7 ms 12484 KB Output is correct
36 Correct 7 ms 12388 KB Output is correct
37 Correct 7 ms 12336 KB Output is correct
38 Correct 6 ms 12136 KB Output is correct
39 Correct 7 ms 12392 KB Output is correct
40 Correct 7 ms 12392 KB Output is correct
41 Correct 7 ms 12460 KB Output is correct
42 Correct 6 ms 12392 KB Output is correct
43 Correct 8 ms 12648 KB Output is correct
44 Correct 5 ms 12136 KB Output is correct
45 Correct 7 ms 12392 KB Output is correct
46 Correct 7 ms 12388 KB Output is correct
47 Correct 5 ms 12212 KB Output is correct
48 Correct 7 ms 12280 KB Output is correct
49 Correct 5 ms 12380 KB Output is correct
50 Correct 7 ms 12380 KB Output is correct
51 Correct 8 ms 12380 KB Output is correct
52 Correct 6 ms 12380 KB Output is correct
53 Correct 5 ms 12380 KB Output is correct
54 Correct 7 ms 12380 KB Output is correct
55 Correct 6 ms 12124 KB Output is correct
56 Correct 34 ms 20656 KB Output is correct
57 Correct 138 ms 20308 KB Output is correct
58 Correct 8 ms 12380 KB Output is correct
59 Correct 5 ms 12124 KB Output is correct
60 Correct 6 ms 12124 KB Output is correct
61 Correct 145 ms 20516 KB Output is correct
62 Correct 5 ms 12380 KB Output is correct
63 Correct 307 ms 25720 KB Output is correct
64 Correct 139 ms 20308 KB Output is correct
65 Correct 8 ms 12488 KB Output is correct
66 Correct 254 ms 20576 KB Output is correct
67 Correct 7 ms 12380 KB Output is correct
68 Correct 7 ms 12468 KB Output is correct
69 Correct 35 ms 20664 KB Output is correct
70 Correct 7 ms 12636 KB Output is correct
71 Correct 128 ms 20288 KB Output is correct
72 Correct 288 ms 22108 KB Output is correct
73 Correct 5 ms 12124 KB Output is correct
74 Correct 287 ms 25840 KB Output is correct
75 Correct 248 ms 23972 KB Output is correct
76 Correct 5 ms 12380 KB Output is correct
77 Correct 6 ms 12380 KB Output is correct
78 Correct 31 ms 19144 KB Output is correct
79 Correct 34 ms 20680 KB Output is correct
80 Correct 6 ms 12380 KB Output is correct
81 Correct 7 ms 12380 KB Output is correct
82 Correct 4 ms 12124 KB Output is correct
83 Correct 5 ms 12124 KB Output is correct
84 Correct 6 ms 12396 KB Output is correct
85 Correct 161 ms 21376 KB Output is correct
86 Correct 7 ms 12376 KB Output is correct
87 Correct 157 ms 20500 KB Output is correct
88 Correct 5 ms 12136 KB Output is correct
89 Correct 225 ms 20108 KB Output is correct
90 Correct 185 ms 21480 KB Output is correct
91 Correct 132 ms 22368 KB Output is correct
92 Correct 40 ms 20568 KB Output is correct
93 Correct 5 ms 12392 KB Output is correct
94 Correct 5 ms 12228 KB Output is correct
95 Correct 40 ms 22304 KB Output is correct
96 Correct 318 ms 25184 KB Output is correct
97 Correct 7 ms 12392 KB Output is correct
98 Correct 5 ms 12136 KB Output is correct
99 Correct 44 ms 21656 KB Output is correct
100 Correct 6 ms 12392 KB Output is correct
101 Correct 206 ms 20508 KB Output is correct
102 Correct 242 ms 25108 KB Output is correct
103 Correct 5 ms 12392 KB Output is correct
104 Correct 7 ms 12392 KB Output is correct
105 Correct 8 ms 12644 KB Output is correct
106 Correct 5 ms 12392 KB Output is correct
107 Correct 6 ms 12484 KB Output is correct
108 Correct 275 ms 60392 KB Output is correct
109 Execution timed out 3075 ms 65876 KB Time limit exceeded
110 Halted 0 ms 0 KB -