# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
210360 | 2020-03-17T07:20:02 Z | arman_ferdous | Mergers (JOI19_mergers) | C++17 | 98 ms | 34912 KB |
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 5e5+10; int n, k, c[N], dsu[N], deg[N]; vector<int> g[N], node[N]; vector<pair<int,int>> E; int lev[N], par[N]; int dfs(int u, int f, int d) { par[u] = f; lev[u] = d; for(int v : g[u]) if(v != f) dfs(v, u, d + 1); } int find(int u) { if(dsu[u] == u) return u; return dsu[u] = find(dsu[u]); } int main() { scanf("%d %d", &n, &k); for(int i = 1; i < n; i++) { int u, v; scanf("%d %d", &u, &v); g[u].push_back(v); g[v].push_back(u); E.emplace_back(u, v); } for(int i = 1; i <= n; i++) { scanf("%d", &c[i]); node[c[i]].push_back(i); } iota(dsu + 1, dsu + 1 + n, 1); dfs(1, 0, 0); for(int i = 1; i <= k; i++) if((int)node[i].size() > 1) { for(int j = 1; j < (int)node[i].size(); j++) { int u = node[i][j - 1], v = node[i][j]; while(u != v) { if(lev[u] > lev[v]) swap(u, v); dsu[v] = find(par[v]); v = dsu[v]; } } } for(auto e : E) { if(find(e.first) != find(e.second)) deg[e.first]++, deg[e.second]++; } int cnt = 0; for(int i = 1; i <= n; i++) cnt += (deg[i] == 1); printf("%d\n", (cnt + 1) >> 1); return 0; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 21 ms | 23800 KB | Output is correct |
2 | Correct | 19 ms | 23800 KB | Output is correct |
3 | Incorrect | 21 ms | 23800 KB | Output isn't correct |
4 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 21 ms | 23800 KB | Output is correct |
2 | Correct | 19 ms | 23800 KB | Output is correct |
3 | Incorrect | 21 ms | 23800 KB | Output isn't correct |
4 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 21 ms | 23800 KB | Output is correct |
2 | Correct | 19 ms | 23800 KB | Output is correct |
3 | Incorrect | 21 ms | 23800 KB | Output isn't correct |
4 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 85 ms | 31716 KB | Output is correct |
2 | Correct | 98 ms | 34912 KB | Output is correct |
3 | Incorrect | 23 ms | 24184 KB | Output isn't correct |
4 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 21 ms | 23800 KB | Output is correct |
2 | Correct | 19 ms | 23800 KB | Output is correct |
3 | Incorrect | 21 ms | 23800 KB | Output isn't correct |
4 | Halted | 0 ms | 0 KB | - |