Submission #118498

#TimeUsernameProblemLanguageResultExecution timeMemory
118498raghav0307Mergers (JOI19_mergers)C++14
0 / 100
91 ms35788 KiB
/*raghav0307 - Raghav Gupta*/ #include <bits/stdc++.h> using namespace std; #define ff first #define ss second #define pb push_back #define fast_io() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); typedef long long ll; typedef pair<int, int> pii; typedef long double ld; #define int ll const int MAXN = 5e5 + 5; vector<int> graph[MAXN]; vector<int> state[MAXN]; int depth[MAXN]; int parent[MAXN]; int degree[MAXN]; void dfs(int nd, int p, int lvl){ parent[nd] = p; depth[nd] = lvl; for(auto x: graph[nd]){ if(x == p) continue; dfs(x, nd, lvl+1); } } int color[MAXN]; int find(int x){ return color[x] == x ? x : color[x] = find(color[x]); } void Union(int a, int b){ if(a == b) return; a = find(a); b = find(b); while(a != b){ if(depth[b] > depth[a]) swap(a, b); int x = find(parent[a]); color[a] = x; a = x; } } signed main(){ fast_io(); int n, k; cin >> n >> k; for(int i = 0; i < n-1; i++){ int a, b; cin >> a >> b; graph[a].pb(b); graph[b].pb(a); } for(int i = 1; i <= n; i++){ int indx; cin >> indx; state[indx].pb(i); } dfs(1, 0, 0); for(int i = 1; i <= n; i++) color[i] = i; for(int i = 1; i <= k; i++){ for(int j = 1; j < (int)state[i].size(); j++){ Union(state[i][j], state[i][j-1]); } } for(int i = 1; i <= n; i++){ if(find(i) != i) continue; if(find(parent[i]) == 0) continue; degree[i]++; degree[find(parent[i])]++; } int ans = 0; for(int i = 1; i <= k; i++){ // cout << i << "'s degree " << degree[i] << "\n"; if(degree[i] == 1) ans++; } // cout << ans << "\n"; cout << (ans+1)/2 << "\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...