# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
109762 | 2019-05-08T03:25:09 Z | luciocf | Mergers (JOI19_mergers) | C++14 | 3000 ms | 15244 KB |
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5+10; int n, k; int pai[maxn], peso[maxn]; int s[maxn]; int par[maxn], grau[maxn]; bool mark[maxn]; vector<int> grafo[maxn], state[maxn]; void init(void) { for (int i = 1; i <= n; i++) pai[i] = i, peso[i] = 1; } int Find(int x) { if (pai[x] == x) return x; return pai[x] = Find(pai[x]); } void Join(int x, int y) { x = Find(x), y = Find(y); if (x == y) return; if (peso[x] < peso[y]) swap(x, y); pai[y] = x, peso[x] += peso[y]; } void dfs(int u, int p) { for (auto v: grafo[u]) { if (v == p) continue; par[v] = u; dfs(v, u); } } int main(void) { scanf("%d %d", &n, &k); for (int i = 1; i < n; i++) { int u, v; scanf("%d %d", &u, &v); grafo[u].push_back(v); grafo[v].push_back(u); } for (int i = 1; i <= n; i++) { scanf("%d", &s[i]); state[s[i]].push_back(i); } init(); for (int i = 1; i <= n; i++) { if (mark[s[i]]) continue; mark[s[i]] = 1; par[i] = 0; dfs(i, 0); for (auto v: state[s[i]]) { while (v != i) { Join(i, v); v = par[v]; } } } for (int i = 1; i <= n; i++) for (auto v: grafo[i]) if (Find(v) != Find(i)) grau[v]++; int leaf = 0; for (int i = 1; i <= n; i++) if (grau[i] == 1) leaf++; printf("%d\n", (leaf+1)/2); }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 7 ms | 5120 KB | Output is correct |
2 | Correct | 6 ms | 5120 KB | Output is correct |
3 | Incorrect | 5 ms | 5120 KB | Output isn't correct |
4 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 7 ms | 5120 KB | Output is correct |
2 | Correct | 6 ms | 5120 KB | Output is correct |
3 | Incorrect | 5 ms | 5120 KB | Output isn't correct |
4 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 7 ms | 5120 KB | Output is correct |
2 | Correct | 6 ms | 5120 KB | Output is correct |
3 | Incorrect | 5 ms | 5120 KB | Output isn't correct |
4 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 206 ms | 12148 KB | Output is correct |
2 | Execution timed out | 3086 ms | 15244 KB | Time limit exceeded |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 7 ms | 5120 KB | Output is correct |
2 | Correct | 6 ms | 5120 KB | Output is correct |
3 | Incorrect | 5 ms | 5120 KB | Output isn't correct |
4 | Halted | 0 ms | 0 KB | - |