답안 #1094987

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1094987 2024-10-01T05:58:34 Z pokmui9909 Mergers (JOI19_mergers) C++17
70 / 100
3000 ms 212704 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 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];
vector<ll> G[500005], H[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){
        G[c].push_back(A[u]); G[A[u]].push_back(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], Num[500005];

void f(ll u){
    for(auto v : G[u]){
        if(Num[v]) continue;
        Num[v] = Num[u]; f(v);
    }
}

ll chk[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];
    }
    dnc(1);
    for(ll i = 1; i <= N; i++){
        for(auto j : G[i]){
            if(chk[j]) continue;
            H[i].push_back(j);
            chk[j] = 1;
        }
        for(auto j : H[i]) chk[j] = 0;
    }
    for(ll i = 1, j = 0; i <= N; i++){
        if(Num[i]) continue;
        Num[i] = ++j; f(i);
    }
    for(ll i = 1; i <= N; i++){
        for(auto j : T[i]){
            if(i > j) continue;
            ll a = Num[A[i]], b = Num[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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 37724 KB Output is correct
2 Correct 13 ms 37756 KB Output is correct
3 Correct 14 ms 37720 KB Output is correct
4 Correct 13 ms 37720 KB Output is correct
5 Correct 14 ms 37724 KB Output is correct
6 Correct 13 ms 37720 KB Output is correct
7 Correct 13 ms 37720 KB Output is correct
8 Correct 13 ms 37724 KB Output is correct
9 Correct 14 ms 37724 KB Output is correct
10 Correct 14 ms 37720 KB Output is correct
11 Correct 14 ms 37704 KB Output is correct
12 Correct 14 ms 37724 KB Output is correct
13 Correct 13 ms 37760 KB Output is correct
14 Correct 13 ms 37560 KB Output is correct
15 Correct 13 ms 37724 KB Output is correct
16 Correct 13 ms 37720 KB Output is correct
17 Correct 14 ms 37724 KB Output is correct
18 Correct 15 ms 37724 KB Output is correct
19 Correct 14 ms 37672 KB Output is correct
20 Correct 15 ms 37724 KB Output is correct
21 Correct 14 ms 37720 KB Output is correct
22 Correct 13 ms 37724 KB Output is correct
23 Correct 14 ms 37724 KB Output is correct
24 Correct 15 ms 37540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 37724 KB Output is correct
2 Correct 13 ms 37756 KB Output is correct
3 Correct 14 ms 37720 KB Output is correct
4 Correct 13 ms 37720 KB Output is correct
5 Correct 14 ms 37724 KB Output is correct
6 Correct 13 ms 37720 KB Output is correct
7 Correct 13 ms 37720 KB Output is correct
8 Correct 13 ms 37724 KB Output is correct
9 Correct 14 ms 37724 KB Output is correct
10 Correct 14 ms 37720 KB Output is correct
11 Correct 14 ms 37704 KB Output is correct
12 Correct 14 ms 37724 KB Output is correct
13 Correct 13 ms 37760 KB Output is correct
14 Correct 13 ms 37560 KB Output is correct
15 Correct 13 ms 37724 KB Output is correct
16 Correct 13 ms 37720 KB Output is correct
17 Correct 14 ms 37724 KB Output is correct
18 Correct 15 ms 37724 KB Output is correct
19 Correct 14 ms 37672 KB Output is correct
20 Correct 15 ms 37724 KB Output is correct
21 Correct 14 ms 37720 KB Output is correct
22 Correct 13 ms 37724 KB Output is correct
23 Correct 14 ms 37724 KB Output is correct
24 Correct 15 ms 37540 KB Output is correct
25 Correct 14 ms 37700 KB Output is correct
26 Correct 16 ms 38108 KB Output is correct
27 Correct 18 ms 38232 KB Output is correct
28 Correct 18 ms 38236 KB Output is correct
29 Correct 16 ms 38040 KB Output is correct
30 Correct 18 ms 38092 KB Output is correct
31 Correct 15 ms 37720 KB Output is correct
32 Correct 17 ms 38232 KB Output is correct
33 Correct 15 ms 37724 KB Output is correct
34 Correct 15 ms 37980 KB Output is correct
35 Correct 18 ms 37992 KB Output is correct
36 Correct 17 ms 37980 KB Output is correct
37 Correct 17 ms 37980 KB Output is correct
38 Correct 14 ms 37724 KB Output is correct
39 Correct 17 ms 37976 KB Output is correct
40 Correct 19 ms 38392 KB Output is correct
41 Correct 16 ms 38236 KB Output is correct
42 Correct 15 ms 37980 KB Output is correct
43 Correct 17 ms 38616 KB Output is correct
44 Correct 19 ms 37976 KB Output is correct
45 Correct 18 ms 38284 KB Output is correct
46 Correct 17 ms 37980 KB Output is correct
47 Correct 14 ms 37724 KB Output is correct
48 Correct 16 ms 38084 KB Output is correct
49 Correct 17 ms 37976 KB Output is correct
50 Correct 17 ms 37980 KB Output is correct
51 Correct 17 ms 37976 KB Output is correct
52 Correct 16 ms 37980 KB Output is correct
53 Correct 14 ms 37984 KB Output is correct
54 Correct 16 ms 38256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 37724 KB Output is correct
2 Correct 13 ms 37756 KB Output is correct
3 Correct 14 ms 37720 KB Output is correct
4 Correct 13 ms 37720 KB Output is correct
5 Correct 14 ms 37724 KB Output is correct
6 Correct 13 ms 37720 KB Output is correct
7 Correct 13 ms 37720 KB Output is correct
8 Correct 13 ms 37724 KB Output is correct
9 Correct 14 ms 37724 KB Output is correct
10 Correct 14 ms 37720 KB Output is correct
11 Correct 14 ms 37704 KB Output is correct
12 Correct 14 ms 37724 KB Output is correct
13 Correct 13 ms 37760 KB Output is correct
14 Correct 13 ms 37560 KB Output is correct
15 Correct 13 ms 37724 KB Output is correct
16 Correct 13 ms 37720 KB Output is correct
17 Correct 14 ms 37724 KB Output is correct
18 Correct 15 ms 37724 KB Output is correct
19 Correct 14 ms 37672 KB Output is correct
20 Correct 15 ms 37724 KB Output is correct
21 Correct 14 ms 37720 KB Output is correct
22 Correct 13 ms 37724 KB Output is correct
23 Correct 14 ms 37724 KB Output is correct
24 Correct 15 ms 37540 KB Output is correct
25 Correct 13 ms 37724 KB Output is correct
26 Correct 48 ms 48072 KB Output is correct
27 Correct 154 ms 59740 KB Output is correct
28 Correct 17 ms 38236 KB Output is correct
29 Correct 14 ms 37724 KB Output is correct
30 Correct 13 ms 37536 KB Output is correct
31 Correct 156 ms 55056 KB Output is correct
32 Correct 15 ms 37976 KB Output is correct
33 Correct 350 ms 77524 KB Output is correct
34 Correct 166 ms 59012 KB Output is correct
35 Correct 17 ms 37980 KB Output is correct
36 Correct 272 ms 61628 KB Output is correct
37 Correct 16 ms 38364 KB Output is correct
38 Correct 16 ms 38236 KB Output is correct
39 Correct 51 ms 48904 KB Output is correct
40 Correct 17 ms 38616 KB Output is correct
41 Correct 157 ms 61356 KB Output is correct
42 Correct 334 ms 73404 KB Output is correct
43 Correct 14 ms 37784 KB Output is correct
44 Correct 344 ms 83096 KB Output is correct
45 Correct 264 ms 74804 KB Output is correct
46 Correct 14 ms 37980 KB Output is correct
47 Correct 16 ms 38296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 47816 KB Output is correct
2 Correct 49 ms 47300 KB Output is correct
3 Correct 16 ms 37976 KB Output is correct
4 Correct 17 ms 38236 KB Output is correct
5 Correct 14 ms 37724 KB Output is correct
6 Correct 14 ms 37724 KB Output is correct
7 Correct 18 ms 37996 KB Output is correct
8 Correct 187 ms 52564 KB Output is correct
9 Correct 16 ms 37976 KB Output is correct
10 Correct 169 ms 53620 KB Output is correct
11 Correct 13 ms 37724 KB Output is correct
12 Correct 240 ms 53840 KB Output is correct
13 Correct 190 ms 50768 KB Output is correct
14 Correct 145 ms 47120 KB Output is correct
15 Correct 52 ms 48960 KB Output is correct
16 Correct 17 ms 37976 KB Output is correct
17 Correct 14 ms 37724 KB Output is correct
18 Correct 54 ms 48536 KB Output is correct
19 Correct 321 ms 50260 KB Output is correct
20 Correct 16 ms 37976 KB Output is correct
21 Correct 14 ms 37720 KB Output is correct
22 Correct 59 ms 49860 KB Output is correct
23 Correct 17 ms 38112 KB Output is correct
24 Correct 217 ms 50736 KB Output is correct
25 Correct 243 ms 49920 KB Output is correct
26 Correct 15 ms 37980 KB Output is correct
27 Correct 16 ms 37976 KB Output is correct
28 Correct 18 ms 37976 KB Output is correct
29 Correct 16 ms 37980 KB Output is correct
30 Correct 15 ms 37992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 37724 KB Output is correct
2 Correct 13 ms 37756 KB Output is correct
3 Correct 14 ms 37720 KB Output is correct
4 Correct 13 ms 37720 KB Output is correct
5 Correct 14 ms 37724 KB Output is correct
6 Correct 13 ms 37720 KB Output is correct
7 Correct 13 ms 37720 KB Output is correct
8 Correct 13 ms 37724 KB Output is correct
9 Correct 14 ms 37724 KB Output is correct
10 Correct 14 ms 37720 KB Output is correct
11 Correct 14 ms 37704 KB Output is correct
12 Correct 14 ms 37724 KB Output is correct
13 Correct 13 ms 37760 KB Output is correct
14 Correct 13 ms 37560 KB Output is correct
15 Correct 13 ms 37724 KB Output is correct
16 Correct 13 ms 37720 KB Output is correct
17 Correct 14 ms 37724 KB Output is correct
18 Correct 15 ms 37724 KB Output is correct
19 Correct 14 ms 37672 KB Output is correct
20 Correct 15 ms 37724 KB Output is correct
21 Correct 14 ms 37720 KB Output is correct
22 Correct 13 ms 37724 KB Output is correct
23 Correct 14 ms 37724 KB Output is correct
24 Correct 15 ms 37540 KB Output is correct
25 Correct 14 ms 37700 KB Output is correct
26 Correct 16 ms 38108 KB Output is correct
27 Correct 18 ms 38232 KB Output is correct
28 Correct 18 ms 38236 KB Output is correct
29 Correct 16 ms 38040 KB Output is correct
30 Correct 18 ms 38092 KB Output is correct
31 Correct 15 ms 37720 KB Output is correct
32 Correct 17 ms 38232 KB Output is correct
33 Correct 15 ms 37724 KB Output is correct
34 Correct 15 ms 37980 KB Output is correct
35 Correct 18 ms 37992 KB Output is correct
36 Correct 17 ms 37980 KB Output is correct
37 Correct 17 ms 37980 KB Output is correct
38 Correct 14 ms 37724 KB Output is correct
39 Correct 17 ms 37976 KB Output is correct
40 Correct 19 ms 38392 KB Output is correct
41 Correct 16 ms 38236 KB Output is correct
42 Correct 15 ms 37980 KB Output is correct
43 Correct 17 ms 38616 KB Output is correct
44 Correct 19 ms 37976 KB Output is correct
45 Correct 18 ms 38284 KB Output is correct
46 Correct 17 ms 37980 KB Output is correct
47 Correct 14 ms 37724 KB Output is correct
48 Correct 16 ms 38084 KB Output is correct
49 Correct 17 ms 37976 KB Output is correct
50 Correct 17 ms 37980 KB Output is correct
51 Correct 17 ms 37976 KB Output is correct
52 Correct 16 ms 37980 KB Output is correct
53 Correct 14 ms 37984 KB Output is correct
54 Correct 16 ms 38256 KB Output is correct
55 Correct 13 ms 37724 KB Output is correct
56 Correct 48 ms 48072 KB Output is correct
57 Correct 154 ms 59740 KB Output is correct
58 Correct 17 ms 38236 KB Output is correct
59 Correct 14 ms 37724 KB Output is correct
60 Correct 13 ms 37536 KB Output is correct
61 Correct 156 ms 55056 KB Output is correct
62 Correct 15 ms 37976 KB Output is correct
63 Correct 350 ms 77524 KB Output is correct
64 Correct 166 ms 59012 KB Output is correct
65 Correct 17 ms 37980 KB Output is correct
66 Correct 272 ms 61628 KB Output is correct
67 Correct 16 ms 38364 KB Output is correct
68 Correct 16 ms 38236 KB Output is correct
69 Correct 51 ms 48904 KB Output is correct
70 Correct 17 ms 38616 KB Output is correct
71 Correct 157 ms 61356 KB Output is correct
72 Correct 334 ms 73404 KB Output is correct
73 Correct 14 ms 37784 KB Output is correct
74 Correct 344 ms 83096 KB Output is correct
75 Correct 264 ms 74804 KB Output is correct
76 Correct 14 ms 37980 KB Output is correct
77 Correct 16 ms 38296 KB Output is correct
78 Correct 45 ms 47816 KB Output is correct
79 Correct 49 ms 47300 KB Output is correct
80 Correct 16 ms 37976 KB Output is correct
81 Correct 17 ms 38236 KB Output is correct
82 Correct 14 ms 37724 KB Output is correct
83 Correct 14 ms 37724 KB Output is correct
84 Correct 18 ms 37996 KB Output is correct
85 Correct 187 ms 52564 KB Output is correct
86 Correct 16 ms 37976 KB Output is correct
87 Correct 169 ms 53620 KB Output is correct
88 Correct 13 ms 37724 KB Output is correct
89 Correct 240 ms 53840 KB Output is correct
90 Correct 190 ms 50768 KB Output is correct
91 Correct 145 ms 47120 KB Output is correct
92 Correct 52 ms 48960 KB Output is correct
93 Correct 17 ms 37976 KB Output is correct
94 Correct 14 ms 37724 KB Output is correct
95 Correct 54 ms 48536 KB Output is correct
96 Correct 321 ms 50260 KB Output is correct
97 Correct 16 ms 37976 KB Output is correct
98 Correct 14 ms 37720 KB Output is correct
99 Correct 59 ms 49860 KB Output is correct
100 Correct 17 ms 38112 KB Output is correct
101 Correct 217 ms 50736 KB Output is correct
102 Correct 243 ms 49920 KB Output is correct
103 Correct 15 ms 37980 KB Output is correct
104 Correct 16 ms 37976 KB Output is correct
105 Correct 18 ms 37976 KB Output is correct
106 Correct 16 ms 37980 KB Output is correct
107 Correct 15 ms 37992 KB Output is correct
108 Correct 340 ms 97976 KB Output is correct
109 Execution timed out 3029 ms 212704 KB Time limit exceeded
110 Halted 0 ms 0 KB -