# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
262016 | 2020-08-12T09:32:35 Z | patrikpavic2 | Mergers (JOI19_mergers) | C++17 | 87 ms | 34544 KB |
#include <cstdio> #include <cstdlib> #include <vector> #define PB push_back using namespace std; const int N = 5e5 + 500; vector < int > v[N], v2[N]; int A[N], kol[N], siz[N], sol, bio[N], bla, n; int hsh[N], par[N], zad[N]; inline int pr(int x){ if(x == par[x]) return x; return par[x] = pr(par[x]); } inline void mrg(int x, int y){ par[pr(x)] = pr(y); } int dfs(int x, int lst){ int ret = hsh[x]; for(int y : v[x]){ if(y == lst) continue; ret += dfs(y, x); } //printf("x = %d ret = %d\n", x, ret); if(ret != 0) mrg(x, lst); return ret; } int f(int x, int lst){ if((int)v2[x].size() == 1) return 1; if(x == lst){ int dos = 0; for(int y : v2[x]){ int nov = f(y, x); if(dos < nov) {nov ^= dos, dos ^= nov, nov ^= dos;} sol += nov, dos -= nov; } sol += dos; return 0; } int ret = 0; for(int y : v2[x]) if(y != lst) ret += f(y, x); if(ret % 1){ sol += ret / 2; ret = 1; } else{ sol += (ret - 2) / 2; ret = 2; } return ret; } int main(){ scanf("%d%d", &n, &bla); for(int i = 1;i < n;i++){ int x, y; scanf("%d%d", &x, &y); v[x].PB(y), v[y].PB(x); } for(int i = 1;i <= n;i++){ scanf("%d", A + i); zad[A[i]] = i; par[i] = i; } for(int i = 1;i <= n;i++){ if(zad[A[i]] != i) hsh[i] = rand() * rand(); else hsh[i] = -kol[A[i]]; kol[A[i]] += hsh[i]; } dfs(1, 1); for(int i = 1;i <= n;i++){ for(int x : v[i]) if(pr(x) != pr(i)) v2[pr(i)].PB(pr(x)); } if((int)v2[pr(1)].size() == 0){ printf("0\n"); return 0; } int root = -1; for(int i = 1;i <= n;i++) if(pr(i) == i && (int)v2[i].size() != 1) root = i; if(root == -1){ printf("1\n"); return 0; } f(root, root); printf("%d\n", sol); return 0; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 17 ms | 23808 KB | Output is correct |
2 | Correct | 15 ms | 23808 KB | Output is correct |
3 | Correct | 15 ms | 23936 KB | Output is correct |
4 | Correct | 15 ms | 23808 KB | Output is correct |
5 | Correct | 15 ms | 23808 KB | Output is correct |
6 | Correct | 15 ms | 23808 KB | Output is correct |
7 | Correct | 15 ms | 23808 KB | Output is correct |
8 | Correct | 16 ms | 23808 KB | Output is correct |
9 | Incorrect | 15 ms | 23808 KB | Output isn't correct |
10 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 17 ms | 23808 KB | Output is correct |
2 | Correct | 15 ms | 23808 KB | Output is correct |
3 | Correct | 15 ms | 23936 KB | Output is correct |
4 | Correct | 15 ms | 23808 KB | Output is correct |
5 | Correct | 15 ms | 23808 KB | Output is correct |
6 | Correct | 15 ms | 23808 KB | Output is correct |
7 | Correct | 15 ms | 23808 KB | Output is correct |
8 | Correct | 16 ms | 23808 KB | Output is correct |
9 | Incorrect | 15 ms | 23808 KB | Output isn't correct |
10 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 17 ms | 23808 KB | Output is correct |
2 | Correct | 15 ms | 23808 KB | Output is correct |
3 | Correct | 15 ms | 23936 KB | Output is correct |
4 | Correct | 15 ms | 23808 KB | Output is correct |
5 | Correct | 15 ms | 23808 KB | Output is correct |
6 | Correct | 15 ms | 23808 KB | Output is correct |
7 | Correct | 15 ms | 23808 KB | Output is correct |
8 | Correct | 16 ms | 23808 KB | Output is correct |
9 | Incorrect | 15 ms | 23808 KB | Output isn't correct |
10 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 86 ms | 29936 KB | Output is correct |
2 | Correct | 87 ms | 34544 KB | Output is correct |
3 | Incorrect | 21 ms | 24064 KB | Output isn't correct |
4 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 17 ms | 23808 KB | Output is correct |
2 | Correct | 15 ms | 23808 KB | Output is correct |
3 | Correct | 15 ms | 23936 KB | Output is correct |
4 | Correct | 15 ms | 23808 KB | Output is correct |
5 | Correct | 15 ms | 23808 KB | Output is correct |
6 | Correct | 15 ms | 23808 KB | Output is correct |
7 | Correct | 15 ms | 23808 KB | Output is correct |
8 | Correct | 16 ms | 23808 KB | Output is correct |
9 | Incorrect | 15 ms | 23808 KB | Output isn't correct |
10 | Halted | 0 ms | 0 KB | - |