#include<bits/stdc++.h>
#define int long long
#define f first
#define s second
#define pii pair<int,int>
#define Pii pair<int,pii>
using namespace std;
const int N = 2e5 + 5, mod = 1e9 + 7; // !
int t, h[N], g[N], ans[N], sz[N], n, vis[N], f[N], all[N], oneway;
pii par[N];
vector<pii> x;
vector<Pii> V[N];
void dfs(int u,int p,int s) {
g[u] = s;
x.push_back({h[u], u});
for(int i = 0; i < V[u].size(); i++) {
int v = V[u][i].f;
if(v== p || f[v]) continue;
h[v] = h[u] + V[u][i].s.f;
par[v] = {u, V[u][i].s.f};
dfs(v, u, s);
}
}
void dfs(int u,int p) {
sz[u] = 0;
for(int i = 0; i < V[u].size(); i++) {
int v = V[u][i].f;
if(v== p || f[v]) continue;
dfs(v, u);
sz[u] += sz[v];
}
sz[u]++;
}
int find(int u,int p, int Sz) {
for(int i = 0; i < V[u].size(); i++) {
int v = V[u][i].f;
if(v== p || f[v]) continue;
if(sz[v] > Sz/2) return find(v, u, Sz);
}
return u;
}
int up(int u,int c) {
int dep = 0;
while(!(vis[u] == c)) {
dep += par[u].s;
vis[u] = c;
u = par[u].f;
}
return dep;
}
void decomp(int U) {
dfs(U, 0);
int c = find(U, 0, sz[U]);
f[c] = 1;
g[c] = 0;
x.clear();
for(int i = 0; i < V[c].size(); i++) {
int v = V[c][i].f;
if(f[v]) continue;
h[v] = V[c][i].s.f;
dfs(v, c, v);
par[v] = {h[v], c};
swap(par[v].f, par[v].s);
}
x.push_back({0, c});
sort(x.rbegin(), x.rend());
vector<pii> y;
vis[c] = c;
for(int i = 0; i < x.size(); i++) {
int u = x[i].s;
y.push_back({up(u, c), u});
}
sort(y.rbegin(), y.rend());
int id = 0;
for(int i = 0; i < y.size(); i++) {
if(g[y[i].s] != g[y[0].s]) {
id = i;
break;
}
}
ans[2] = max(ans[2], all[c] + y[0].f + y[id].f);
int p = all[c] + y[0].f + y[id].f, cnt = 2;
for(int i = 1; i < y.size(); i++) {
if(i == id) continue;
cnt++;
p += y[i].f;
ans[cnt] = max(ans[cnt], p);
}
// return;
for(int i = 0; i < V[c].size(); i++) {
if(!f[V[c][i].f]) decomp(V[c][i].f);
}
}
void dfs1(int u,int p) {
for(int i = 0; i < V[u].size(); i++) {
int v = V[u][i].f;
if(v == p) continue;
dfs1(v, u);
oneway += V[u][i].s.s;
}
}
void dfs2(int u,int p) {
all[u] = oneway;
ans[1] = max(ans[1], oneway);
for(int i = 0; i < V[u].size(); i++) {
int v = V[u][i].f;
if(v == p) continue;
oneway -= V[u][i].s.s;
oneway += V[u][i].s.f;
dfs2(v, u);
oneway -= -V[u][i].s.s;
oneway += -V[u][i].s.f;
}
}
main(){
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin >> n;
int MS = 0;
for(int i = 2; i <= n; i++) {
int u,v,w ,a , b;
cin >> u >> v >> a >> b;
V[u].push_back({v, {a, b}});
V[v].push_back({u, {b, a}});
MS += a + b;
}
dfs1(1, 0);
dfs2(1, 0);
decomp(1);
int q; cin >> q;
while(q--) {
int c; cin >> c;
cout << MS - ans[c] << " ";
}
}
Compilation message
designated_cities.cpp: In function 'void dfs(long long int, long long int, long long int)':
designated_cities.cpp:16:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | for(int i = 0; i < V[u].size(); i++) {
| ~~^~~~~~~~~~~~~
designated_cities.cpp: In function 'void dfs(long long int, long long int)':
designated_cities.cpp:26:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for(int i = 0; i < V[u].size(); i++) {
| ~~^~~~~~~~~~~~~
designated_cities.cpp: In function 'long long int find(long long int, long long int, long long int)':
designated_cities.cpp:35:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for(int i = 0; i < V[u].size(); i++) {
| ~~^~~~~~~~~~~~~
designated_cities.cpp: In function 'void decomp(long long int)':
designated_cities.cpp:58:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for(int i = 0; i < V[c].size(); i++) {
| ~~^~~~~~~~~~~~~
designated_cities.cpp:70:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for(int i = 0; i < x.size(); i++) {
| ~~^~~~~~~~~~
designated_cities.cpp:76:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | for(int i = 0; i < y.size(); i++) {
| ~~^~~~~~~~~~
designated_cities.cpp:84:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for(int i = 1; i < y.size(); i++) {
| ~~^~~~~~~~~~
designated_cities.cpp:91:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | for(int i = 0; i < V[c].size(); i++) {
| ~~^~~~~~~~~~~~~
designated_cities.cpp: In function 'void dfs1(long long int, long long int)':
designated_cities.cpp:96:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
96 | for(int i = 0; i < V[u].size(); i++) {
| ~~^~~~~~~~~~~~~
designated_cities.cpp: In function 'void dfs2(long long int, long long int)':
designated_cities.cpp:106:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
106 | for(int i = 0; i < V[u].size(); i++) {
| ~~^~~~~~~~~~~~~
designated_cities.cpp: At global scope:
designated_cities.cpp:116:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
116 | main(){
| ^~~~
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:121:11: warning: unused variable 'w' [-Wunused-variable]
121 | int u,v,w ,a , b;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5008 KB |
Output is correct |
2 |
Correct |
3 ms |
5004 KB |
Output is correct |
3 |
Correct |
3 ms |
5024 KB |
Output is correct |
4 |
Correct |
3 ms |
5068 KB |
Output is correct |
5 |
Correct |
3 ms |
5020 KB |
Output is correct |
6 |
Correct |
3 ms |
5068 KB |
Output is correct |
7 |
Correct |
2 ms |
5068 KB |
Output is correct |
8 |
Correct |
4 ms |
5184 KB |
Output is correct |
9 |
Correct |
3 ms |
5016 KB |
Output is correct |
10 |
Correct |
2 ms |
5068 KB |
Output is correct |
11 |
Correct |
3 ms |
5068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Correct |
952 ms |
48168 KB |
Output is correct |
3 |
Correct |
1562 ms |
59424 KB |
Output is correct |
4 |
Correct |
1086 ms |
47096 KB |
Output is correct |
5 |
Correct |
424 ms |
44932 KB |
Output is correct |
6 |
Correct |
1399 ms |
50064 KB |
Output is correct |
7 |
Correct |
372 ms |
43752 KB |
Output is correct |
8 |
Correct |
1625 ms |
59088 KB |
Output is correct |
9 |
Correct |
221 ms |
46852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5068 KB |
Output is correct |
2 |
Correct |
992 ms |
48436 KB |
Output is correct |
3 |
Correct |
1489 ms |
60456 KB |
Output is correct |
4 |
Correct |
997 ms |
47140 KB |
Output is correct |
5 |
Correct |
406 ms |
44808 KB |
Output is correct |
6 |
Correct |
1230 ms |
50492 KB |
Output is correct |
7 |
Correct |
253 ms |
46976 KB |
Output is correct |
8 |
Correct |
1373 ms |
55940 KB |
Output is correct |
9 |
Correct |
202 ms |
46436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5008 KB |
Output is correct |
2 |
Correct |
3 ms |
5004 KB |
Output is correct |
3 |
Correct |
3 ms |
5024 KB |
Output is correct |
4 |
Correct |
3 ms |
5068 KB |
Output is correct |
5 |
Correct |
3 ms |
5020 KB |
Output is correct |
6 |
Correct |
3 ms |
5068 KB |
Output is correct |
7 |
Correct |
2 ms |
5068 KB |
Output is correct |
8 |
Correct |
4 ms |
5184 KB |
Output is correct |
9 |
Correct |
3 ms |
5016 KB |
Output is correct |
10 |
Correct |
2 ms |
5068 KB |
Output is correct |
11 |
Correct |
3 ms |
5068 KB |
Output is correct |
12 |
Correct |
2 ms |
5068 KB |
Output is correct |
13 |
Correct |
6 ms |
5452 KB |
Output is correct |
14 |
Correct |
6 ms |
5580 KB |
Output is correct |
15 |
Correct |
7 ms |
5580 KB |
Output is correct |
16 |
Correct |
7 ms |
5412 KB |
Output is correct |
17 |
Correct |
6 ms |
5452 KB |
Output is correct |
18 |
Correct |
6 ms |
5412 KB |
Output is correct |
19 |
Correct |
8 ms |
5544 KB |
Output is correct |
20 |
Correct |
5 ms |
5460 KB |
Output is correct |
21 |
Correct |
6 ms |
5452 KB |
Output is correct |
22 |
Correct |
7 ms |
5452 KB |
Output is correct |
23 |
Correct |
6 ms |
5452 KB |
Output is correct |
24 |
Correct |
4 ms |
5488 KB |
Output is correct |
25 |
Correct |
7 ms |
5580 KB |
Output is correct |
26 |
Correct |
4 ms |
5452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Correct |
952 ms |
48168 KB |
Output is correct |
3 |
Correct |
1562 ms |
59424 KB |
Output is correct |
4 |
Correct |
1086 ms |
47096 KB |
Output is correct |
5 |
Correct |
424 ms |
44932 KB |
Output is correct |
6 |
Correct |
1399 ms |
50064 KB |
Output is correct |
7 |
Correct |
372 ms |
43752 KB |
Output is correct |
8 |
Correct |
1625 ms |
59088 KB |
Output is correct |
9 |
Correct |
221 ms |
46852 KB |
Output is correct |
10 |
Correct |
4 ms |
5068 KB |
Output is correct |
11 |
Correct |
992 ms |
48436 KB |
Output is correct |
12 |
Correct |
1489 ms |
60456 KB |
Output is correct |
13 |
Correct |
997 ms |
47140 KB |
Output is correct |
14 |
Correct |
406 ms |
44808 KB |
Output is correct |
15 |
Correct |
1230 ms |
50492 KB |
Output is correct |
16 |
Correct |
253 ms |
46976 KB |
Output is correct |
17 |
Correct |
1373 ms |
55940 KB |
Output is correct |
18 |
Correct |
202 ms |
46436 KB |
Output is correct |
19 |
Correct |
3 ms |
5012 KB |
Output is correct |
20 |
Correct |
991 ms |
48328 KB |
Output is correct |
21 |
Correct |
1465 ms |
60584 KB |
Output is correct |
22 |
Correct |
979 ms |
47120 KB |
Output is correct |
23 |
Correct |
1037 ms |
48700 KB |
Output is correct |
24 |
Correct |
1010 ms |
47652 KB |
Output is correct |
25 |
Correct |
976 ms |
48764 KB |
Output is correct |
26 |
Correct |
948 ms |
47548 KB |
Output is correct |
27 |
Correct |
410 ms |
44700 KB |
Output is correct |
28 |
Correct |
1175 ms |
50184 KB |
Output is correct |
29 |
Correct |
958 ms |
48800 KB |
Output is correct |
30 |
Correct |
834 ms |
46996 KB |
Output is correct |
31 |
Correct |
295 ms |
43572 KB |
Output is correct |
32 |
Correct |
1346 ms |
56268 KB |
Output is correct |
33 |
Correct |
221 ms |
47004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5008 KB |
Output is correct |
2 |
Correct |
3 ms |
5004 KB |
Output is correct |
3 |
Correct |
3 ms |
5024 KB |
Output is correct |
4 |
Correct |
3 ms |
5068 KB |
Output is correct |
5 |
Correct |
3 ms |
5020 KB |
Output is correct |
6 |
Correct |
3 ms |
5068 KB |
Output is correct |
7 |
Correct |
2 ms |
5068 KB |
Output is correct |
8 |
Correct |
4 ms |
5184 KB |
Output is correct |
9 |
Correct |
3 ms |
5016 KB |
Output is correct |
10 |
Correct |
2 ms |
5068 KB |
Output is correct |
11 |
Correct |
3 ms |
5068 KB |
Output is correct |
12 |
Correct |
3 ms |
5068 KB |
Output is correct |
13 |
Correct |
952 ms |
48168 KB |
Output is correct |
14 |
Correct |
1562 ms |
59424 KB |
Output is correct |
15 |
Correct |
1086 ms |
47096 KB |
Output is correct |
16 |
Correct |
424 ms |
44932 KB |
Output is correct |
17 |
Correct |
1399 ms |
50064 KB |
Output is correct |
18 |
Correct |
372 ms |
43752 KB |
Output is correct |
19 |
Correct |
1625 ms |
59088 KB |
Output is correct |
20 |
Correct |
221 ms |
46852 KB |
Output is correct |
21 |
Correct |
4 ms |
5068 KB |
Output is correct |
22 |
Correct |
992 ms |
48436 KB |
Output is correct |
23 |
Correct |
1489 ms |
60456 KB |
Output is correct |
24 |
Correct |
997 ms |
47140 KB |
Output is correct |
25 |
Correct |
406 ms |
44808 KB |
Output is correct |
26 |
Correct |
1230 ms |
50492 KB |
Output is correct |
27 |
Correct |
253 ms |
46976 KB |
Output is correct |
28 |
Correct |
1373 ms |
55940 KB |
Output is correct |
29 |
Correct |
202 ms |
46436 KB |
Output is correct |
30 |
Correct |
2 ms |
5068 KB |
Output is correct |
31 |
Correct |
6 ms |
5452 KB |
Output is correct |
32 |
Correct |
6 ms |
5580 KB |
Output is correct |
33 |
Correct |
7 ms |
5580 KB |
Output is correct |
34 |
Correct |
7 ms |
5412 KB |
Output is correct |
35 |
Correct |
6 ms |
5452 KB |
Output is correct |
36 |
Correct |
6 ms |
5412 KB |
Output is correct |
37 |
Correct |
8 ms |
5544 KB |
Output is correct |
38 |
Correct |
5 ms |
5460 KB |
Output is correct |
39 |
Correct |
6 ms |
5452 KB |
Output is correct |
40 |
Correct |
7 ms |
5452 KB |
Output is correct |
41 |
Correct |
6 ms |
5452 KB |
Output is correct |
42 |
Correct |
4 ms |
5488 KB |
Output is correct |
43 |
Correct |
7 ms |
5580 KB |
Output is correct |
44 |
Correct |
4 ms |
5452 KB |
Output is correct |
45 |
Correct |
3 ms |
5012 KB |
Output is correct |
46 |
Correct |
991 ms |
48328 KB |
Output is correct |
47 |
Correct |
1465 ms |
60584 KB |
Output is correct |
48 |
Correct |
979 ms |
47120 KB |
Output is correct |
49 |
Correct |
1037 ms |
48700 KB |
Output is correct |
50 |
Correct |
1010 ms |
47652 KB |
Output is correct |
51 |
Correct |
976 ms |
48764 KB |
Output is correct |
52 |
Correct |
948 ms |
47548 KB |
Output is correct |
53 |
Correct |
410 ms |
44700 KB |
Output is correct |
54 |
Correct |
1175 ms |
50184 KB |
Output is correct |
55 |
Correct |
958 ms |
48800 KB |
Output is correct |
56 |
Correct |
834 ms |
46996 KB |
Output is correct |
57 |
Correct |
295 ms |
43572 KB |
Output is correct |
58 |
Correct |
1346 ms |
56268 KB |
Output is correct |
59 |
Correct |
221 ms |
47004 KB |
Output is correct |
60 |
Correct |
2 ms |
5016 KB |
Output is correct |
61 |
Correct |
979 ms |
48456 KB |
Output is correct |
62 |
Correct |
1465 ms |
59724 KB |
Output is correct |
63 |
Correct |
972 ms |
47208 KB |
Output is correct |
64 |
Correct |
936 ms |
48808 KB |
Output is correct |
65 |
Correct |
966 ms |
47720 KB |
Output is correct |
66 |
Correct |
1014 ms |
49132 KB |
Output is correct |
67 |
Correct |
1010 ms |
47840 KB |
Output is correct |
68 |
Correct |
448 ms |
45120 KB |
Output is correct |
69 |
Correct |
1290 ms |
51220 KB |
Output is correct |
70 |
Correct |
1063 ms |
49420 KB |
Output is correct |
71 |
Correct |
883 ms |
46892 KB |
Output is correct |
72 |
Correct |
348 ms |
43920 KB |
Output is correct |
73 |
Correct |
1417 ms |
56400 KB |
Output is correct |
74 |
Correct |
284 ms |
47844 KB |
Output is correct |