# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
227198 | 2020-04-26T13:06:46 Z | MKopchev | Mergers (JOI19_mergers) | C++14 | 75 ms | 18960 KB |
#include <bits/stdc++.h> using namespace std; const int nmax=5e5+42; int n,k; int colour[nmax],cnt[nmax]; vector<int> adj[nmax]; int wrong=0; int seen[nmax]; void add(int val) { val=colour[val]; if(seen[val]==0)wrong++; seen[val]++; if(seen[val]==cnt[val])wrong--; } void sub(int val) { val=colour[val]; if(seen[val]==cnt[val])wrong++; seen[val]--; if(seen[val]==0)wrong--; } int SZ[nmax]; void dfs_sz(int node,int par) { SZ[node]=1; for(auto k:adj[node]) if(k!=par) { dfs_sz(k,node); SZ[node]+=SZ[k]; } } int can_divide=0; void dfs_clean(int node,int par) { sub(node); for(auto k:adj[node]) if(k!=par) { dfs_clean(k,node); } } void dfs_add(int node,int par) { add(node); for(auto k:adj[node]) if(k!=par) { dfs_add(k,node); } } int dfs(int node,int par) { bool found=0; int big=0; for(auto k:adj[node]) if(k!=par&&SZ[big]<SZ[k])big=k; for(auto k:adj[node]) if(k!=par&&big!=k) { found=dfs(k,node)|found; dfs_clean(k,node); } /* cout<<node<<" -> "<<big<<" "<<found<<" "<<wrong<<endl; cout<<"seen(should be empty): ";for(int i=1;i<=k;i++)cout<<seen[i]<<" ";cout<<endl; */ if(big)found=found|dfs(big,node); for(auto k:adj[node]) if(k!=par&&big!=k) dfs_add(k,node); add(node); //cout<<node<<" -> "<<big<<" "<<found<<" "<<wrong<<endl; //cout<<"seen: ";for(int i=1;i<=k;i++)cout<<seen[i]<<" ";cout<<endl; if(wrong==0&&found==0&&node!=1) { found=1; can_divide++; } return found; } int main() { scanf("%i%i",&n,&k); int u,v; for(int i=1;i<n;i++) { scanf("%i%i",&u,&v); adj[u].push_back(v); adj[v].push_back(u); } for(int i=1;i<=n;i++) { scanf("%i",&colour[i]); cnt[colour[i]]++; } dfs_sz(1,0); dfs(1,0); printf("%i\n",(can_divide+1)/2); return 0; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 11 ms | 12032 KB | Output is correct |
2 | Correct | 11 ms | 12032 KB | Output is correct |
3 | Correct | 11 ms | 12032 KB | Output is correct |
4 | Correct | 11 ms | 12032 KB | Output is correct |
5 | Correct | 11 ms | 12032 KB | Output is correct |
6 | Correct | 11 ms | 12160 KB | Output is correct |
7 | Correct | 11 ms | 12160 KB | Output is correct |
8 | Correct | 11 ms | 12032 KB | Output is correct |
9 | Correct | 11 ms | 12160 KB | Output is correct |
10 | Correct | 19 ms | 12032 KB | Output is correct |
11 | Correct | 11 ms | 12160 KB | Output is correct |
12 | Correct | 11 ms | 12160 KB | Output is correct |
13 | Correct | 11 ms | 12160 KB | Output is correct |
14 | Correct | 11 ms | 12032 KB | Output is correct |
15 | Correct | 11 ms | 12160 KB | Output is correct |
16 | Incorrect | 11 ms | 12032 KB | Output isn't correct |
17 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 11 ms | 12032 KB | Output is correct |
2 | Correct | 11 ms | 12032 KB | Output is correct |
3 | Correct | 11 ms | 12032 KB | Output is correct |
4 | Correct | 11 ms | 12032 KB | Output is correct |
5 | Correct | 11 ms | 12032 KB | Output is correct |
6 | Correct | 11 ms | 12160 KB | Output is correct |
7 | Correct | 11 ms | 12160 KB | Output is correct |
8 | Correct | 11 ms | 12032 KB | Output is correct |
9 | Correct | 11 ms | 12160 KB | Output is correct |
10 | Correct | 19 ms | 12032 KB | Output is correct |
11 | Correct | 11 ms | 12160 KB | Output is correct |
12 | Correct | 11 ms | 12160 KB | Output is correct |
13 | Correct | 11 ms | 12160 KB | Output is correct |
14 | Correct | 11 ms | 12032 KB | Output is correct |
15 | Correct | 11 ms | 12160 KB | Output is correct |
16 | Incorrect | 11 ms | 12032 KB | Output isn't correct |
17 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 11 ms | 12032 KB | Output is correct |
2 | Correct | 11 ms | 12032 KB | Output is correct |
3 | Correct | 11 ms | 12032 KB | Output is correct |
4 | Correct | 11 ms | 12032 KB | Output is correct |
5 | Correct | 11 ms | 12032 KB | Output is correct |
6 | Correct | 11 ms | 12160 KB | Output is correct |
7 | Correct | 11 ms | 12160 KB | Output is correct |
8 | Correct | 11 ms | 12032 KB | Output is correct |
9 | Correct | 11 ms | 12160 KB | Output is correct |
10 | Correct | 19 ms | 12032 KB | Output is correct |
11 | Correct | 11 ms | 12160 KB | Output is correct |
12 | Correct | 11 ms | 12160 KB | Output is correct |
13 | Correct | 11 ms | 12160 KB | Output is correct |
14 | Correct | 11 ms | 12032 KB | Output is correct |
15 | Correct | 11 ms | 12160 KB | Output is correct |
16 | Incorrect | 11 ms | 12032 KB | Output isn't correct |
17 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 75 ms | 17888 KB | Output is correct |
2 | Incorrect | 73 ms | 18960 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 11 ms | 12032 KB | Output is correct |
2 | Correct | 11 ms | 12032 KB | Output is correct |
3 | Correct | 11 ms | 12032 KB | Output is correct |
4 | Correct | 11 ms | 12032 KB | Output is correct |
5 | Correct | 11 ms | 12032 KB | Output is correct |
6 | Correct | 11 ms | 12160 KB | Output is correct |
7 | Correct | 11 ms | 12160 KB | Output is correct |
8 | Correct | 11 ms | 12032 KB | Output is correct |
9 | Correct | 11 ms | 12160 KB | Output is correct |
10 | Correct | 19 ms | 12032 KB | Output is correct |
11 | Correct | 11 ms | 12160 KB | Output is correct |
12 | Correct | 11 ms | 12160 KB | Output is correct |
13 | Correct | 11 ms | 12160 KB | Output is correct |
14 | Correct | 11 ms | 12032 KB | Output is correct |
15 | Correct | 11 ms | 12160 KB | Output is correct |
16 | Incorrect | 11 ms | 12032 KB | Output isn't correct |
17 | Halted | 0 ms | 0 KB | - |