#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 ----------
struct edge { int v,c,d; };
vector<edge> adjList[200000];
LLI ss[200000],hh[200000];
int doDFS(int u,int p) {
int i;
ss[u] = 0;
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i].v;
if (v != p) {
doDFS(v,u);
ss[u] += ss[v]+adjList[u][i].d;
hh[u] = max(hh[u],hh[v]+adjList[u][i].c);
}
}
return 0;
}
int doDFS2(int u,int p,LLI d,LLI h) {
int i;
ss[u] += d;
LLI m1 = 0,m2 = 0,mi = -1;
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i].v;
if (v != p) {
LLI hhh = hh[v]+adjList[u][i].c;
if (hhh > m1) m2 = m1,m1 = hhh,mi = v;
else if (hhh > m2) m2 = hhh;
}
}
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i].v;
if (v != p) {
if (v != mi) doDFS2(v,u,ss[u]-ss[v]-adjList[u][i].d+adjList[u][i].c,max(h,m1)+adjList[u][i].d);
else doDFS2(v,u,ss[u]-ss[v]-adjList[u][i].d+adjList[u][i].c,max(h,m2)+adjList[u][i].d);
}
}
hh[u] = max(hh[u],h);
return 0;
}
LLI ans[200001];
int parent[200000],pw[200000],disc[200000],fin[200000],inv[200000],num = 0;
LLI pp[200000],sum2 = 0;
int done[200000];
int doDFS3(int u,int p,LLI d) {
int i;
parent[u] = p,pp[u] = d,disc[u] = num++,inv[num-1] = u;
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i].v;
if (v != p) pw[v] = adjList[u][i].c,doDFS3(v,u,d+adjList[u][i].c),sum2 += adjList[u][i].d;
}
fin[u] = num;
return 0;
}
LLI tree[1 << 19],lazy[1 << 19];
int pos[1 << 19];
int prop(int s,int e,int i) {
tree[i] += lazy[i];
if (s != e) lazy[2*i+1] += lazy[i],lazy[2*i+2] += lazy[i];
lazy[i] = 0;
return 0;
}
LLI build(int s,int e,int i) {
if (s == e) {
tree[i] = pp[inv[s]],lazy[i] = 0,pos[i] = s;
return tree[i];
}
int mid = (s+e) / 2;
tree[i] = max(build(s,mid,2*i+1),build(mid+1,e,2*i+2)),lazy[i] = 0;
if (tree[2*i+1] > tree[2*i+2]) pos[i] = pos[2*i+1];
else pos[i] = pos[2*i+2];
return tree[i];
}
LLI update(int s,int e,int as,int ae,int i,LLI num) {
prop(s,e,i);
if ((s > ae) || (e < as)) return tree[i];
else if ((s >= as) && (e <= ae)) {
lazy[i] += num;
prop(s,e,i);
return tree[i];
}
int mid = (s+e) / 2;
tree[i] = max(update(s,mid,as,ae,2*i+1,num),update(mid+1,e,as,ae,2*i+2,num));
if (tree[2*i+1] > tree[2*i+2]) pos[i] = pos[2*i+1];
else pos[i] = pos[2*i+2];
return tree[i];
}
int main() {
int i;
int N,Q;
int A,B,C,D;
LLI sum = 0;
scanf("%d",&N);
for (i = 0; i < N-1; i++) {
scanf("%d %d %d %d",&A,&B,&C,&D);
A--,B--;
adjList[A].pb((edge){B,C,D});
adjList[B].pb((edge){A,D,C});
sum += C+D;
}
int b = -1;
doDFS(0,-1);
doDFS2(0,-1,0,0);
for (i = 0; i < N; i++) {
ans[1] = max(ans[1],ss[i]);
if (ss[i]+hh[i] > ans[2]) ans[2] = ss[i]+hh[i],b = i;
}
sum2 = num = 0;
doDFS3(b,-1,0);
fill(done,done+N,0);
build(0,N-1,0);
for (i = 2; i <= N; i++) {
prop(0,N-1,0);
int u = inv[pos[0]];
sum2 += tree[0];
ans[i] = max(ans[i],sum2);
while (!done[u] && (u != b)) {
update(0,N-1,disc[u],fin[u]-1,0,-pw[u]);
done[u] = 1,u = parent[u];
}
}
int E;
scanf("%d",&Q);
for (i = 0; i < Q; i++) {
scanf("%d",&E);
printf("%lld\n",sum-ans[E]);
}
return 0;
}
Compilation message
designated_cities.cpp: In function 'int doDFS(int, int)':
designated_cities.cpp:64:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < adjList[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~
designated_cities.cpp: In function 'int doDFS2(int, int, LLI, LLI)':
designated_cities.cpp:78:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < adjList[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~
designated_cities.cpp:86:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < adjList[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~
designated_cities.cpp: In function 'int doDFS3(int, int, LLI)':
designated_cities.cpp:103:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < adjList[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:150:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&N);
~~~~~^~~~~~~~~
designated_cities.cpp:152:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d %d",&A,&B,&C,&D);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:182:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&Q);
~~~~~^~~~~~~~~
designated_cities.cpp:184:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&E);
~~~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5120 KB |
Output is correct |
2 |
Correct |
6 ms |
5120 KB |
Output is correct |
3 |
Correct |
6 ms |
5120 KB |
Output is correct |
4 |
Correct |
6 ms |
5120 KB |
Output is correct |
5 |
Correct |
6 ms |
5120 KB |
Output is correct |
6 |
Correct |
6 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
5120 KB |
Output is correct |
9 |
Correct |
5 ms |
5120 KB |
Output is correct |
10 |
Correct |
6 ms |
5120 KB |
Output is correct |
11 |
Correct |
6 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5120 KB |
Output is correct |
2 |
Correct |
514 ms |
40828 KB |
Output is correct |
3 |
Correct |
532 ms |
54620 KB |
Output is correct |
4 |
Correct |
488 ms |
39544 KB |
Output is correct |
5 |
Correct |
559 ms |
40960 KB |
Output is correct |
6 |
Correct |
592 ms |
43000 KB |
Output is correct |
7 |
Correct |
473 ms |
40772 KB |
Output is correct |
8 |
Correct |
590 ms |
54944 KB |
Output is correct |
9 |
Correct |
443 ms |
41504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5120 KB |
Output is correct |
2 |
Correct |
549 ms |
40884 KB |
Output is correct |
3 |
Correct |
507 ms |
56736 KB |
Output is correct |
4 |
Correct |
446 ms |
39672 KB |
Output is correct |
5 |
Correct |
509 ms |
40880 KB |
Output is correct |
6 |
Correct |
563 ms |
43512 KB |
Output is correct |
7 |
Correct |
428 ms |
40996 KB |
Output is correct |
8 |
Correct |
583 ms |
50324 KB |
Output is correct |
9 |
Correct |
454 ms |
41112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5120 KB |
Output is correct |
2 |
Correct |
6 ms |
5120 KB |
Output is correct |
3 |
Correct |
6 ms |
5120 KB |
Output is correct |
4 |
Correct |
6 ms |
5120 KB |
Output is correct |
5 |
Correct |
6 ms |
5120 KB |
Output is correct |
6 |
Correct |
6 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
5120 KB |
Output is correct |
9 |
Correct |
5 ms |
5120 KB |
Output is correct |
10 |
Correct |
6 ms |
5120 KB |
Output is correct |
11 |
Correct |
6 ms |
5120 KB |
Output is correct |
12 |
Correct |
5 ms |
5120 KB |
Output is correct |
13 |
Correct |
8 ms |
5504 KB |
Output is correct |
14 |
Correct |
9 ms |
5632 KB |
Output is correct |
15 |
Correct |
8 ms |
5504 KB |
Output is correct |
16 |
Correct |
9 ms |
5504 KB |
Output is correct |
17 |
Correct |
9 ms |
5504 KB |
Output is correct |
18 |
Correct |
8 ms |
5504 KB |
Output is correct |
19 |
Correct |
8 ms |
5504 KB |
Output is correct |
20 |
Correct |
8 ms |
5504 KB |
Output is correct |
21 |
Correct |
9 ms |
5504 KB |
Output is correct |
22 |
Correct |
8 ms |
5504 KB |
Output is correct |
23 |
Correct |
8 ms |
5504 KB |
Output is correct |
24 |
Correct |
9 ms |
5504 KB |
Output is correct |
25 |
Correct |
9 ms |
5632 KB |
Output is correct |
26 |
Correct |
9 ms |
5504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5120 KB |
Output is correct |
2 |
Correct |
514 ms |
40828 KB |
Output is correct |
3 |
Correct |
532 ms |
54620 KB |
Output is correct |
4 |
Correct |
488 ms |
39544 KB |
Output is correct |
5 |
Correct |
559 ms |
40960 KB |
Output is correct |
6 |
Correct |
592 ms |
43000 KB |
Output is correct |
7 |
Correct |
473 ms |
40772 KB |
Output is correct |
8 |
Correct |
590 ms |
54944 KB |
Output is correct |
9 |
Correct |
443 ms |
41504 KB |
Output is correct |
10 |
Correct |
6 ms |
5120 KB |
Output is correct |
11 |
Correct |
549 ms |
40884 KB |
Output is correct |
12 |
Correct |
507 ms |
56736 KB |
Output is correct |
13 |
Correct |
446 ms |
39672 KB |
Output is correct |
14 |
Correct |
509 ms |
40880 KB |
Output is correct |
15 |
Correct |
563 ms |
43512 KB |
Output is correct |
16 |
Correct |
428 ms |
40996 KB |
Output is correct |
17 |
Correct |
583 ms |
50324 KB |
Output is correct |
18 |
Correct |
454 ms |
41112 KB |
Output is correct |
19 |
Correct |
6 ms |
5120 KB |
Output is correct |
20 |
Correct |
556 ms |
40944 KB |
Output is correct |
21 |
Correct |
520 ms |
56992 KB |
Output is correct |
22 |
Correct |
502 ms |
39604 KB |
Output is correct |
23 |
Correct |
531 ms |
40952 KB |
Output is correct |
24 |
Correct |
566 ms |
39800 KB |
Output is correct |
25 |
Correct |
567 ms |
40848 KB |
Output is correct |
26 |
Correct |
577 ms |
39928 KB |
Output is correct |
27 |
Correct |
572 ms |
40884 KB |
Output is correct |
28 |
Correct |
569 ms |
43000 KB |
Output is correct |
29 |
Correct |
562 ms |
40952 KB |
Output is correct |
30 |
Correct |
540 ms |
40080 KB |
Output is correct |
31 |
Correct |
508 ms |
40900 KB |
Output is correct |
32 |
Correct |
634 ms |
50936 KB |
Output is correct |
33 |
Correct |
472 ms |
41528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5120 KB |
Output is correct |
2 |
Correct |
6 ms |
5120 KB |
Output is correct |
3 |
Correct |
6 ms |
5120 KB |
Output is correct |
4 |
Correct |
6 ms |
5120 KB |
Output is correct |
5 |
Correct |
6 ms |
5120 KB |
Output is correct |
6 |
Correct |
6 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
5120 KB |
Output is correct |
9 |
Correct |
5 ms |
5120 KB |
Output is correct |
10 |
Correct |
6 ms |
5120 KB |
Output is correct |
11 |
Correct |
6 ms |
5120 KB |
Output is correct |
12 |
Correct |
6 ms |
5120 KB |
Output is correct |
13 |
Correct |
514 ms |
40828 KB |
Output is correct |
14 |
Correct |
532 ms |
54620 KB |
Output is correct |
15 |
Correct |
488 ms |
39544 KB |
Output is correct |
16 |
Correct |
559 ms |
40960 KB |
Output is correct |
17 |
Correct |
592 ms |
43000 KB |
Output is correct |
18 |
Correct |
473 ms |
40772 KB |
Output is correct |
19 |
Correct |
590 ms |
54944 KB |
Output is correct |
20 |
Correct |
443 ms |
41504 KB |
Output is correct |
21 |
Correct |
6 ms |
5120 KB |
Output is correct |
22 |
Correct |
549 ms |
40884 KB |
Output is correct |
23 |
Correct |
507 ms |
56736 KB |
Output is correct |
24 |
Correct |
446 ms |
39672 KB |
Output is correct |
25 |
Correct |
509 ms |
40880 KB |
Output is correct |
26 |
Correct |
563 ms |
43512 KB |
Output is correct |
27 |
Correct |
428 ms |
40996 KB |
Output is correct |
28 |
Correct |
583 ms |
50324 KB |
Output is correct |
29 |
Correct |
454 ms |
41112 KB |
Output is correct |
30 |
Correct |
5 ms |
5120 KB |
Output is correct |
31 |
Correct |
8 ms |
5504 KB |
Output is correct |
32 |
Correct |
9 ms |
5632 KB |
Output is correct |
33 |
Correct |
8 ms |
5504 KB |
Output is correct |
34 |
Correct |
9 ms |
5504 KB |
Output is correct |
35 |
Correct |
9 ms |
5504 KB |
Output is correct |
36 |
Correct |
8 ms |
5504 KB |
Output is correct |
37 |
Correct |
8 ms |
5504 KB |
Output is correct |
38 |
Correct |
8 ms |
5504 KB |
Output is correct |
39 |
Correct |
9 ms |
5504 KB |
Output is correct |
40 |
Correct |
8 ms |
5504 KB |
Output is correct |
41 |
Correct |
8 ms |
5504 KB |
Output is correct |
42 |
Correct |
9 ms |
5504 KB |
Output is correct |
43 |
Correct |
9 ms |
5632 KB |
Output is correct |
44 |
Correct |
9 ms |
5504 KB |
Output is correct |
45 |
Correct |
6 ms |
5120 KB |
Output is correct |
46 |
Correct |
556 ms |
40944 KB |
Output is correct |
47 |
Correct |
520 ms |
56992 KB |
Output is correct |
48 |
Correct |
502 ms |
39604 KB |
Output is correct |
49 |
Correct |
531 ms |
40952 KB |
Output is correct |
50 |
Correct |
566 ms |
39800 KB |
Output is correct |
51 |
Correct |
567 ms |
40848 KB |
Output is correct |
52 |
Correct |
577 ms |
39928 KB |
Output is correct |
53 |
Correct |
572 ms |
40884 KB |
Output is correct |
54 |
Correct |
569 ms |
43000 KB |
Output is correct |
55 |
Correct |
562 ms |
40952 KB |
Output is correct |
56 |
Correct |
540 ms |
40080 KB |
Output is correct |
57 |
Correct |
508 ms |
40900 KB |
Output is correct |
58 |
Correct |
634 ms |
50936 KB |
Output is correct |
59 |
Correct |
472 ms |
41528 KB |
Output is correct |
60 |
Correct |
6 ms |
5120 KB |
Output is correct |
61 |
Correct |
655 ms |
43580 KB |
Output is correct |
62 |
Correct |
597 ms |
56288 KB |
Output is correct |
63 |
Correct |
513 ms |
42232 KB |
Output is correct |
64 |
Correct |
576 ms |
43640 KB |
Output is correct |
65 |
Correct |
576 ms |
42360 KB |
Output is correct |
66 |
Correct |
620 ms |
43524 KB |
Output is correct |
67 |
Correct |
567 ms |
42292 KB |
Output is correct |
68 |
Correct |
564 ms |
43700 KB |
Output is correct |
69 |
Correct |
591 ms |
45628 KB |
Output is correct |
70 |
Correct |
573 ms |
43396 KB |
Output is correct |
71 |
Correct |
694 ms |
42780 KB |
Output is correct |
72 |
Correct |
501 ms |
44332 KB |
Output is correct |
73 |
Correct |
623 ms |
52984 KB |
Output is correct |
74 |
Correct |
482 ms |
45540 KB |
Output is correct |