#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 ans[200001],ss[200000];
int doDFS(int u,int p) {
int i;
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;
}
return 0;
}
int doDFS2(int u,int p,LLI d) {
int i;
ss[u] += d,ans[1] = max(ans[1],ss[u]);
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i].v;
if (v != p) doDFS2(v,u,ss[u]-ss[v]-adjList[u][i].d+adjList[u][i].c);
}
return 0;
}
int deg[200000],done[200000];
priority_queue<pair<LLI,int> > H;
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;
}
doDFS(0,-1);
doDFS2(0,-1,0);
ans[1] = sum-ans[1];
for (i = 0; i < N; i++) {
if (adjList[i].size() == 1) H.push(mp(-adjList[i][0].d,i));
deg[i] = adjList[i].size();
}
while (H.size() > 2) {
int u = H.top().second;
LLI d = -H.top().first;
H.pop();
done[u] = 1;
for (i = 0; i < adjList[u].size(); i++) {
if (!done[adjList[u][i].v]) break;
}
int v = adjList[u][i].v;
deg[v]--;
if (deg[v] == 1) {
for (i = 0; i < adjList[v].size(); i++) {
if (!done[adjList[v][i].v]) {
H.push(mp(-(d+adjList[v][i].d),v));
break;
}
}
}
else ans[H.size()] = ans[H.size()+1]+d;
}
int E;
scanf("%d",&Q);
for (i = 0; i < Q; i++) {
scanf("%d",&E);
printf("%lld\n",ans[E]);
}
return 0;
}
Compilation message
designated_cities.cpp: In function 'int doDFS(int, int)':
designated_cities.cpp:63: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)':
designated_cities.cpp:72: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:106:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < adjList[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~
designated_cities.cpp:112:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < adjList[v].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~
designated_cities.cpp:85:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&N);
~~~~~^~~~~~~~~
designated_cities.cpp:87: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:123:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&Q);
~~~~~^~~~~~~~~
designated_cities.cpp:125:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&E);
~~~~~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
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 |
6 ms |
5120 KB |
Output is correct |
8 |
Correct |
6 ms |
5120 KB |
Output is correct |
9 |
Correct |
6 ms |
5088 KB |
Output is correct |
10 |
Correct |
6 ms |
4992 KB |
Output is correct |
11 |
Correct |
5 ms |
4992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4992 KB |
Output is correct |
2 |
Correct |
404 ms |
19236 KB |
Output is correct |
3 |
Correct |
334 ms |
32796 KB |
Output is correct |
4 |
Correct |
362 ms |
23652 KB |
Output is correct |
5 |
Correct |
404 ms |
25440 KB |
Output is correct |
6 |
Correct |
415 ms |
26788 KB |
Output is correct |
7 |
Correct |
350 ms |
26656 KB |
Output is correct |
8 |
Correct |
365 ms |
34508 KB |
Output is correct |
9 |
Correct |
270 ms |
30064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5120 KB |
Output is correct |
2 |
Correct |
420 ms |
25000 KB |
Output is correct |
3 |
Correct |
349 ms |
29432 KB |
Output is correct |
4 |
Correct |
417 ms |
23684 KB |
Output is correct |
5 |
Correct |
405 ms |
25376 KB |
Output is correct |
6 |
Correct |
419 ms |
26860 KB |
Output is correct |
7 |
Correct |
300 ms |
29204 KB |
Output is correct |
8 |
Correct |
372 ms |
31924 KB |
Output is correct |
9 |
Correct |
298 ms |
29596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
6 ms |
5120 KB |
Output is correct |
8 |
Correct |
6 ms |
5120 KB |
Output is correct |
9 |
Correct |
6 ms |
5088 KB |
Output is correct |
10 |
Correct |
6 ms |
4992 KB |
Output is correct |
11 |
Correct |
5 ms |
4992 KB |
Output is correct |
12 |
Correct |
6 ms |
5120 KB |
Output is correct |
13 |
Correct |
9 ms |
5248 KB |
Output is correct |
14 |
Correct |
8 ms |
5248 KB |
Output is correct |
15 |
Correct |
8 ms |
5248 KB |
Output is correct |
16 |
Correct |
9 ms |
5248 KB |
Output is correct |
17 |
Correct |
8 ms |
5248 KB |
Output is correct |
18 |
Correct |
8 ms |
5248 KB |
Output is correct |
19 |
Correct |
8 ms |
5248 KB |
Output is correct |
20 |
Correct |
8 ms |
5248 KB |
Output is correct |
21 |
Correct |
9 ms |
5248 KB |
Output is correct |
22 |
Correct |
8 ms |
5376 KB |
Output is correct |
23 |
Correct |
9 ms |
5376 KB |
Output is correct |
24 |
Correct |
9 ms |
5376 KB |
Output is correct |
25 |
Correct |
8 ms |
5376 KB |
Output is correct |
26 |
Correct |
8 ms |
5376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4992 KB |
Output is correct |
2 |
Correct |
404 ms |
19236 KB |
Output is correct |
3 |
Correct |
334 ms |
32796 KB |
Output is correct |
4 |
Correct |
362 ms |
23652 KB |
Output is correct |
5 |
Correct |
404 ms |
25440 KB |
Output is correct |
6 |
Correct |
415 ms |
26788 KB |
Output is correct |
7 |
Correct |
350 ms |
26656 KB |
Output is correct |
8 |
Correct |
365 ms |
34508 KB |
Output is correct |
9 |
Correct |
270 ms |
30064 KB |
Output is correct |
10 |
Correct |
6 ms |
5120 KB |
Output is correct |
11 |
Correct |
420 ms |
25000 KB |
Output is correct |
12 |
Correct |
349 ms |
29432 KB |
Output is correct |
13 |
Correct |
417 ms |
23684 KB |
Output is correct |
14 |
Correct |
405 ms |
25376 KB |
Output is correct |
15 |
Correct |
419 ms |
26860 KB |
Output is correct |
16 |
Correct |
300 ms |
29204 KB |
Output is correct |
17 |
Correct |
372 ms |
31924 KB |
Output is correct |
18 |
Correct |
298 ms |
29596 KB |
Output is correct |
19 |
Correct |
6 ms |
5120 KB |
Output is correct |
20 |
Correct |
535 ms |
24904 KB |
Output is correct |
21 |
Correct |
344 ms |
35064 KB |
Output is correct |
22 |
Correct |
375 ms |
23484 KB |
Output is correct |
23 |
Correct |
433 ms |
24936 KB |
Output is correct |
24 |
Correct |
426 ms |
23804 KB |
Output is correct |
25 |
Correct |
386 ms |
24808 KB |
Output is correct |
26 |
Correct |
390 ms |
23736 KB |
Output is correct |
27 |
Correct |
410 ms |
25224 KB |
Output is correct |
28 |
Correct |
412 ms |
26740 KB |
Output is correct |
29 |
Correct |
400 ms |
24552 KB |
Output is correct |
30 |
Correct |
403 ms |
25000 KB |
Output is correct |
31 |
Correct |
350 ms |
27164 KB |
Output is correct |
32 |
Correct |
387 ms |
32528 KB |
Output is correct |
33 |
Correct |
272 ms |
30108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
6 ms |
5120 KB |
Output is correct |
8 |
Correct |
6 ms |
5120 KB |
Output is correct |
9 |
Correct |
6 ms |
5088 KB |
Output is correct |
10 |
Correct |
6 ms |
4992 KB |
Output is correct |
11 |
Correct |
5 ms |
4992 KB |
Output is correct |
12 |
Correct |
5 ms |
4992 KB |
Output is correct |
13 |
Correct |
404 ms |
19236 KB |
Output is correct |
14 |
Correct |
334 ms |
32796 KB |
Output is correct |
15 |
Correct |
362 ms |
23652 KB |
Output is correct |
16 |
Correct |
404 ms |
25440 KB |
Output is correct |
17 |
Correct |
415 ms |
26788 KB |
Output is correct |
18 |
Correct |
350 ms |
26656 KB |
Output is correct |
19 |
Correct |
365 ms |
34508 KB |
Output is correct |
20 |
Correct |
270 ms |
30064 KB |
Output is correct |
21 |
Correct |
6 ms |
5120 KB |
Output is correct |
22 |
Correct |
420 ms |
25000 KB |
Output is correct |
23 |
Correct |
349 ms |
29432 KB |
Output is correct |
24 |
Correct |
417 ms |
23684 KB |
Output is correct |
25 |
Correct |
405 ms |
25376 KB |
Output is correct |
26 |
Correct |
419 ms |
26860 KB |
Output is correct |
27 |
Correct |
300 ms |
29204 KB |
Output is correct |
28 |
Correct |
372 ms |
31924 KB |
Output is correct |
29 |
Correct |
298 ms |
29596 KB |
Output is correct |
30 |
Correct |
6 ms |
5120 KB |
Output is correct |
31 |
Correct |
9 ms |
5248 KB |
Output is correct |
32 |
Correct |
8 ms |
5248 KB |
Output is correct |
33 |
Correct |
8 ms |
5248 KB |
Output is correct |
34 |
Correct |
9 ms |
5248 KB |
Output is correct |
35 |
Correct |
8 ms |
5248 KB |
Output is correct |
36 |
Correct |
8 ms |
5248 KB |
Output is correct |
37 |
Correct |
8 ms |
5248 KB |
Output is correct |
38 |
Correct |
8 ms |
5248 KB |
Output is correct |
39 |
Correct |
9 ms |
5248 KB |
Output is correct |
40 |
Correct |
8 ms |
5376 KB |
Output is correct |
41 |
Correct |
9 ms |
5376 KB |
Output is correct |
42 |
Correct |
9 ms |
5376 KB |
Output is correct |
43 |
Correct |
8 ms |
5376 KB |
Output is correct |
44 |
Correct |
8 ms |
5376 KB |
Output is correct |
45 |
Correct |
6 ms |
5120 KB |
Output is correct |
46 |
Correct |
535 ms |
24904 KB |
Output is correct |
47 |
Correct |
344 ms |
35064 KB |
Output is correct |
48 |
Correct |
375 ms |
23484 KB |
Output is correct |
49 |
Correct |
433 ms |
24936 KB |
Output is correct |
50 |
Correct |
426 ms |
23804 KB |
Output is correct |
51 |
Correct |
386 ms |
24808 KB |
Output is correct |
52 |
Correct |
390 ms |
23736 KB |
Output is correct |
53 |
Correct |
410 ms |
25224 KB |
Output is correct |
54 |
Correct |
412 ms |
26740 KB |
Output is correct |
55 |
Correct |
400 ms |
24552 KB |
Output is correct |
56 |
Correct |
403 ms |
25000 KB |
Output is correct |
57 |
Correct |
350 ms |
27164 KB |
Output is correct |
58 |
Correct |
387 ms |
32528 KB |
Output is correct |
59 |
Correct |
272 ms |
30108 KB |
Output is correct |
60 |
Correct |
6 ms |
5120 KB |
Output is correct |
61 |
Correct |
458 ms |
27424 KB |
Output is correct |
62 |
Correct |
379 ms |
34552 KB |
Output is correct |
63 |
Correct |
440 ms |
26308 KB |
Output is correct |
64 |
Correct |
458 ms |
27624 KB |
Output is correct |
65 |
Correct |
454 ms |
26088 KB |
Output is correct |
66 |
Correct |
464 ms |
27492 KB |
Output is correct |
67 |
Correct |
452 ms |
26216 KB |
Output is correct |
68 |
Correct |
459 ms |
27984 KB |
Output is correct |
69 |
Correct |
458 ms |
29108 KB |
Output is correct |
70 |
Correct |
438 ms |
26852 KB |
Output is correct |
71 |
Correct |
437 ms |
27680 KB |
Output is correct |
72 |
Correct |
406 ms |
30108 KB |
Output is correct |
73 |
Correct |
443 ms |
33408 KB |
Output is correct |
74 |
Correct |
325 ms |
34204 KB |
Output is correct |