답안 #1095091

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1095091 2024-10-01T08:56:49 Z pokmui9909 Mergers (JOI19_mergers) C++17
70 / 100
3000 ms 214120 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 14 ms 35676 KB Output is correct
2 Correct 14 ms 35724 KB Output is correct
3 Correct 14 ms 35676 KB Output is correct
4 Correct 15 ms 35736 KB Output is correct
5 Correct 14 ms 35676 KB Output is correct
6 Correct 14 ms 35536 KB Output is correct
7 Correct 14 ms 35592 KB Output is correct
8 Correct 14 ms 35760 KB Output is correct
9 Correct 14 ms 35676 KB Output is correct
10 Correct 13 ms 35668 KB Output is correct
11 Correct 14 ms 35676 KB Output is correct
12 Correct 15 ms 35676 KB Output is correct
13 Correct 16 ms 35672 KB Output is correct
14 Correct 14 ms 35540 KB Output is correct
15 Correct 14 ms 35680 KB Output is correct
16 Correct 15 ms 35676 KB Output is correct
17 Correct 14 ms 35676 KB Output is correct
18 Correct 14 ms 35676 KB Output is correct
19 Correct 14 ms 35548 KB Output is correct
20 Correct 14 ms 35632 KB Output is correct
21 Correct 14 ms 35676 KB Output is correct
22 Correct 14 ms 35660 KB Output is correct
23 Correct 14 ms 35672 KB Output is correct
24 Correct 15 ms 35680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 35676 KB Output is correct
2 Correct 14 ms 35724 KB Output is correct
3 Correct 14 ms 35676 KB Output is correct
4 Correct 15 ms 35736 KB Output is correct
5 Correct 14 ms 35676 KB Output is correct
6 Correct 14 ms 35536 KB Output is correct
7 Correct 14 ms 35592 KB Output is correct
8 Correct 14 ms 35760 KB Output is correct
9 Correct 14 ms 35676 KB Output is correct
10 Correct 13 ms 35668 KB Output is correct
11 Correct 14 ms 35676 KB Output is correct
12 Correct 15 ms 35676 KB Output is correct
13 Correct 16 ms 35672 KB Output is correct
14 Correct 14 ms 35540 KB Output is correct
15 Correct 14 ms 35680 KB Output is correct
16 Correct 15 ms 35676 KB Output is correct
17 Correct 14 ms 35676 KB Output is correct
18 Correct 14 ms 35676 KB Output is correct
19 Correct 14 ms 35548 KB Output is correct
20 Correct 14 ms 35632 KB Output is correct
21 Correct 14 ms 35676 KB Output is correct
22 Correct 14 ms 35660 KB Output is correct
23 Correct 14 ms 35672 KB Output is correct
24 Correct 15 ms 35680 KB Output is correct
25 Correct 14 ms 35676 KB Output is correct
26 Correct 17 ms 36076 KB Output is correct
27 Correct 17 ms 36188 KB Output is correct
28 Correct 17 ms 36080 KB Output is correct
29 Correct 16 ms 35820 KB Output is correct
30 Correct 18 ms 36188 KB Output is correct
31 Correct 16 ms 35676 KB Output is correct
32 Correct 19 ms 36292 KB Output is correct
33 Correct 15 ms 35676 KB Output is correct
34 Correct 16 ms 36068 KB Output is correct
35 Correct 17 ms 35932 KB Output is correct
36 Correct 19 ms 36112 KB Output is correct
37 Correct 20 ms 36188 KB Output is correct
38 Correct 17 ms 35676 KB Output is correct
39 Correct 19 ms 36184 KB Output is correct
40 Correct 19 ms 36572 KB Output is correct
41 Correct 18 ms 36188 KB Output is correct
42 Correct 18 ms 36184 KB Output is correct
43 Correct 19 ms 36568 KB Output is correct
44 Correct 15 ms 35736 KB Output is correct
45 Correct 19 ms 36180 KB Output is correct
46 Correct 19 ms 35932 KB Output is correct
47 Correct 23 ms 35720 KB Output is correct
48 Correct 17 ms 35932 KB Output is correct
49 Correct 15 ms 35932 KB Output is correct
50 Correct 18 ms 35932 KB Output is correct
51 Correct 17 ms 35972 KB Output is correct
52 Correct 17 ms 35932 KB Output is correct
53 Correct 17 ms 35932 KB Output is correct
54 Correct 16 ms 36188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 35676 KB Output is correct
2 Correct 14 ms 35724 KB Output is correct
3 Correct 14 ms 35676 KB Output is correct
4 Correct 15 ms 35736 KB Output is correct
5 Correct 14 ms 35676 KB Output is correct
6 Correct 14 ms 35536 KB Output is correct
7 Correct 14 ms 35592 KB Output is correct
8 Correct 14 ms 35760 KB Output is correct
9 Correct 14 ms 35676 KB Output is correct
10 Correct 13 ms 35668 KB Output is correct
11 Correct 14 ms 35676 KB Output is correct
12 Correct 15 ms 35676 KB Output is correct
13 Correct 16 ms 35672 KB Output is correct
14 Correct 14 ms 35540 KB Output is correct
15 Correct 14 ms 35680 KB Output is correct
16 Correct 15 ms 35676 KB Output is correct
17 Correct 14 ms 35676 KB Output is correct
18 Correct 14 ms 35676 KB Output is correct
19 Correct 14 ms 35548 KB Output is correct
20 Correct 14 ms 35632 KB Output is correct
21 Correct 14 ms 35676 KB Output is correct
22 Correct 14 ms 35660 KB Output is correct
23 Correct 14 ms 35672 KB Output is correct
24 Correct 15 ms 35680 KB Output is correct
25 Correct 16 ms 35672 KB Output is correct
26 Correct 51 ms 46232 KB Output is correct
27 Correct 170 ms 58048 KB Output is correct
28 Correct 18 ms 36188 KB Output is correct
29 Correct 16 ms 35672 KB Output is correct
30 Correct 16 ms 35572 KB Output is correct
31 Correct 169 ms 53392 KB Output is correct
32 Correct 15 ms 35932 KB Output is correct
33 Correct 385 ms 76056 KB Output is correct
34 Correct 181 ms 57212 KB Output is correct
35 Correct 21 ms 36188 KB Output is correct
36 Correct 303 ms 60056 KB Output is correct
37 Correct 16 ms 36572 KB Output is correct
38 Correct 16 ms 36184 KB Output is correct
39 Correct 50 ms 47420 KB Output is correct
40 Correct 18 ms 36568 KB Output is correct
41 Correct 155 ms 59576 KB Output is correct
42 Correct 336 ms 71816 KB Output is correct
43 Correct 15 ms 35544 KB Output is correct
44 Correct 367 ms 81336 KB Output is correct
45 Correct 271 ms 73100 KB Output is correct
46 Correct 16 ms 35928 KB Output is correct
47 Correct 15 ms 36048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 46276 KB Output is correct
2 Correct 51 ms 45928 KB Output is correct
3 Correct 15 ms 35932 KB Output is correct
4 Correct 26 ms 36148 KB Output is correct
5 Correct 13 ms 35700 KB Output is correct
6 Correct 13 ms 35676 KB Output is correct
7 Correct 13 ms 35932 KB Output is correct
8 Correct 203 ms 51076 KB Output is correct
9 Correct 18 ms 36184 KB Output is correct
10 Correct 193 ms 52188 KB Output is correct
11 Correct 14 ms 35672 KB Output is correct
12 Correct 277 ms 52120 KB Output is correct
13 Correct 236 ms 49408 KB Output is correct
14 Correct 165 ms 45644 KB Output is correct
15 Correct 74 ms 47360 KB Output is correct
16 Correct 18 ms 36056 KB Output is correct
17 Correct 19 ms 35612 KB Output is correct
18 Correct 61 ms 47284 KB Output is correct
19 Correct 349 ms 49128 KB Output is correct
20 Correct 19 ms 35928 KB Output is correct
21 Correct 24 ms 35600 KB Output is correct
22 Correct 64 ms 48444 KB Output is correct
23 Correct 22 ms 35972 KB Output is correct
24 Correct 240 ms 49360 KB Output is correct
25 Correct 255 ms 48464 KB Output is correct
26 Correct 17 ms 35932 KB Output is correct
27 Correct 20 ms 35972 KB Output is correct
28 Correct 16 ms 36008 KB Output is correct
29 Correct 27 ms 36000 KB Output is correct
30 Correct 18 ms 35932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 35676 KB Output is correct
2 Correct 14 ms 35724 KB Output is correct
3 Correct 14 ms 35676 KB Output is correct
4 Correct 15 ms 35736 KB Output is correct
5 Correct 14 ms 35676 KB Output is correct
6 Correct 14 ms 35536 KB Output is correct
7 Correct 14 ms 35592 KB Output is correct
8 Correct 14 ms 35760 KB Output is correct
9 Correct 14 ms 35676 KB Output is correct
10 Correct 13 ms 35668 KB Output is correct
11 Correct 14 ms 35676 KB Output is correct
12 Correct 15 ms 35676 KB Output is correct
13 Correct 16 ms 35672 KB Output is correct
14 Correct 14 ms 35540 KB Output is correct
15 Correct 14 ms 35680 KB Output is correct
16 Correct 15 ms 35676 KB Output is correct
17 Correct 14 ms 35676 KB Output is correct
18 Correct 14 ms 35676 KB Output is correct
19 Correct 14 ms 35548 KB Output is correct
20 Correct 14 ms 35632 KB Output is correct
21 Correct 14 ms 35676 KB Output is correct
22 Correct 14 ms 35660 KB Output is correct
23 Correct 14 ms 35672 KB Output is correct
24 Correct 15 ms 35680 KB Output is correct
25 Correct 14 ms 35676 KB Output is correct
26 Correct 17 ms 36076 KB Output is correct
27 Correct 17 ms 36188 KB Output is correct
28 Correct 17 ms 36080 KB Output is correct
29 Correct 16 ms 35820 KB Output is correct
30 Correct 18 ms 36188 KB Output is correct
31 Correct 16 ms 35676 KB Output is correct
32 Correct 19 ms 36292 KB Output is correct
33 Correct 15 ms 35676 KB Output is correct
34 Correct 16 ms 36068 KB Output is correct
35 Correct 17 ms 35932 KB Output is correct
36 Correct 19 ms 36112 KB Output is correct
37 Correct 20 ms 36188 KB Output is correct
38 Correct 17 ms 35676 KB Output is correct
39 Correct 19 ms 36184 KB Output is correct
40 Correct 19 ms 36572 KB Output is correct
41 Correct 18 ms 36188 KB Output is correct
42 Correct 18 ms 36184 KB Output is correct
43 Correct 19 ms 36568 KB Output is correct
44 Correct 15 ms 35736 KB Output is correct
45 Correct 19 ms 36180 KB Output is correct
46 Correct 19 ms 35932 KB Output is correct
47 Correct 23 ms 35720 KB Output is correct
48 Correct 17 ms 35932 KB Output is correct
49 Correct 15 ms 35932 KB Output is correct
50 Correct 18 ms 35932 KB Output is correct
51 Correct 17 ms 35972 KB Output is correct
52 Correct 17 ms 35932 KB Output is correct
53 Correct 17 ms 35932 KB Output is correct
54 Correct 16 ms 36188 KB Output is correct
55 Correct 16 ms 35672 KB Output is correct
56 Correct 51 ms 46232 KB Output is correct
57 Correct 170 ms 58048 KB Output is correct
58 Correct 18 ms 36188 KB Output is correct
59 Correct 16 ms 35672 KB Output is correct
60 Correct 16 ms 35572 KB Output is correct
61 Correct 169 ms 53392 KB Output is correct
62 Correct 15 ms 35932 KB Output is correct
63 Correct 385 ms 76056 KB Output is correct
64 Correct 181 ms 57212 KB Output is correct
65 Correct 21 ms 36188 KB Output is correct
66 Correct 303 ms 60056 KB Output is correct
67 Correct 16 ms 36572 KB Output is correct
68 Correct 16 ms 36184 KB Output is correct
69 Correct 50 ms 47420 KB Output is correct
70 Correct 18 ms 36568 KB Output is correct
71 Correct 155 ms 59576 KB Output is correct
72 Correct 336 ms 71816 KB Output is correct
73 Correct 15 ms 35544 KB Output is correct
74 Correct 367 ms 81336 KB Output is correct
75 Correct 271 ms 73100 KB Output is correct
76 Correct 16 ms 35928 KB Output is correct
77 Correct 15 ms 36048 KB Output is correct
78 Correct 47 ms 46276 KB Output is correct
79 Correct 51 ms 45928 KB Output is correct
80 Correct 15 ms 35932 KB Output is correct
81 Correct 26 ms 36148 KB Output is correct
82 Correct 13 ms 35700 KB Output is correct
83 Correct 13 ms 35676 KB Output is correct
84 Correct 13 ms 35932 KB Output is correct
85 Correct 203 ms 51076 KB Output is correct
86 Correct 18 ms 36184 KB Output is correct
87 Correct 193 ms 52188 KB Output is correct
88 Correct 14 ms 35672 KB Output is correct
89 Correct 277 ms 52120 KB Output is correct
90 Correct 236 ms 49408 KB Output is correct
91 Correct 165 ms 45644 KB Output is correct
92 Correct 74 ms 47360 KB Output is correct
93 Correct 18 ms 36056 KB Output is correct
94 Correct 19 ms 35612 KB Output is correct
95 Correct 61 ms 47284 KB Output is correct
96 Correct 349 ms 49128 KB Output is correct
97 Correct 19 ms 35928 KB Output is correct
98 Correct 24 ms 35600 KB Output is correct
99 Correct 64 ms 48444 KB Output is correct
100 Correct 22 ms 35972 KB Output is correct
101 Correct 240 ms 49360 KB Output is correct
102 Correct 255 ms 48464 KB Output is correct
103 Correct 17 ms 35932 KB Output is correct
104 Correct 20 ms 35972 KB Output is correct
105 Correct 16 ms 36008 KB Output is correct
106 Correct 27 ms 36000 KB Output is correct
107 Correct 18 ms 35932 KB Output is correct
108 Correct 471 ms 100700 KB Output is correct
109 Execution timed out 3080 ms 214120 KB Time limit exceeded
110 Halted 0 ms 0 KB -