#define DEBUG 0
#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++) {
~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
2652 KB |
Output is correct |
6 |
Correct |
4 ms |
2680 KB |
Output is correct |
7 |
Correct |
4 ms |
2684 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 |
2676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
2652 KB |
Output is correct |
6 |
Correct |
4 ms |
2680 KB |
Output is correct |
7 |
Correct |
4 ms |
2684 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 |
2676 KB |
Output is correct |
17 |
Correct |
6 ms |
2808 KB |
Output is correct |
18 |
Correct |
6 ms |
2808 KB |
Output is correct |
19 |
Correct |
5 ms |
2808 KB |
Output is correct |
20 |
Correct |
4 ms |
2808 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 |
2808 KB |
Output is correct |
24 |
Correct |
6 ms |
2808 KB |
Output is correct |
25 |
Correct |
6 ms |
2808 KB |
Output is correct |
26 |
Correct |
4 ms |
2680 KB |
Output is correct |
27 |
Correct |
4 ms |
2808 KB |
Output is correct |
28 |
Correct |
5 ms |
2808 KB |
Output is correct |
29 |
Correct |
5 ms |
2808 KB |
Output is correct |
30 |
Correct |
1 ms |
2740 KB |
Output is correct |
31 |
Correct |
5 ms |
2812 KB |
Output is correct |
32 |
Correct |
5 ms |
2808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
2652 KB |
Output is correct |
6 |
Correct |
4 ms |
2680 KB |
Output is correct |
7 |
Correct |
4 ms |
2684 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 |
2676 KB |
Output is correct |
17 |
Correct |
6 ms |
2808 KB |
Output is correct |
18 |
Correct |
6 ms |
2808 KB |
Output is correct |
19 |
Correct |
5 ms |
2808 KB |
Output is correct |
20 |
Correct |
4 ms |
2808 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 |
2808 KB |
Output is correct |
24 |
Correct |
6 ms |
2808 KB |
Output is correct |
25 |
Correct |
6 ms |
2808 KB |
Output is correct |
26 |
Correct |
4 ms |
2680 KB |
Output is correct |
27 |
Correct |
4 ms |
2808 KB |
Output is correct |
28 |
Correct |
5 ms |
2808 KB |
Output is correct |
29 |
Correct |
5 ms |
2808 KB |
Output is correct |
30 |
Correct |
1 ms |
2740 KB |
Output is correct |
31 |
Correct |
5 ms |
2812 KB |
Output is correct |
32 |
Correct |
5 ms |
2808 KB |
Output is correct |
33 |
Correct |
447 ms |
9564 KB |
Output is correct |
34 |
Correct |
150 ms |
9848 KB |
Output is correct |
35 |
Correct |
416 ms |
8224 KB |
Output is correct |
36 |
Correct |
727 ms |
14920 KB |
Output is correct |
37 |
Correct |
27 ms |
6096 KB |
Output is correct |
38 |
Correct |
796 ms |
15716 KB |
Output is correct |
39 |
Correct |
803 ms |
15732 KB |
Output is correct |
40 |
Correct |
782 ms |
15728 KB |
Output is correct |
41 |
Correct |
795 ms |
15720 KB |
Output is correct |
42 |
Correct |
753 ms |
15744 KB |
Output is correct |
43 |
Correct |
776 ms |
15728 KB |
Output is correct |
44 |
Correct |
731 ms |
15744 KB |
Output is correct |
45 |
Correct |
737 ms |
15752 KB |
Output is correct |
46 |
Correct |
754 ms |
15732 KB |
Output is correct |
47 |
Correct |
778 ms |
15724 KB |
Output is correct |
48 |
Correct |
244 ms |
13784 KB |
Output is correct |
49 |
Correct |
260 ms |
15572 KB |
Output is correct |
50 |
Correct |
82 ms |
5724 KB |
Output is correct |
51 |
Correct |
97 ms |
8212 KB |
Output is correct |
52 |
Correct |
40 ms |
5496 KB |
Output is correct |
53 |
Correct |
309 ms |
14560 KB |
Output is correct |
54 |
Correct |
232 ms |
8376 KB |
Output is correct |
55 |
Correct |
634 ms |
13212 KB |
Output is correct |
56 |
Correct |
366 ms |
8996 KB |
Output is correct |
57 |
Correct |
495 ms |
14288 KB |
Output is correct |
58 |
Correct |
43 ms |
8436 KB |
Output is correct |
59 |
Correct |
93 ms |
6976 KB |
Output is correct |
60 |
Correct |
211 ms |
14584 KB |
Output is correct |
61 |
Correct |
236 ms |
14816 KB |
Output is correct |
62 |
Correct |
138 ms |
13136 KB |
Output is correct |
63 |
Correct |
77 ms |
11256 KB |
Output is correct |
64 |
Correct |
79 ms |
12900 KB |
Output is correct |
65 |
Correct |
109 ms |
19320 KB |
Output is correct |
66 |
Correct |
99 ms |
6852 KB |
Output is correct |
67 |
Correct |
106 ms |
15608 KB |
Output is correct |
68 |
Correct |
211 ms |
19448 KB |
Output is correct |
69 |
Correct |
44 ms |
4220 KB |
Output is correct |
70 |
Correct |
13 ms |
2936 KB |
Output is correct |
71 |
Correct |
100 ms |
10744 KB |
Output is correct |
72 |
Correct |
129 ms |
17912 KB |
Output is correct |
73 |
Correct |
331 ms |
22392 KB |
Output is correct |
74 |
Correct |
359 ms |
18808 KB |
Output is correct |
75 |
Correct |
251 ms |
22268 KB |
Output is correct |
76 |
Correct |
238 ms |
20960 KB |
Output is correct |
77 |
Correct |
369 ms |
19192 KB |
Output is correct |