Submission #1094973

# Submission time Handle Problem Language Result Execution time Memory
1094973 2024-10-01T05:05:06 Z pokmui9909 Mergers (JOI19_mergers) C++17
0 / 100
39 ms 21704 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 chk[500005];

void dfs(ll u, ll p, ll c){
    chk[u] = (A[u] == c);
    for(auto v : T[u]){
        if(v == p || vis[v]) continue;
        dfs(v, u, c);
        if(chk[v]) chk[u] = 1;
    }
    if(chk[u]) Union(A[u], c);
}
void dnc(ll _u){
    ll u = Cent(_u, -1, Size(_u, -1));
    vis[u] = 1;
    for(auto v : T[u]){
        if(vis[v]) continue;
        dfs(v, u, A[u]);
    }
    for(auto v : T[u]){
        if(vis[v]) continue;
        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 Cnt = 0;
    for(ll i = 1; i <= N; i++){
        if(deg[i] == 1) Cnt++;
    }
    if(Cnt == 1){
        cout << 0;
    } else {
        cout << (Cnt + 1) / 2;
    }
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 12124 KB Output is correct
2 Correct 5 ms 12080 KB Output is correct
3 Correct 5 ms 12124 KB Output is correct
4 Incorrect 4 ms 12124 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 12124 KB Output is correct
2 Correct 5 ms 12080 KB Output is correct
3 Correct 5 ms 12124 KB Output is correct
4 Incorrect 4 ms 12124 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 12124 KB Output is correct
2 Correct 5 ms 12080 KB Output is correct
3 Correct 5 ms 12124 KB Output is correct
4 Incorrect 4 ms 12124 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 33 ms 20932 KB Output is correct
2 Correct 39 ms 21704 KB Output is correct
3 Incorrect 6 ms 12380 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 12124 KB Output is correct
2 Correct 5 ms 12080 KB Output is correct
3 Correct 5 ms 12124 KB Output is correct
4 Incorrect 4 ms 12124 KB Output isn't correct
5 Halted 0 ms 0 KB -