#include "catdog.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int len = 1e5+5;
int par[len], dep[len], dif[len], col[len];
int ans, n;
vector<int> adj[len];
void fix(int u){
for (int j = 0; j < adj[u].size(); j++){
int v = adj[u][j];
if (v == par[u])
continue;
dep[v] = dep[u]+1;
par[v] = u;
fix(v);
}
}
void upd(int u, int v, int x){
while (dep[u] >= dep[v])
dif[u] += x, u = par[u];
}
int fin(int u, int l, int r){
int ans = 0;
while (u != 0 && col[u] == 0 && l <= dif[u] && dif[u] <= r)
ans = u, u = par[u];
return ans;
}
void change(int u, int t){
/*
3: red-green
2: white-green
1: red-white
0: nothing
-1: green-white
-2: white-red
-3: green-red
*/
if (u == 0)
return;
//printf("change: u = %d, t = %d\n", u, t);
if (t == 3){
int v = fin(u, -1, -1);
if (v != 0){
upd(u, v, 2);
v = par[v];
if (v == 0) return;
}
else
v = u;
if (col[v] == 1)
ans--;
else if (col[v] == -1)
ans++;
else if (dif[v] <= -3)
ans++;
else if (dif[v] == -2)
ans++, change(par[v], 1);
else if (dif[v] == 0)
ans--, change(par[v], 2);
else if (dif[v] >= 1)
ans--;
upd(v, v, 2);
}
else if (t == 2 || t == 1){
int v = fin(u, -1, 0), c = t;
//printf("v = %d\n", v);
if (v != 0){
if (t == 2 && dif[v] == -1)
ans++, c = 1;
if (t == 1 && dif[v] == 0)
ans--, c = 2;
upd(u, v, 1);
v = par[v];
if (v == 0) return;
}
else
v = u;
if (c == 1){
if (col[v] == 1)
ans--;
else if (col[v] == -1)
ans += 0;
else if (dif[v] <= -2)
ans += 0;
else if (dif[v] >= 1)
ans--;
}
else{
if (col[v] == 1)
ans += 0;
else if (col[v] == -1)
ans++;
else if (dif[v] <= -2)
ans++;
else if (dif[v] >= 1)
ans += 0;
}
upd(v, v, 1);
}
else if (t == -1 || t == -2){
int v = fin(u, 0, 1), c = t;
//printf("v = %d\n", v);
if (v != 0){
if (t == -1 && dif[v] == 0)
ans--, c = -2;
if (t == -2 && dif[v] == 1)
ans++, c = -1;
upd(u, v, -1);
v = par[v];
if (v == 0) return;
}
else
v = u;
if (c == -1){
if (col[v] == 1)
ans += 0;
else if (col[v] == -1)
ans--;
else if (dif[v] >= 2)
ans += 0;
else if (dif[v] <= -1)
ans--;
}
else{
if (col[v] == 1)
ans++;
else if (col[v] == -1)
ans += 0;
else if (dif[v] >= 2)
ans++;
else if (dif[v] <= -1)
ans += 0;
}
upd(v, v, -1);
}
else if (t == -3){
int v = fin(u, 1, 1);
//printf("v = %d\n", v);
if (v != 0){
upd(u, v, -2);
v = par[v];
if (v == 0) return;
}
else
v = u;
if (col[v] == 1)
ans++;
else if (col[v] == -1)
ans--;
else if (dif[v] >= 3)
ans++;
else if (dif[v] == 2)
ans++, change(par[v], -1);
else if (dif[v] == 0)
ans--, change(par[v], -2);
else if (dif[v] <= -1)
ans--;
upd(v, v, -2);
}
}
void print(){
for (int i = 1; i <= n; i++)
printf("i = %d, dif = %d\n", i, dif[i]);
printf("\n");
}
void initialize(int N, vector<int> A, vector<int> B){
n = N;
for (int i = 0; i < n-1; i++){
int a = A[i], b = B[i];
adj[a].pb(b);
adj[b].pb(a);
}
fix(1), dep[0] = -1;
}
int cat(int u){
col[u] = -1;
if (dif[u] > 0)
ans += dif[u], change(par[u], -3);
else if (dif[u] == 0)
change(par[u], -2);
//print();
return ans;
}
int dog(int u){
col[u] = 1;
if (dif[u] < 0)
ans -= dif[u], change(par[u], 3);
else if (dif[u] == 0)
change(par[u], 2);
//print();
return ans;
}
int neighbor(int u){
if (col[u] == 1){
if (dif[u] < 0)
ans += dif[u], change(par[u], -3);
else if (dif[u] == 0)
change(par[u], -1);
}
else{
if (dif[u] > 0)
ans -= dif[u], change(par[u], 3);
else if (dif[u] == 0)
change(par[u], 1);
}
col[u] = 0;
//print();
return ans;
}
/*
test cases:
5
1 2
2 3
2 4
1 5
8
1 3
2 4
2 5
3 3
3 4
1 4
1 1
2 2
*/
Compilation message
catdog.cpp: In function 'void fix(int)':
catdog.cpp:13:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < adj[u].size(); j++){
~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Correct |
4 ms |
2684 KB |
Output is correct |
6 |
Correct |
4 ms |
2680 KB |
Output is correct |
7 |
Correct |
4 ms |
2680 KB |
Output is correct |
8 |
Correct |
4 ms |
2680 KB |
Output is correct |
9 |
Correct |
4 ms |
2680 KB |
Output is correct |
10 |
Correct |
4 ms |
2680 KB |
Output is correct |
11 |
Correct |
4 ms |
2680 KB |
Output is correct |
12 |
Correct |
4 ms |
2680 KB |
Output is correct |
13 |
Correct |
4 ms |
2680 KB |
Output is correct |
14 |
Correct |
4 ms |
2680 KB |
Output is correct |
15 |
Correct |
4 ms |
2684 KB |
Output is correct |
16 |
Correct |
4 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Correct |
4 ms |
2684 KB |
Output is correct |
6 |
Correct |
4 ms |
2680 KB |
Output is correct |
7 |
Correct |
4 ms |
2680 KB |
Output is correct |
8 |
Correct |
4 ms |
2680 KB |
Output is correct |
9 |
Correct |
4 ms |
2680 KB |
Output is correct |
10 |
Correct |
4 ms |
2680 KB |
Output is correct |
11 |
Correct |
4 ms |
2680 KB |
Output is correct |
12 |
Correct |
4 ms |
2680 KB |
Output is correct |
13 |
Correct |
4 ms |
2680 KB |
Output is correct |
14 |
Correct |
4 ms |
2680 KB |
Output is correct |
15 |
Correct |
4 ms |
2684 KB |
Output is correct |
16 |
Correct |
4 ms |
2652 KB |
Output is correct |
17 |
Correct |
5 ms |
2808 KB |
Output is correct |
18 |
Correct |
5 ms |
2808 KB |
Output is correct |
19 |
Correct |
5 ms |
2808 KB |
Output is correct |
20 |
Correct |
4 ms |
2680 KB |
Output is correct |
21 |
Correct |
4 ms |
2680 KB |
Output is correct |
22 |
Correct |
4 ms |
2680 KB |
Output is correct |
23 |
Correct |
5 ms |
2680 KB |
Output is correct |
24 |
Correct |
9 ms |
2808 KB |
Output is correct |
25 |
Correct |
5 ms |
2680 KB |
Output is correct |
26 |
Correct |
5 ms |
2680 KB |
Output is correct |
27 |
Correct |
4 ms |
2680 KB |
Output is correct |
28 |
Correct |
4 ms |
2716 KB |
Output is correct |
29 |
Correct |
5 ms |
2808 KB |
Output is correct |
30 |
Correct |
4 ms |
2680 KB |
Output is correct |
31 |
Correct |
4 ms |
2680 KB |
Output is correct |
32 |
Correct |
4 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Correct |
4 ms |
2684 KB |
Output is correct |
6 |
Correct |
4 ms |
2680 KB |
Output is correct |
7 |
Correct |
4 ms |
2680 KB |
Output is correct |
8 |
Correct |
4 ms |
2680 KB |
Output is correct |
9 |
Correct |
4 ms |
2680 KB |
Output is correct |
10 |
Correct |
4 ms |
2680 KB |
Output is correct |
11 |
Correct |
4 ms |
2680 KB |
Output is correct |
12 |
Correct |
4 ms |
2680 KB |
Output is correct |
13 |
Correct |
4 ms |
2680 KB |
Output is correct |
14 |
Correct |
4 ms |
2680 KB |
Output is correct |
15 |
Correct |
4 ms |
2684 KB |
Output is correct |
16 |
Correct |
4 ms |
2652 KB |
Output is correct |
17 |
Correct |
5 ms |
2808 KB |
Output is correct |
18 |
Correct |
5 ms |
2808 KB |
Output is correct |
19 |
Correct |
5 ms |
2808 KB |
Output is correct |
20 |
Correct |
4 ms |
2680 KB |
Output is correct |
21 |
Correct |
4 ms |
2680 KB |
Output is correct |
22 |
Correct |
4 ms |
2680 KB |
Output is correct |
23 |
Correct |
5 ms |
2680 KB |
Output is correct |
24 |
Correct |
9 ms |
2808 KB |
Output is correct |
25 |
Correct |
5 ms |
2680 KB |
Output is correct |
26 |
Correct |
5 ms |
2680 KB |
Output is correct |
27 |
Correct |
4 ms |
2680 KB |
Output is correct |
28 |
Correct |
4 ms |
2716 KB |
Output is correct |
29 |
Correct |
5 ms |
2808 KB |
Output is correct |
30 |
Correct |
4 ms |
2680 KB |
Output is correct |
31 |
Correct |
4 ms |
2680 KB |
Output is correct |
32 |
Correct |
4 ms |
2652 KB |
Output is correct |
33 |
Correct |
65 ms |
7784 KB |
Output is correct |
34 |
Correct |
49 ms |
7384 KB |
Output is correct |
35 |
Correct |
58 ms |
6960 KB |
Output is correct |
36 |
Correct |
103 ms |
11136 KB |
Output is correct |
37 |
Correct |
21 ms |
4984 KB |
Output is correct |
38 |
Correct |
117 ms |
11848 KB |
Output is correct |
39 |
Correct |
131 ms |
12004 KB |
Output is correct |
40 |
Correct |
112 ms |
11880 KB |
Output is correct |
41 |
Correct |
111 ms |
11852 KB |
Output is correct |
42 |
Correct |
114 ms |
11876 KB |
Output is correct |
43 |
Correct |
111 ms |
11884 KB |
Output is correct |
44 |
Correct |
112 ms |
11888 KB |
Output is correct |
45 |
Correct |
116 ms |
11880 KB |
Output is correct |
46 |
Correct |
114 ms |
11944 KB |
Output is correct |
47 |
Correct |
110 ms |
12004 KB |
Output is correct |
48 |
Correct |
72 ms |
9152 KB |
Output is correct |
49 |
Correct |
80 ms |
10400 KB |
Output is correct |
50 |
Correct |
27 ms |
4728 KB |
Output is correct |
51 |
Correct |
31 ms |
5852 KB |
Output is correct |
52 |
Correct |
16 ms |
4344 KB |
Output is correct |
53 |
Correct |
124 ms |
10220 KB |
Output is correct |
54 |
Correct |
45 ms |
6320 KB |
Output is correct |
55 |
Correct |
89 ms |
9580 KB |
Output is correct |
56 |
Correct |
60 ms |
7172 KB |
Output is correct |
57 |
Correct |
130 ms |
10400 KB |
Output is correct |
58 |
Correct |
25 ms |
5748 KB |
Output is correct |
59 |
Correct |
30 ms |
5624 KB |
Output is correct |
60 |
Correct |
67 ms |
9768 KB |
Output is correct |
61 |
Correct |
72 ms |
10208 KB |
Output is correct |
62 |
Correct |
51 ms |
8272 KB |
Output is correct |
63 |
Execution timed out |
3025 ms |
8056 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |