답안 #1095085

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1095085 2024-10-01T08:46:27 Z pokmui9909 Mergers (JOI19_mergers) C++17
0 / 100
39 ms 33480 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define x first
#define y second

ll N, K, A[500005], D[500005], P[500005];
vector<ll> T[500005];

struct UnionFind{
    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; }
}uf;

void dfs(ll u, ll p){
    P[u] = p;
    for(auto v : T[u]){
        if(v == p) continue;
        D[v] = D[u] + 1;
        dfs(v, u);
    }
}
void Comp(ll u, ll v){
    u = uf.Find(u), v = uf.Find(v);
    while(u != v){
        if(D[u] < D[v]) swap(u, v);
        uf.Union(P[u], u);
        u = uf.Find(u);
    }
}

vector<ll> S[500005];
ll deg[500005];

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

    cin >> N >> K;
    fill(uf.p, uf.p + N + 1, -1LL);
    for(ll i = 1; i < N; i++){
        ll u, v; cin >> u >> v;
        T[u].push_back(v); T[v].push_back(u);
    }
    dfs(1, -1);
    for(ll i = 1; i <= N; i++){
        ll v; cin >> v;
        S[v].push_back(i);
    }
    for(ll i = 1; i <= K; i++){
        for(ll j = 1; j < S[i].size(); i++){
            Comp(S[i][0], S[i][j]);
        }
    }
    for(ll i = 1; i <= N; i++){
        for(auto j : T[i]){
            if(i > j) continue;
            ll u = uf.Find(i), v = uf.Find(j);
            if(u != v) deg[u]++, deg[v]++;
        }
    }
    ll Cnt = 0;
    for(ll i = 1; i <= N; i++){
        if(deg[i] == 1) Cnt++;
    }
    cout << (Cnt + 1) / 2;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:52:25: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |         for(ll j = 1; j < S[i].size(); i++){
      |                       ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 23900 KB Output is correct
2 Incorrect 11 ms 23900 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 23900 KB Output is correct
2 Incorrect 11 ms 23900 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 23900 KB Output is correct
2 Incorrect 11 ms 23900 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 39 ms 33480 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 23900 KB Output is correct
2 Incorrect 11 ms 23900 KB Output isn't correct
3 Halted 0 ms 0 KB -