#define DEBUG 1
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template<typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
#include "catdog.h"
int n;
vi adjList[100000];
int parent[100000],size[100000],depth[100000],heavy[100000];
int chain[100000],disc[100000],head[100000],tail[100000],csize[100000];
int c = 0,num = 0;
int curr[100000],dp[100000][2];
struct node { int cc,cd,dc,dd; };
node com(node a,node b) {
if (a.cc == -1) return b;
else if (b.cc == -1) return a;
node c;
c.cc = min(min(min(a.cc+b.cc,a.cd+b.dc),min(a.cd+b.cc,a.cc+b.dc)+1),(int) 1e9);
c.cd = min(min(min(a.cc+b.cd,a.cd+b.dd),min(a.cd+b.cd,a.cc+b.dd)+1),(int) 1e9);
c.dc = min(min(min(a.dc+b.cc,a.dd+b.dc),min(a.dd+b.cc,a.dc+b.dc)+1),(int) 1e9);
c.dd = min(min(min(a.dc+b.cd,a.dd+b.dd),min(a.dd+b.cd,a.dc+b.dd)+1),(int) 1e9);
return c;
}
node tree[1 << 18];
int build(int s,int e,int i) {
if (s == e) {
tree[i] = (node){0,(int) 1e9,(int) 1e9,0};
return 0;
}
int mid = (s+e) / 2;
build(s,mid,2*i+1),build(mid+1,e,2*i+2);
tree[i] = com(tree[2*i+1],tree[2*i+2]);
return 0;
}
int update(int s,int e,int ai,int i,int c,int d) {
if ((s > ai) || (e < ai)) return 0;
else if (s == e) {
tree[i].cc += c,tree[i].dd += d;
return 0;
}
int mid = (s+e) / 2;
update(s,mid,ai,2*i+1,c,d),update(mid+1,e,ai,2*i+2,c,d);
tree[i] = com(tree[2*i+1],tree[2*i+2]);
return 0;
}
node query(int s,int e,int qs,int qe,int i) {
if ((s > qe) || (e < qs)) return (node){-1,-1,-1,-1};
else if ((s >= qs) && (e <= qe)) return tree[i];
int mid = (s+e) / 2;
return com(query(s,mid,qs,qe,2*i+1),query(mid+1,e,qs,qe,2*i+2));
}
pii query(int u) {
node r = query(0,n-1,disc[head[chain[u]]],disc[tail[chain[u]]],0);
int c = min(r.cc,r.cd),d = min(r.dc,r.dd);
return mp(min(c,d+1),min(d,c+1));
}
int doDFS(int u,int p,int d) {
int i;
parent[u] = p,depth[u] = d,size[u] = 1,heavy[u] = -1;
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i];
if (v != p) {
size[u] += doDFS(v,u,d+1);
if ((heavy[u] == -1) || (size[v] > size[heavy[u]])) heavy[u] = v;
}
}
return size[u];
}
int doHLD(int u,int p) {
int i;
chain[u] = c,disc[u] = num++;
if (csize[c] == 0) head[c] = u;
csize[c]++,tail[c] = u;
if (heavy[u] != -1) doHLD(heavy[u],u);
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i];
if ((v != p) && (v != heavy[u])) c++,doHLD(v,u);
}
return 0;
}
void initialize(int N,vector<int> A,vector<int> B) {
int i;
n = N;
for (i = 0; i < N-1; i++) {
A[i]--,B[i]--;
adjList[A[i]].pb(B[i]);
adjList[B[i]].pb(A[i]);
}
doDFS(0,-1,0),doHLD(0,-1),build(0,N-1,0);
}
int update(int u,int c,int d) {
while (u != -1) {
pii a = query(u);
update(0,n-1,disc[u],0,c,d);
pii b = query(u);
c = b.first-a.first,d = b.second-a.second;
u = parent[head[chain[u]]];
}
return 0;
}
int cat(int v) {
v--,curr[v] = 1,update(v,0,1e9);
pii ans = query(0);
return min(ans.first,ans.second);
}
int dog(int v) {
v--,curr[v] = 2,update(v,1e9,0);
pii ans = query(0);
return min(ans.first,ans.second);
}
int neighbor(int v) {
v--;
if (curr[v] == 1) update(v,0,-1e9);
else if (curr[v] == 2) update(v,-1e9,0);
curr[v] = 0;
pii ans = query(0);
return min(ans.first,ans.second);
}
Compilation message
catdog.cpp: In function 'int doDFS(int, int, int)':
catdog.cpp:115:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < adjList[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~
catdog.cpp: In function 'int doHLD(int, int)':
catdog.cpp:130:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < adjList[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
5 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 |
2704 KB |
Output is correct |
6 |
Correct |
4 ms |
2808 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 |
2684 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 |
2680 KB |
Output is correct |
16 |
Correct |
4 ms |
2808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
5 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 |
2704 KB |
Output is correct |
6 |
Correct |
4 ms |
2808 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 |
2684 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 |
2680 KB |
Output is correct |
16 |
Correct |
4 ms |
2808 KB |
Output is correct |
17 |
Correct |
6 ms |
2808 KB |
Output is correct |
18 |
Correct |
6 ms |
2780 KB |
Output is correct |
19 |
Correct |
5 ms |
2808 KB |
Output is correct |
20 |
Correct |
4 ms |
2680 KB |
Output is correct |
21 |
Correct |
5 ms |
2680 KB |
Output is correct |
22 |
Correct |
5 ms |
2808 KB |
Output is correct |
23 |
Correct |
6 ms |
3064 KB |
Output is correct |
24 |
Correct |
6 ms |
2808 KB |
Output is correct |
25 |
Correct |
5 ms |
2808 KB |
Output is correct |
26 |
Correct |
5 ms |
2808 KB |
Output is correct |
27 |
Correct |
5 ms |
2808 KB |
Output is correct |
28 |
Correct |
5 ms |
2808 KB |
Output is correct |
29 |
Correct |
5 ms |
2936 KB |
Output is correct |
30 |
Correct |
5 ms |
2680 KB |
Output is correct |
31 |
Correct |
5 ms |
2808 KB |
Output is correct |
32 |
Correct |
5 ms |
2808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
5 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 |
2704 KB |
Output is correct |
6 |
Correct |
4 ms |
2808 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 |
2684 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 |
2680 KB |
Output is correct |
16 |
Correct |
4 ms |
2808 KB |
Output is correct |
17 |
Correct |
6 ms |
2808 KB |
Output is correct |
18 |
Correct |
6 ms |
2780 KB |
Output is correct |
19 |
Correct |
5 ms |
2808 KB |
Output is correct |
20 |
Correct |
4 ms |
2680 KB |
Output is correct |
21 |
Correct |
5 ms |
2680 KB |
Output is correct |
22 |
Correct |
5 ms |
2808 KB |
Output is correct |
23 |
Correct |
6 ms |
3064 KB |
Output is correct |
24 |
Correct |
6 ms |
2808 KB |
Output is correct |
25 |
Correct |
5 ms |
2808 KB |
Output is correct |
26 |
Correct |
5 ms |
2808 KB |
Output is correct |
27 |
Correct |
5 ms |
2808 KB |
Output is correct |
28 |
Correct |
5 ms |
2808 KB |
Output is correct |
29 |
Correct |
5 ms |
2936 KB |
Output is correct |
30 |
Correct |
5 ms |
2680 KB |
Output is correct |
31 |
Correct |
5 ms |
2808 KB |
Output is correct |
32 |
Correct |
5 ms |
2808 KB |
Output is correct |
33 |
Correct |
437 ms |
10712 KB |
Output is correct |
34 |
Correct |
144 ms |
10616 KB |
Output is correct |
35 |
Correct |
406 ms |
9196 KB |
Output is correct |
36 |
Correct |
712 ms |
16748 KB |
Output is correct |
37 |
Correct |
26 ms |
6520 KB |
Output is correct |
38 |
Correct |
794 ms |
17740 KB |
Output is correct |
39 |
Correct |
816 ms |
17752 KB |
Output is correct |
40 |
Correct |
790 ms |
17636 KB |
Output is correct |
41 |
Correct |
780 ms |
17652 KB |
Output is correct |
42 |
Correct |
737 ms |
17768 KB |
Output is correct |
43 |
Correct |
749 ms |
17764 KB |
Output is correct |
44 |
Correct |
714 ms |
17768 KB |
Output is correct |
45 |
Correct |
731 ms |
17736 KB |
Output is correct |
46 |
Correct |
736 ms |
17744 KB |
Output is correct |
47 |
Correct |
756 ms |
17740 KB |
Output is correct |
48 |
Correct |
233 ms |
15284 KB |
Output is correct |
49 |
Correct |
267 ms |
17072 KB |
Output is correct |
50 |
Correct |
81 ms |
6264 KB |
Output is correct |
51 |
Correct |
99 ms |
8844 KB |
Output is correct |
52 |
Correct |
40 ms |
5880 KB |
Output is correct |
53 |
Correct |
325 ms |
15992 KB |
Output is correct |
54 |
Correct |
249 ms |
9136 KB |
Output is correct |
55 |
Correct |
628 ms |
14732 KB |
Output is correct |
56 |
Correct |
366 ms |
10100 KB |
Output is correct |
57 |
Correct |
491 ms |
15928 KB |
Output is correct |
58 |
Correct |
44 ms |
8948 KB |
Output is correct |
59 |
Correct |
91 ms |
7672 KB |
Output is correct |
60 |
Correct |
213 ms |
16120 KB |
Output is correct |
61 |
Correct |
236 ms |
16480 KB |
Output is correct |
62 |
Correct |
139 ms |
14324 KB |
Output is correct |
63 |
Correct |
72 ms |
11996 KB |
Output is correct |
64 |
Correct |
80 ms |
13688 KB |
Output is correct |
65 |
Correct |
109 ms |
20608 KB |
Output is correct |
66 |
Correct |
98 ms |
7544 KB |
Output is correct |
67 |
Correct |
106 ms |
16760 KB |
Output is correct |
68 |
Correct |
216 ms |
21064 KB |
Output is correct |
69 |
Correct |
45 ms |
4600 KB |
Output is correct |
70 |
Correct |
13 ms |
3064 KB |
Output is correct |
71 |
Correct |
87 ms |
11520 KB |
Output is correct |
72 |
Correct |
135 ms |
19064 KB |
Output is correct |
73 |
Correct |
339 ms |
24160 KB |
Output is correct |
74 |
Correct |
366 ms |
20728 KB |
Output is correct |
75 |
Correct |
247 ms |
24204 KB |
Output is correct |
76 |
Correct |
239 ms |
22904 KB |
Output is correct |
77 |
Correct |
376 ms |
21112 KB |
Output is correct |