#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 ----------
vi adjList[100000];
int parent[100000],depth[100000],size[100000],heavy[100000];
int chain[100000],disc[100000],head[100000],csize[100000];
int num = 0,c = 0;
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]++;
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;
}
vector<pair<pii,int> > paths[100000];
int dp[100000];
int bit[100001];
int query(int i) {
int sum = 0;
for (i++; i > 0; i -= i & (-i)) sum += bit[i];
return sum;
}
int update(int i,int num,int N) {
for (i++; i <= N; i += i & (-i)) bit[i] += num;
return 0;
}
int doDFS2(int u,int N) {
int i;
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i];
if (v != parent[u]) dp[u] += doDFS2(v,N);
}
for (i = 0; i < paths[u].size(); i++) {
int uu = paths[u][i].first.first;
int vv = paths[u][i].first.second;
int s = 0;
while (chain[uu] != chain[vv]) {
if (depth[head[chain[uu]]] < depth[head[chain[vv]]]) swap(uu,vv);
s += query(disc[uu])-query(disc[head[chain[uu]]]-1);
uu = parent[head[chain[uu]]];
}
if (depth[uu] < depth[vv]) s += query(disc[vv])-query(disc[uu]-1);
else s += query(disc[uu])-query(disc[vv]-1);
dp[u] = max(dp[u],s+paths[u][i].second);
}
if (parent[u] != -1) update(disc[parent[u]],dp[u],N);
update(disc[u],-dp[u],N);
return dp[u];
}
int main() {
int i;
int N,M,A,B,C;
scanf("%d",&N);
for (i = 1; i < N; i++) {
scanf("%d %d",&A,&B);
A--,B--;
adjList[A].pb(B);
adjList[B].pb(A);
}
doDFS(0,-1,0);
doHLD(0,-1);
scanf("%d",&M);
for (i = 0; i < M; i++) {
scanf("%d %d %d",&A,&B,&C);
A--,B--;
int u = A,v = B;
while (chain[u] != chain[v]) {
if (depth[head[chain[u]]] < depth[head[chain[v]]]) swap(u,v);
u = parent[head[chain[u]]];
}
if (depth[u] < depth[v]) paths[u].pb(mp(mp(A,B),C));
else paths[v].pb(mp(mp(A,B),C));
}
printf("%d\n",doDFS2(0,N));
return 0;
}
Compilation message
election_campaign.cpp: In function 'int doDFS(int, int, int)':
election_campaign.cpp:65:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < adjList[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~
election_campaign.cpp: In function 'int doHLD(int, int)':
election_campaign.cpp:81:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < adjList[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~
election_campaign.cpp: In function 'int doDFS2(int, int)':
election_campaign.cpp:101:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < adjList[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~
election_campaign.cpp:105:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < paths[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:125:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&N);
~~~~~^~~~~~~~~
election_campaign.cpp:127:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&A,&B);
~~~~~^~~~~~~~~~~~~~~
election_campaign.cpp:134:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&M);
~~~~~^~~~~~~~~
election_campaign.cpp:136:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d",&A,&B,&C);
~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5212 KB |
Output is correct |
3 |
Correct |
6 ms |
5324 KB |
Output is correct |
4 |
Correct |
6 ms |
5436 KB |
Output is correct |
5 |
Correct |
115 ms |
13380 KB |
Output is correct |
6 |
Correct |
60 ms |
21876 KB |
Output is correct |
7 |
Correct |
112 ms |
21876 KB |
Output is correct |
8 |
Correct |
94 ms |
21876 KB |
Output is correct |
9 |
Correct |
132 ms |
21876 KB |
Output is correct |
10 |
Correct |
94 ms |
21876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
21876 KB |
Output is correct |
2 |
Correct |
7 ms |
21876 KB |
Output is correct |
3 |
Correct |
7 ms |
21876 KB |
Output is correct |
4 |
Correct |
125 ms |
31116 KB |
Output is correct |
5 |
Correct |
127 ms |
33484 KB |
Output is correct |
6 |
Correct |
125 ms |
36020 KB |
Output is correct |
7 |
Correct |
128 ms |
38676 KB |
Output is correct |
8 |
Correct |
125 ms |
40932 KB |
Output is correct |
9 |
Correct |
121 ms |
43388 KB |
Output is correct |
10 |
Correct |
130 ms |
45884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
21876 KB |
Output is correct |
2 |
Correct |
7 ms |
21876 KB |
Output is correct |
3 |
Correct |
7 ms |
21876 KB |
Output is correct |
4 |
Correct |
125 ms |
31116 KB |
Output is correct |
5 |
Correct |
127 ms |
33484 KB |
Output is correct |
6 |
Correct |
125 ms |
36020 KB |
Output is correct |
7 |
Correct |
128 ms |
38676 KB |
Output is correct |
8 |
Correct |
125 ms |
40932 KB |
Output is correct |
9 |
Correct |
121 ms |
43388 KB |
Output is correct |
10 |
Correct |
130 ms |
45884 KB |
Output is correct |
11 |
Correct |
18 ms |
45884 KB |
Output is correct |
12 |
Correct |
127 ms |
48824 KB |
Output is correct |
13 |
Correct |
129 ms |
51668 KB |
Output is correct |
14 |
Correct |
126 ms |
54312 KB |
Output is correct |
15 |
Correct |
144 ms |
57140 KB |
Output is correct |
16 |
Correct |
128 ms |
59744 KB |
Output is correct |
17 |
Correct |
133 ms |
62476 KB |
Output is correct |
18 |
Correct |
127 ms |
65208 KB |
Output is correct |
19 |
Correct |
124 ms |
68192 KB |
Output is correct |
20 |
Correct |
124 ms |
70848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
257 ms |
70848 KB |
Output is correct |
2 |
Correct |
117 ms |
75656 KB |
Output is correct |
3 |
Correct |
202 ms |
75656 KB |
Output is correct |
4 |
Correct |
167 ms |
75656 KB |
Output is correct |
5 |
Correct |
204 ms |
79736 KB |
Output is correct |
6 |
Correct |
165 ms |
79736 KB |
Output is correct |
7 |
Correct |
200 ms |
84364 KB |
Output is correct |
8 |
Correct |
224 ms |
84364 KB |
Output is correct |
9 |
Correct |
120 ms |
93076 KB |
Output is correct |
10 |
Correct |
191 ms |
93076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5212 KB |
Output is correct |
3 |
Correct |
6 ms |
5324 KB |
Output is correct |
4 |
Correct |
6 ms |
5436 KB |
Output is correct |
5 |
Correct |
115 ms |
13380 KB |
Output is correct |
6 |
Correct |
60 ms |
21876 KB |
Output is correct |
7 |
Correct |
112 ms |
21876 KB |
Output is correct |
8 |
Correct |
94 ms |
21876 KB |
Output is correct |
9 |
Correct |
132 ms |
21876 KB |
Output is correct |
10 |
Correct |
94 ms |
21876 KB |
Output is correct |
11 |
Correct |
7 ms |
93076 KB |
Output is correct |
12 |
Correct |
8 ms |
93076 KB |
Output is correct |
13 |
Correct |
7 ms |
93076 KB |
Output is correct |
14 |
Correct |
7 ms |
93076 KB |
Output is correct |
15 |
Correct |
8 ms |
93076 KB |
Output is correct |
16 |
Correct |
8 ms |
93076 KB |
Output is correct |
17 |
Correct |
7 ms |
93076 KB |
Output is correct |
18 |
Correct |
8 ms |
93076 KB |
Output is correct |
19 |
Correct |
7 ms |
93076 KB |
Output is correct |
20 |
Correct |
10 ms |
93076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5212 KB |
Output is correct |
3 |
Correct |
6 ms |
5324 KB |
Output is correct |
4 |
Correct |
6 ms |
5436 KB |
Output is correct |
5 |
Correct |
115 ms |
13380 KB |
Output is correct |
6 |
Correct |
60 ms |
21876 KB |
Output is correct |
7 |
Correct |
112 ms |
21876 KB |
Output is correct |
8 |
Correct |
94 ms |
21876 KB |
Output is correct |
9 |
Correct |
132 ms |
21876 KB |
Output is correct |
10 |
Correct |
94 ms |
21876 KB |
Output is correct |
11 |
Correct |
6 ms |
21876 KB |
Output is correct |
12 |
Correct |
7 ms |
21876 KB |
Output is correct |
13 |
Correct |
7 ms |
21876 KB |
Output is correct |
14 |
Correct |
125 ms |
31116 KB |
Output is correct |
15 |
Correct |
127 ms |
33484 KB |
Output is correct |
16 |
Correct |
125 ms |
36020 KB |
Output is correct |
17 |
Correct |
128 ms |
38676 KB |
Output is correct |
18 |
Correct |
125 ms |
40932 KB |
Output is correct |
19 |
Correct |
121 ms |
43388 KB |
Output is correct |
20 |
Correct |
130 ms |
45884 KB |
Output is correct |
21 |
Correct |
18 ms |
45884 KB |
Output is correct |
22 |
Correct |
127 ms |
48824 KB |
Output is correct |
23 |
Correct |
129 ms |
51668 KB |
Output is correct |
24 |
Correct |
126 ms |
54312 KB |
Output is correct |
25 |
Correct |
144 ms |
57140 KB |
Output is correct |
26 |
Correct |
128 ms |
59744 KB |
Output is correct |
27 |
Correct |
133 ms |
62476 KB |
Output is correct |
28 |
Correct |
127 ms |
65208 KB |
Output is correct |
29 |
Correct |
124 ms |
68192 KB |
Output is correct |
30 |
Correct |
124 ms |
70848 KB |
Output is correct |
31 |
Correct |
257 ms |
70848 KB |
Output is correct |
32 |
Correct |
117 ms |
75656 KB |
Output is correct |
33 |
Correct |
202 ms |
75656 KB |
Output is correct |
34 |
Correct |
167 ms |
75656 KB |
Output is correct |
35 |
Correct |
204 ms |
79736 KB |
Output is correct |
36 |
Correct |
165 ms |
79736 KB |
Output is correct |
37 |
Correct |
200 ms |
84364 KB |
Output is correct |
38 |
Correct |
224 ms |
84364 KB |
Output is correct |
39 |
Correct |
120 ms |
93076 KB |
Output is correct |
40 |
Correct |
191 ms |
93076 KB |
Output is correct |
41 |
Correct |
7 ms |
93076 KB |
Output is correct |
42 |
Correct |
8 ms |
93076 KB |
Output is correct |
43 |
Correct |
7 ms |
93076 KB |
Output is correct |
44 |
Correct |
7 ms |
93076 KB |
Output is correct |
45 |
Correct |
8 ms |
93076 KB |
Output is correct |
46 |
Correct |
8 ms |
93076 KB |
Output is correct |
47 |
Correct |
7 ms |
93076 KB |
Output is correct |
48 |
Correct |
8 ms |
93076 KB |
Output is correct |
49 |
Correct |
7 ms |
93076 KB |
Output is correct |
50 |
Correct |
10 ms |
93076 KB |
Output is correct |
51 |
Correct |
224 ms |
93076 KB |
Output is correct |
52 |
Correct |
124 ms |
101168 KB |
Output is correct |
53 |
Correct |
194 ms |
101168 KB |
Output is correct |
54 |
Correct |
162 ms |
101168 KB |
Output is correct |
55 |
Correct |
257 ms |
101912 KB |
Output is correct |
56 |
Correct |
125 ms |
112136 KB |
Output is correct |
57 |
Correct |
225 ms |
112136 KB |
Output is correct |
58 |
Correct |
170 ms |
112136 KB |
Output is correct |
59 |
Correct |
255 ms |
112984 KB |
Output is correct |
60 |
Correct |
127 ms |
123412 KB |
Output is correct |
61 |
Correct |
201 ms |
123412 KB |
Output is correct |
62 |
Correct |
178 ms |
123412 KB |
Output is correct |
63 |
Correct |
290 ms |
123468 KB |
Output is correct |
64 |
Correct |
146 ms |
134280 KB |
Output is correct |
65 |
Correct |
251 ms |
134280 KB |
Output is correct |
66 |
Correct |
179 ms |
134280 KB |
Output is correct |
67 |
Correct |
278 ms |
134720 KB |
Output is correct |
68 |
Correct |
180 ms |
145136 KB |
Output is correct |
69 |
Correct |
215 ms |
145136 KB |
Output is correct |
70 |
Correct |
177 ms |
145136 KB |
Output is correct |