#include <bits/stdc++.h>
using namespace std;
const int N = 200001;
const long long inf = 1000000000000000ll;
struct edge{
int to,nxt;
long long w;
}tree[N << 1];
int n,q,head[N],cnt = 0;
void addedge(int u,int v,long long w){
edge x = {v,head[u],w};
tree[head[u] = cnt++] = x;
}
template<class T>
void read(T &x){
int sgn = 1;
char ch;
x = 0;
for(ch = getchar();(ch < '0' || ch > '9') && ch != '-';ch = getchar());
if(ch == '-')ch = getchar(),sgn = -1;
for(;'0' <= ch && ch <= '9';ch = getchar())x = x * 10 + ch - '0';
x *= sgn;
}
template<class T>
void write(T x){
if(x < 0)putchar('-'),write(-x);
else if(x < 10)putchar(x + '0');
else write(x / 10),putchar(x % 10 + '0');
}
long long ans[N],depth[N],sz[N];
bool visited[N];
int size[N],child[N];
int find_centroid(int u,int fa,int total,int &weight,int &pos){
int w = total - size[u];
for(int i = head[u];i >= 0;i = tree[i].nxt){
int v = tree[i].to;
if(!visited[v] && v != fa){
find_centroid(v,u,total,weight,pos);
w = max(w,size[v]);
}
}
if(w < weight)weight = w,pos = u;
}
void dfs1(int u,int edge){
sz[u] = depth[u] = tree[edge ^ 1].w,child[u] = 0,size[u] = 1;
for(int i = head[u];i >= 0;i = tree[i].nxt){
int v = tree[i].to;
if(!visited[v] && i != edge){
dfs1(v,i ^ 1);
size[u] += size[v],sz[u] += sz[v];
if(!child[u] || depth[child[u]] < depth[v]){
child[u] = v;
depth[u] = tree[edge ^ 1].w + depth[child[u]];
}
}
}
}
struct node{
int belong;
long long d;
bool operator < (node other) const{
return d > other.d;
}
}chain[N];
int tot = 0;
void dfs2(int u,int fa,int root,int t){
if(u == t){
node x = {root,depth[u]};
chain[++tot] = x;
}
if(child[u])dfs2(child[u],u,root,t);
for(int i = head[u];i >= 0;i = tree[i].nxt){
int v = tree[i].to;
if(!visited[v] && v != fa && v != child[u])dfs2(v,u,root,v);
}
}
void divide_and_conquer(int u,long long w){
visited[u] = true,sz[u] = 0ll,size[u] = 1;
node x = {u,0ll};
chain[tot = 0] = x;
for(int i = head[u];i >= 0;i = tree[i].nxt){
int v = tree[i].to;
if(!visited[v]){
dfs1(v,i ^ 1);
sz[u] += sz[v],size[u] += size[v];
dfs2(v,u,v,v);
}
}
sort(chain,chain + tot + 1);
int p = 0;
for(int i = 1;i <= tot;i++){
if(chain[i].belong != chain[0].belong){
p = i;
break;
}
}
long long total = 0ll;
ans[1] = min(ans[1],sz[u] + w);
for(int i = 0;i < p;i++){
if(i >= 0)total += chain[i].d;
ans[i + 2] = min(ans[i + 2],sz[u] - total - chain[p].d + w);
}
for(int i = p;i <= tot;i++){
total += chain[i].d;
ans[i + 1] = min(ans[i + 1],sz[u] - total + w);
}
for(int i = tot + 2;i <= size[u];i++)ans[i] = min(ans[i],sz[u] - total + w);
for(int i = head[u];i >= 0;i = tree[i].nxt){
int v = tree[i].to;
if(!visited[v]){
int weight = n,pos = v;
find_centroid(v,u,size[v],weight,pos);
divide_and_conquer(pos,w + sz[u] - sz[v] + tree[i ^ 1].w);
}
}
visited[u] = false;
}
int main(){
read(n);
for(int i = 1;i <= n;i++)head[i] = -1;
for(int i = 1;i < n;i++){
int u,v;
long long w1,w2;
read(u),read(v);
read(w1),read(w2);
addedge(u,v,w1);
addedge(v,u,w2);
}
for(int i = 1;i <= n;i++)ans[i] = inf;
divide_and_conquer(1,0ll);
read(q);
for(int i = 1;i <= q;i++){
int x;
read(x);
write(ans[x]),putchar('\n');
}
return 0;
}
Compilation message
designated_cities.cpp: In function 'int find_centroid(int, int, int, int&, int&)':
designated_cities.cpp:43:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
815 ms |
15160 KB |
Output is correct |
3 |
Correct |
1119 ms |
26744 KB |
Output is correct |
4 |
Correct |
918 ms |
19960 KB |
Output is correct |
5 |
Correct |
339 ms |
21660 KB |
Output is correct |
6 |
Correct |
930 ms |
23104 KB |
Output is correct |
7 |
Correct |
308 ms |
22236 KB |
Output is correct |
8 |
Correct |
1231 ms |
33308 KB |
Output is correct |
9 |
Correct |
262 ms |
23416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
959 ms |
15352 KB |
Output is correct |
3 |
Correct |
1291 ms |
28728 KB |
Output is correct |
4 |
Correct |
857 ms |
20248 KB |
Output is correct |
5 |
Correct |
342 ms |
21640 KB |
Output is correct |
6 |
Correct |
1087 ms |
23544 KB |
Output is correct |
7 |
Correct |
252 ms |
22892 KB |
Output is correct |
8 |
Correct |
1320 ms |
29092 KB |
Output is correct |
9 |
Correct |
230 ms |
23180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
4 ms |
640 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
6 ms |
512 KB |
Output is correct |
17 |
Correct |
5 ms |
512 KB |
Output is correct |
18 |
Correct |
6 ms |
512 KB |
Output is correct |
19 |
Correct |
5 ms |
640 KB |
Output is correct |
20 |
Correct |
3 ms |
512 KB |
Output is correct |
21 |
Correct |
5 ms |
512 KB |
Output is correct |
22 |
Correct |
4 ms |
512 KB |
Output is correct |
23 |
Correct |
5 ms |
512 KB |
Output is correct |
24 |
Correct |
4 ms |
640 KB |
Output is correct |
25 |
Correct |
5 ms |
640 KB |
Output is correct |
26 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
815 ms |
15160 KB |
Output is correct |
3 |
Correct |
1119 ms |
26744 KB |
Output is correct |
4 |
Correct |
918 ms |
19960 KB |
Output is correct |
5 |
Correct |
339 ms |
21660 KB |
Output is correct |
6 |
Correct |
930 ms |
23104 KB |
Output is correct |
7 |
Correct |
308 ms |
22236 KB |
Output is correct |
8 |
Correct |
1231 ms |
33308 KB |
Output is correct |
9 |
Correct |
262 ms |
23416 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
959 ms |
15352 KB |
Output is correct |
12 |
Correct |
1291 ms |
28728 KB |
Output is correct |
13 |
Correct |
857 ms |
20248 KB |
Output is correct |
14 |
Correct |
342 ms |
21640 KB |
Output is correct |
15 |
Correct |
1087 ms |
23544 KB |
Output is correct |
16 |
Correct |
252 ms |
22892 KB |
Output is correct |
17 |
Correct |
1320 ms |
29092 KB |
Output is correct |
18 |
Correct |
230 ms |
23180 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
855 ms |
21544 KB |
Output is correct |
21 |
Correct |
1169 ms |
35292 KB |
Output is correct |
22 |
Correct |
922 ms |
20204 KB |
Output is correct |
23 |
Correct |
962 ms |
21496 KB |
Output is correct |
24 |
Correct |
1039 ms |
20472 KB |
Output is correct |
25 |
Correct |
931 ms |
21700 KB |
Output is correct |
26 |
Correct |
765 ms |
20444 KB |
Output is correct |
27 |
Correct |
298 ms |
21712 KB |
Output is correct |
28 |
Correct |
964 ms |
23248 KB |
Output is correct |
29 |
Correct |
721 ms |
21452 KB |
Output is correct |
30 |
Correct |
647 ms |
20984 KB |
Output is correct |
31 |
Correct |
272 ms |
22392 KB |
Output is correct |
32 |
Correct |
1012 ms |
29688 KB |
Output is correct |
33 |
Correct |
230 ms |
23408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
815 ms |
15160 KB |
Output is correct |
14 |
Correct |
1119 ms |
26744 KB |
Output is correct |
15 |
Correct |
918 ms |
19960 KB |
Output is correct |
16 |
Correct |
339 ms |
21660 KB |
Output is correct |
17 |
Correct |
930 ms |
23104 KB |
Output is correct |
18 |
Correct |
308 ms |
22236 KB |
Output is correct |
19 |
Correct |
1231 ms |
33308 KB |
Output is correct |
20 |
Correct |
262 ms |
23416 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
959 ms |
15352 KB |
Output is correct |
23 |
Correct |
1291 ms |
28728 KB |
Output is correct |
24 |
Correct |
857 ms |
20248 KB |
Output is correct |
25 |
Correct |
342 ms |
21640 KB |
Output is correct |
26 |
Correct |
1087 ms |
23544 KB |
Output is correct |
27 |
Correct |
252 ms |
22892 KB |
Output is correct |
28 |
Correct |
1320 ms |
29092 KB |
Output is correct |
29 |
Correct |
230 ms |
23180 KB |
Output is correct |
30 |
Correct |
2 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
512 KB |
Output is correct |
32 |
Correct |
4 ms |
640 KB |
Output is correct |
33 |
Correct |
5 ms |
512 KB |
Output is correct |
34 |
Correct |
6 ms |
512 KB |
Output is correct |
35 |
Correct |
5 ms |
512 KB |
Output is correct |
36 |
Correct |
6 ms |
512 KB |
Output is correct |
37 |
Correct |
5 ms |
640 KB |
Output is correct |
38 |
Correct |
3 ms |
512 KB |
Output is correct |
39 |
Correct |
5 ms |
512 KB |
Output is correct |
40 |
Correct |
4 ms |
512 KB |
Output is correct |
41 |
Correct |
5 ms |
512 KB |
Output is correct |
42 |
Correct |
4 ms |
640 KB |
Output is correct |
43 |
Correct |
5 ms |
640 KB |
Output is correct |
44 |
Correct |
5 ms |
512 KB |
Output is correct |
45 |
Correct |
2 ms |
384 KB |
Output is correct |
46 |
Correct |
855 ms |
21544 KB |
Output is correct |
47 |
Correct |
1169 ms |
35292 KB |
Output is correct |
48 |
Correct |
922 ms |
20204 KB |
Output is correct |
49 |
Correct |
962 ms |
21496 KB |
Output is correct |
50 |
Correct |
1039 ms |
20472 KB |
Output is correct |
51 |
Correct |
931 ms |
21700 KB |
Output is correct |
52 |
Correct |
765 ms |
20444 KB |
Output is correct |
53 |
Correct |
298 ms |
21712 KB |
Output is correct |
54 |
Correct |
964 ms |
23248 KB |
Output is correct |
55 |
Correct |
721 ms |
21452 KB |
Output is correct |
56 |
Correct |
647 ms |
20984 KB |
Output is correct |
57 |
Correct |
272 ms |
22392 KB |
Output is correct |
58 |
Correct |
1012 ms |
29688 KB |
Output is correct |
59 |
Correct |
230 ms |
23408 KB |
Output is correct |
60 |
Correct |
2 ms |
384 KB |
Output is correct |
61 |
Correct |
924 ms |
24160 KB |
Output is correct |
62 |
Correct |
1082 ms |
34680 KB |
Output is correct |
63 |
Correct |
723 ms |
22776 KB |
Output is correct |
64 |
Correct |
821 ms |
24156 KB |
Output is correct |
65 |
Correct |
776 ms |
22776 KB |
Output is correct |
66 |
Correct |
882 ms |
24184 KB |
Output is correct |
67 |
Correct |
821 ms |
22904 KB |
Output is correct |
68 |
Correct |
384 ms |
24312 KB |
Output is correct |
69 |
Correct |
1317 ms |
25796 KB |
Output is correct |
70 |
Correct |
917 ms |
24264 KB |
Output is correct |
71 |
Correct |
785 ms |
23560 KB |
Output is correct |
72 |
Correct |
299 ms |
25564 KB |
Output is correct |
73 |
Correct |
1217 ms |
30684 KB |
Output is correct |
74 |
Correct |
287 ms |
27568 KB |
Output is correct |