#include "catdog.h"
#include <algorithm>
using namespace std;
int N;
vector<int> G[100100]; int P[100100];
const int inf = 1000000000;
struct mat{
mat(long long a, long long b)
{
if (a > inf) a = inf;
if (b > inf) b = inf;
A[0][0] = a; A[0][1] = a+1;
A[1][0] = b+1; A[1][1] = b;
}
mat()
{
A[0][0] = 0; A[0][1] = 1;
A[1][0] = 1; A[1][1] = 0;
}
int A[2][2];
mat operator *(const mat &t) const{
mat r(0,0);
for (int i=0;i<2;i++) for (int j=0;j<2;j++){
r.A[i][j] = inf;
for (int k=0;k<2;k++){
r.A[i][j] = min(r.A[i][j],A[i][k]+t.A[k][j]);
}
}
return r;
};
};
vector<int> see;
int go(int x, int l)
{
see.push_back(x);
P[x] = l;
auto I = find(G[x].begin(),G[x].end(),l);
if (I != G[x].end()) G[x].erase(I);
int sz = 1, mx = 0, v;
for (auto &y : G[x]){
int z = go(y,x);
sz += z;
if (mx < z)
mx = z, v = y;
}
if (mx){
auto J = find(G[x].begin(),G[x].end(),v);
swap(G[x][0],*J);
}
return sz;
}
int C,num[100100],pos[100100],sz[100100],pw[100100],la[100100],lb[100100];
vector<long long> chain[100100],addA[100100],addB[100100];
vector<mat> IT[100100];
void initialize(int N_, std::vector<int> A, std::vector<int> B) {
N = N_;
for (int i=0;i<A.size();i++){
int x = A[i], y = B[i];
G[x].push_back(y);
G[y].push_back(x);
}
go(1,0);
C++;
for (int &i : see){
if (G[i].empty()) continue;
int x = G[i][0];
num[x] = num[i];
for (int j=1;j<G[i].size();j++){
int y = G[i][j];
num[y] = C++;
}
}
for (int &i : see){
pos[i] = sz[num[i]]++;
chain[num[i]].push_back(i);
}
for (int i=0;i<C;i++){
addA[i].resize(sz[i]);
addB[i].resize(sz[i]);
pw[i] = 1;
while (pw[i] < sz[i]) pw[i] *= 2;
IT[i].resize(pw[i]*2);
}
}
void up(vector<mat> &it, int Z, int x, mat t)
{
x += Z;
it[x] = t; x /= 2;
while (x){
it[x] = it[x*2] * it[x*2+1];
x /= 2;
}
}
void push(int x, int a, int b)
{
bool f = true;
while (x){
int n = num[x];
int v = chain[n][0];
int p = P[v];
if (p){
int A = min(IT[n][1].A[0][0],IT[n][1].A[0][1]);
int B = min(IT[n][1].A[1][0],IT[n][1].A[1][1]);
int np = num[p];
addA[np][pos[p]] -= min(A,B+1);
addB[np][pos[p]] -= min(A+1,B);
}
int ps = pos[x];
if (f){
addA[n][ps] -= la[x];
addB[n][ps] -= lb[x];
la[x] = a;
lb[x] = b;
addA[n][ps] += a;
addB[n][ps] += b;
f = 0;
}
up(IT[n], pw[n], ps, mat(addA[n][ps],addB[n][ps]));
if (p){
int A = min(IT[n][1].A[0][0],IT[n][1].A[0][1]);
int B = min(IT[n][1].A[1][0],IT[n][1].A[1][1]);
int np = num[p];
addA[np][pos[p]] += min(A,B+1);
addB[np][pos[p]] += min(A+1,B);
}
x = p;
}
}
int getV()
{
return min({IT[0][1].A[0][0],IT[0][1].A[0][1],IT[0][1].A[1][0],IT[0][1].A[1][1]});
}
int cat(int v) {
push(v, 0, inf);
return getV();
}
int dog(int v) {
push(v, inf ,0);
return getV();
}
int neighbor(int v) {
push(v, 0, 0);
return getV();
}
Compilation message
catdog.cpp: In function 'void initialize(int, std::vector<int>, std::vector<int>)':
catdog.cpp:67:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0;i<A.size();i++){
~^~~~~~~~~
catdog.cpp:79:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j=1;j<G[i].size();j++){
~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
12152 KB |
Output is correct |
2 |
Correct |
17 ms |
12152 KB |
Output is correct |
3 |
Correct |
16 ms |
12152 KB |
Output is correct |
4 |
Correct |
15 ms |
12256 KB |
Output is correct |
5 |
Correct |
16 ms |
12368 KB |
Output is correct |
6 |
Correct |
17 ms |
12368 KB |
Output is correct |
7 |
Correct |
16 ms |
12464 KB |
Output is correct |
8 |
Correct |
16 ms |
12464 KB |
Output is correct |
9 |
Correct |
16 ms |
12484 KB |
Output is correct |
10 |
Correct |
15 ms |
12484 KB |
Output is correct |
11 |
Correct |
16 ms |
12484 KB |
Output is correct |
12 |
Correct |
13 ms |
12484 KB |
Output is correct |
13 |
Correct |
16 ms |
12484 KB |
Output is correct |
14 |
Correct |
16 ms |
12484 KB |
Output is correct |
15 |
Correct |
14 ms |
12484 KB |
Output is correct |
16 |
Correct |
14 ms |
12484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
12152 KB |
Output is correct |
2 |
Correct |
17 ms |
12152 KB |
Output is correct |
3 |
Correct |
16 ms |
12152 KB |
Output is correct |
4 |
Correct |
15 ms |
12256 KB |
Output is correct |
5 |
Correct |
16 ms |
12368 KB |
Output is correct |
6 |
Correct |
17 ms |
12368 KB |
Output is correct |
7 |
Correct |
16 ms |
12464 KB |
Output is correct |
8 |
Correct |
16 ms |
12464 KB |
Output is correct |
9 |
Correct |
16 ms |
12484 KB |
Output is correct |
10 |
Correct |
15 ms |
12484 KB |
Output is correct |
11 |
Correct |
16 ms |
12484 KB |
Output is correct |
12 |
Correct |
13 ms |
12484 KB |
Output is correct |
13 |
Correct |
16 ms |
12484 KB |
Output is correct |
14 |
Correct |
16 ms |
12484 KB |
Output is correct |
15 |
Correct |
14 ms |
12484 KB |
Output is correct |
16 |
Correct |
14 ms |
12484 KB |
Output is correct |
17 |
Correct |
16 ms |
12680 KB |
Output is correct |
18 |
Correct |
17 ms |
12692 KB |
Output is correct |
19 |
Correct |
15 ms |
12708 KB |
Output is correct |
20 |
Correct |
15 ms |
12708 KB |
Output is correct |
21 |
Correct |
18 ms |
12708 KB |
Output is correct |
22 |
Correct |
17 ms |
12708 KB |
Output is correct |
23 |
Correct |
18 ms |
12804 KB |
Output is correct |
24 |
Correct |
17 ms |
12812 KB |
Output is correct |
25 |
Correct |
18 ms |
12812 KB |
Output is correct |
26 |
Correct |
14 ms |
12812 KB |
Output is correct |
27 |
Correct |
16 ms |
12812 KB |
Output is correct |
28 |
Correct |
14 ms |
12844 KB |
Output is correct |
29 |
Correct |
14 ms |
12852 KB |
Output is correct |
30 |
Correct |
15 ms |
12852 KB |
Output is correct |
31 |
Correct |
16 ms |
12872 KB |
Output is correct |
32 |
Correct |
17 ms |
12872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
12152 KB |
Output is correct |
2 |
Correct |
17 ms |
12152 KB |
Output is correct |
3 |
Correct |
16 ms |
12152 KB |
Output is correct |
4 |
Correct |
15 ms |
12256 KB |
Output is correct |
5 |
Correct |
16 ms |
12368 KB |
Output is correct |
6 |
Correct |
17 ms |
12368 KB |
Output is correct |
7 |
Correct |
16 ms |
12464 KB |
Output is correct |
8 |
Correct |
16 ms |
12464 KB |
Output is correct |
9 |
Correct |
16 ms |
12484 KB |
Output is correct |
10 |
Correct |
15 ms |
12484 KB |
Output is correct |
11 |
Correct |
16 ms |
12484 KB |
Output is correct |
12 |
Correct |
13 ms |
12484 KB |
Output is correct |
13 |
Correct |
16 ms |
12484 KB |
Output is correct |
14 |
Correct |
16 ms |
12484 KB |
Output is correct |
15 |
Correct |
14 ms |
12484 KB |
Output is correct |
16 |
Correct |
14 ms |
12484 KB |
Output is correct |
17 |
Correct |
16 ms |
12680 KB |
Output is correct |
18 |
Correct |
17 ms |
12692 KB |
Output is correct |
19 |
Correct |
15 ms |
12708 KB |
Output is correct |
20 |
Correct |
15 ms |
12708 KB |
Output is correct |
21 |
Correct |
18 ms |
12708 KB |
Output is correct |
22 |
Correct |
17 ms |
12708 KB |
Output is correct |
23 |
Correct |
18 ms |
12804 KB |
Output is correct |
24 |
Correct |
17 ms |
12812 KB |
Output is correct |
25 |
Correct |
18 ms |
12812 KB |
Output is correct |
26 |
Correct |
14 ms |
12812 KB |
Output is correct |
27 |
Correct |
16 ms |
12812 KB |
Output is correct |
28 |
Correct |
14 ms |
12844 KB |
Output is correct |
29 |
Correct |
14 ms |
12852 KB |
Output is correct |
30 |
Correct |
15 ms |
12852 KB |
Output is correct |
31 |
Correct |
16 ms |
12872 KB |
Output is correct |
32 |
Correct |
17 ms |
12872 KB |
Output is correct |
33 |
Correct |
255 ms |
23108 KB |
Output is correct |
34 |
Correct |
164 ms |
25320 KB |
Output is correct |
35 |
Correct |
189 ms |
25320 KB |
Output is correct |
36 |
Correct |
364 ms |
33448 KB |
Output is correct |
37 |
Correct |
43 ms |
33448 KB |
Output is correct |
38 |
Correct |
431 ms |
37312 KB |
Output is correct |
39 |
Correct |
366 ms |
39220 KB |
Output is correct |
40 |
Correct |
544 ms |
41160 KB |
Output is correct |
41 |
Correct |
458 ms |
43212 KB |
Output is correct |
42 |
Correct |
504 ms |
45100 KB |
Output is correct |
43 |
Correct |
366 ms |
47088 KB |
Output is correct |
44 |
Correct |
389 ms |
48908 KB |
Output is correct |
45 |
Correct |
408 ms |
50888 KB |
Output is correct |
46 |
Correct |
389 ms |
52812 KB |
Output is correct |
47 |
Correct |
441 ms |
54660 KB |
Output is correct |
48 |
Correct |
195 ms |
54904 KB |
Output is correct |
49 |
Correct |
211 ms |
60800 KB |
Output is correct |
50 |
Correct |
64 ms |
60800 KB |
Output is correct |
51 |
Correct |
79 ms |
60800 KB |
Output is correct |
52 |
Correct |
59 ms |
60800 KB |
Output is correct |
53 |
Correct |
254 ms |
60800 KB |
Output is correct |
54 |
Correct |
148 ms |
60800 KB |
Output is correct |
55 |
Correct |
378 ms |
60800 KB |
Output is correct |
56 |
Correct |
200 ms |
60800 KB |
Output is correct |
57 |
Correct |
273 ms |
62920 KB |
Output is correct |
58 |
Correct |
62 ms |
62920 KB |
Output is correct |
59 |
Correct |
101 ms |
62920 KB |
Output is correct |
60 |
Correct |
183 ms |
68644 KB |
Output is correct |
61 |
Correct |
218 ms |
71164 KB |
Output is correct |
62 |
Correct |
168 ms |
71164 KB |
Output is correct |
63 |
Correct |
76 ms |
71164 KB |
Output is correct |
64 |
Correct |
82 ms |
71164 KB |
Output is correct |
65 |
Correct |
115 ms |
74216 KB |
Output is correct |
66 |
Correct |
69 ms |
74216 KB |
Output is correct |
67 |
Correct |
99 ms |
74216 KB |
Output is correct |
68 |
Correct |
153 ms |
76660 KB |
Output is correct |
69 |
Correct |
36 ms |
76660 KB |
Output is correct |
70 |
Correct |
22 ms |
76660 KB |
Output is correct |
71 |
Correct |
79 ms |
76660 KB |
Output is correct |
72 |
Correct |
129 ms |
76964 KB |
Output is correct |
73 |
Correct |
289 ms |
83264 KB |
Output is correct |
74 |
Correct |
199 ms |
83264 KB |
Output is correct |
75 |
Correct |
178 ms |
87128 KB |
Output is correct |
76 |
Correct |
190 ms |
89044 KB |
Output is correct |
77 |
Correct |
233 ms |
89044 KB |
Output is correct |