# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
569978 | 2022-05-28T12:14:14 Z | Doxeno | Mergers (JOI19_mergers) | C++17 | 133 ms | 39944 KB |
#include <bits/stdc++.h> using namespace std; const int MAXN = 500010; vector<int> adj[MAXN]; vector<int> col[MAXN]; int c[MAXN]; int d[MAXN]; int st[MAXN][20]; bool vis[MAXN]; int mc[MAXN]; int og[MAXN]; int ans; void dd(int n){ vis[n] = 1; for(auto x: adj[n]){ if(vis[x])continue; st[x][0] = n; d[x] = d[n]+1; dd(x); } for(int i = 1; i < 20; i++){ st[n][i] = st[st[n][i-1]][i-1]; } } int lca(int a, int b){ if(d[a] < d[b])swap(a,b); for(int i = 0; i < 20; i++){ if((d[a]-d[b])&(1<<i))a = st[a][i]; } for(int i = 19; i >= 0; i--){ if(st[a][i] != st[b][i]){ a = st[a][i]; b = st[b][i]; } } return st[a][0]; } int dfs(int n){ vis[n] = 1; int k = 0; og[n] = mc[c[n]]; for(auto x: adj[n]){ if(vis[x])continue; k+=dfs(x); og[n] = min(og[n],og[x]); } if(og[n] == d[n] && k == 0 && (n!=0))k = 1; return k; //ans+=k; //return(((n!=0) && (og[n] == d[n])) || (k&1)); } int main(){ int N,K,a,b; cin >> N >> K; for(int i = 0; i < N-1; i++){ cin >> a >> b; adj[--a].push_back(--b); adj[b].push_back(a); } for(int i = 0; i < N; i++){ cin >> c[i]; c[i]--; col[c[i]].push_back(i); } dd(0); for(int i = 0; i < N; i++)vis[i] = 0; for(int i = 0; i < K; i++){ if(col[i].empty())continue; mc[i] = col[i][0]; for(int j = 1; j < col[i].size(); j++){ mc[i] = lca(mc[i],col[i][j]); } mc[i] = d[mc[i]]; } cout << (1+dfs(0))/2 << endl; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 13 ms | 23764 KB | Output is correct |
2 | Incorrect | 12 ms | 23764 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 13 ms | 23764 KB | Output is correct |
2 | Incorrect | 12 ms | 23764 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 13 ms | 23764 KB | Output is correct |
2 | Incorrect | 12 ms | 23764 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 131 ms | 36928 KB | Output is correct |
2 | Incorrect | 133 ms | 39944 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 13 ms | 23764 KB | Output is correct |
2 | Incorrect | 12 ms | 23764 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |