#include<bits/stdc++.h>
#define pb push_back
#define ii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define INF 100000000000000000
#define modulo 1000000007
#define mod 998244353
#define int long long int
using namespace std;
vector<int> adj[200005];
vector<int> val(200005);
vector<int> Ans(200005, 0);
vector<int> sub(200005, 1);
vector<int> anc(200005, 0);
vector<pair<ii, int> > V(200005);
vector<int> P(200005, 0);
vector<bool> mark(200005, false);
vector<bool> vis(200005, false);
unordered_map<int, int> cost;
ii Q = {-INF, -1};
int S = 0;
int _hash(ii a){
return a.first * 1e6 + a.second;
}
void dfs1(int x, int pre, int& v){
for(int i = 0; i < adj[x].size(); i++){
int y = adj[x][i];
if(y == pre) continue;
v += cost[_hash({y, x})];
dfs1(y, x, v);
}
}
void dfs2(int x, int pre, int v){
val[x] = v;
for(int i = 0; i < adj[x].size(); i++){
int y = adj[x][i];
if(y == pre) continue;
dfs2(y, x, v - cost[_hash({y, x})] + cost[_hash({x, y})]);
}
}
void dfs3(int x, int pre){
sub[x] = 1;
for(int i = 0; i < adj[x].size(); i++){
int y = adj[x][i];
if(y == pre || mark[y]) continue;
dfs3(y, x);
sub[x] += sub[y];
}
}
void dfs4(int x, int pre, int sum, ii& mx){
if(sum > mx.first){
mx = {sum, x};
}
for(int i = 0; i < adj[x].size(); i++){
int y = adj[x][i];
if(y == pre || mark[y]) continue;
dfs4(y, x, sum + cost[_hash({x, y})], mx);
}
}
void dfs5(int x, int pre){
for(int i = 0; i < adj[x].size(); i++){
int y = adj[x][i];
if(y == pre) continue;
dfs5(y, x);
anc[y] = x;
}
}
void dfs6(int x, int pre, int value){
for(auto y : adj[x]){
if(vis[y] || pre == y)continue;
dfs6(y, x, value + cost[_hash({x, y})]);
}
if(adj[x].size() == 1 && pre != -1){
if(Q.first < value){
Q = {value, x};
}
}
}
int find_centroid(int x, int pre, int sum){
for(int i = 0; i < adj[x].size(); i++){
int y = adj[x][i];
if(y == pre || mark[y])continue;
if(sub[y] > sum / 2)return find_centroid(y, x, sum);
}
return x;
}
void solve(int x){
dfs3(x, -1);
x = find_centroid(x, -1, sub[x]);
ii mx = {0, x};
pair <ii, int> ans = {{x, x}, 0};
for(int i = 0; i < adj[x].size(); i++){
int y = adj[x][i];
if(mark[y])continue;
ii temp = {cost[_hash({x, y})], y};
dfs4(y, x, cost[_hash({x, y})], temp);
if(ans.second < mx.first + temp.first){
ans.first = {mx.second, temp.second};
ans.second = mx.first + temp.first;
}
if(temp.first > mx.first){
swap(temp, mx);
}
}
V[x] = ans;
mark[x] = true;
for(int i = 0; i < adj[x].size(); i++){
int y =adj[x][i];
if(mark[y])continue;
solve(y);
}
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("q.gir","r",stdin);
// freopen("q.cik","w",stdout);
int n;
cin >> n;
for(int i = 0; i < n - 1; i++){
int x, y, a, b;
cin >> x >> y >> a >> b;
adj[x].pb(y);
adj[y].pb(x);
cost[_hash({x, y})] = a;
cost[_hash({y, x})] = b;
S += a + b;
}
int w = 0;
dfs1(1, 0, w);
dfs2(1, 0, w);
Ans[1] = S - *max_element(all(val));
solve(1);
Ans[2] = INF;
int X, Y;
for(int i = 1; i <= n; i++){
if((S - val[i]) - V[i].second < Ans[2]){
X = V[i].first.first;
Y = V[i].first.second;
Ans[2] = (S - val[i]) - V[i].second;
}
}
if(n <= 2001){
dfs5(X, 0);
int t = Y;
while(t != X){
vis[t] = true;
t = anc[t];
}
vis[X] = true;
for(int i = 1; i <= n; i++){
if(vis[i]) dfs6(i, -1, 0);
}
for(int q = 3; Ans[q - 1] != 0; q++){
Ans[q] = Ans[q - 1] - Q.first;
int x = Q.second;
Q = {-INF, -1};
t = x;
while(!vis[t]){
vis[t] = true;
t = anc[t];
}
for(int i = 1; i <= n; i++){
if(vis[i]) dfs6(i, -1, 0);
}
}
}
int q;
cin >> q;
while(q--){
int k;
cin >> k;
cout << Ans[k] << "\n";
}
}
Compilation message
designated_cities.cpp: In function 'void dfs1(long long int, long long int, long long int&)':
designated_cities.cpp:27:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < adj[x].size(); i++){
~~^~~~~~~~~~~~~~~
designated_cities.cpp: In function 'void dfs2(long long int, long long int, long long int)':
designated_cities.cpp:36:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < adj[x].size(); i++){
~~^~~~~~~~~~~~~~~
designated_cities.cpp: In function 'void dfs3(long long int, long long int)':
designated_cities.cpp:44:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < adj[x].size(); i++){
~~^~~~~~~~~~~~~~~
designated_cities.cpp: In function 'void dfs4(long long int, long long int, long long int, std::pair<long long int, long long int>&)':
designated_cities.cpp:55:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < adj[x].size(); i++){
~~^~~~~~~~~~~~~~~
designated_cities.cpp: In function 'void dfs5(long long int, long long int)':
designated_cities.cpp:62:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < adj[x].size(); i++){
~~^~~~~~~~~~~~~~~
designated_cities.cpp: In function 'long long int find_centroid(long long int, long long int, long long int)':
designated_cities.cpp:81:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < adj[x].size(); i++){
~~^~~~~~~~~~~~~~~
designated_cities.cpp: In function 'void solve(long long int)':
designated_cities.cpp:93:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < adj[x].size(); i++){
~~^~~~~~~~~~~~~~~
designated_cities.cpp:108:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < adj[x].size(); i++){
~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
17656 KB |
Output is correct |
2 |
Correct |
19 ms |
17656 KB |
Output is correct |
3 |
Correct |
15 ms |
17660 KB |
Output is correct |
4 |
Correct |
15 ms |
17656 KB |
Output is correct |
5 |
Correct |
15 ms |
17656 KB |
Output is correct |
6 |
Correct |
15 ms |
17656 KB |
Output is correct |
7 |
Correct |
15 ms |
17660 KB |
Output is correct |
8 |
Correct |
15 ms |
17656 KB |
Output is correct |
9 |
Correct |
15 ms |
17656 KB |
Output is correct |
10 |
Correct |
15 ms |
17656 KB |
Output is correct |
11 |
Correct |
15 ms |
17656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
17656 KB |
Output is correct |
2 |
Correct |
1194 ms |
40968 KB |
Output is correct |
3 |
Correct |
1795 ms |
60300 KB |
Output is correct |
4 |
Correct |
1221 ms |
41156 KB |
Output is correct |
5 |
Correct |
808 ms |
41700 KB |
Output is correct |
6 |
Correct |
1435 ms |
50232 KB |
Output is correct |
7 |
Correct |
681 ms |
47628 KB |
Output is correct |
8 |
Correct |
1806 ms |
67488 KB |
Output is correct |
9 |
Correct |
486 ms |
47992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
17656 KB |
Output is correct |
2 |
Correct |
1376 ms |
41100 KB |
Output is correct |
3 |
Correct |
1972 ms |
63624 KB |
Output is correct |
4 |
Correct |
1379 ms |
40968 KB |
Output is correct |
5 |
Correct |
877 ms |
40844 KB |
Output is correct |
6 |
Correct |
1657 ms |
44168 KB |
Output is correct |
7 |
Correct |
550 ms |
41464 KB |
Output is correct |
8 |
Correct |
1878 ms |
53344 KB |
Output is correct |
9 |
Correct |
506 ms |
47612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
17656 KB |
Output is correct |
2 |
Correct |
19 ms |
17656 KB |
Output is correct |
3 |
Correct |
15 ms |
17660 KB |
Output is correct |
4 |
Correct |
15 ms |
17656 KB |
Output is correct |
5 |
Correct |
15 ms |
17656 KB |
Output is correct |
6 |
Correct |
15 ms |
17656 KB |
Output is correct |
7 |
Correct |
15 ms |
17660 KB |
Output is correct |
8 |
Correct |
15 ms |
17656 KB |
Output is correct |
9 |
Correct |
15 ms |
17656 KB |
Output is correct |
10 |
Correct |
15 ms |
17656 KB |
Output is correct |
11 |
Correct |
15 ms |
17656 KB |
Output is correct |
12 |
Correct |
15 ms |
17656 KB |
Output is correct |
13 |
Correct |
54 ms |
17912 KB |
Output is correct |
14 |
Correct |
19 ms |
18040 KB |
Output is correct |
15 |
Correct |
64 ms |
17912 KB |
Output is correct |
16 |
Correct |
56 ms |
17912 KB |
Output is correct |
17 |
Correct |
55 ms |
17912 KB |
Output is correct |
18 |
Correct |
59 ms |
17912 KB |
Output is correct |
19 |
Correct |
58 ms |
17912 KB |
Output is correct |
20 |
Correct |
62 ms |
17964 KB |
Output is correct |
21 |
Correct |
58 ms |
17912 KB |
Output is correct |
22 |
Correct |
52 ms |
17912 KB |
Output is correct |
23 |
Correct |
80 ms |
18040 KB |
Output is correct |
24 |
Correct |
94 ms |
17912 KB |
Output is correct |
25 |
Correct |
25 ms |
18040 KB |
Output is correct |
26 |
Correct |
101 ms |
17944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
17656 KB |
Output is correct |
2 |
Correct |
1194 ms |
40968 KB |
Output is correct |
3 |
Correct |
1795 ms |
60300 KB |
Output is correct |
4 |
Correct |
1221 ms |
41156 KB |
Output is correct |
5 |
Correct |
808 ms |
41700 KB |
Output is correct |
6 |
Correct |
1435 ms |
50232 KB |
Output is correct |
7 |
Correct |
681 ms |
47628 KB |
Output is correct |
8 |
Correct |
1806 ms |
67488 KB |
Output is correct |
9 |
Correct |
486 ms |
47992 KB |
Output is correct |
10 |
Correct |
15 ms |
17656 KB |
Output is correct |
11 |
Correct |
1376 ms |
41100 KB |
Output is correct |
12 |
Correct |
1972 ms |
63624 KB |
Output is correct |
13 |
Correct |
1379 ms |
40968 KB |
Output is correct |
14 |
Correct |
877 ms |
40844 KB |
Output is correct |
15 |
Correct |
1657 ms |
44168 KB |
Output is correct |
16 |
Correct |
550 ms |
41464 KB |
Output is correct |
17 |
Correct |
1878 ms |
53344 KB |
Output is correct |
18 |
Correct |
506 ms |
47612 KB |
Output is correct |
19 |
Correct |
15 ms |
17656 KB |
Output is correct |
20 |
Incorrect |
1413 ms |
47500 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
17656 KB |
Output is correct |
2 |
Correct |
19 ms |
17656 KB |
Output is correct |
3 |
Correct |
15 ms |
17660 KB |
Output is correct |
4 |
Correct |
15 ms |
17656 KB |
Output is correct |
5 |
Correct |
15 ms |
17656 KB |
Output is correct |
6 |
Correct |
15 ms |
17656 KB |
Output is correct |
7 |
Correct |
15 ms |
17660 KB |
Output is correct |
8 |
Correct |
15 ms |
17656 KB |
Output is correct |
9 |
Correct |
15 ms |
17656 KB |
Output is correct |
10 |
Correct |
15 ms |
17656 KB |
Output is correct |
11 |
Correct |
15 ms |
17656 KB |
Output is correct |
12 |
Correct |
14 ms |
17656 KB |
Output is correct |
13 |
Correct |
1194 ms |
40968 KB |
Output is correct |
14 |
Correct |
1795 ms |
60300 KB |
Output is correct |
15 |
Correct |
1221 ms |
41156 KB |
Output is correct |
16 |
Correct |
808 ms |
41700 KB |
Output is correct |
17 |
Correct |
1435 ms |
50232 KB |
Output is correct |
18 |
Correct |
681 ms |
47628 KB |
Output is correct |
19 |
Correct |
1806 ms |
67488 KB |
Output is correct |
20 |
Correct |
486 ms |
47992 KB |
Output is correct |
21 |
Correct |
15 ms |
17656 KB |
Output is correct |
22 |
Correct |
1376 ms |
41100 KB |
Output is correct |
23 |
Correct |
1972 ms |
63624 KB |
Output is correct |
24 |
Correct |
1379 ms |
40968 KB |
Output is correct |
25 |
Correct |
877 ms |
40844 KB |
Output is correct |
26 |
Correct |
1657 ms |
44168 KB |
Output is correct |
27 |
Correct |
550 ms |
41464 KB |
Output is correct |
28 |
Correct |
1878 ms |
53344 KB |
Output is correct |
29 |
Correct |
506 ms |
47612 KB |
Output is correct |
30 |
Correct |
15 ms |
17656 KB |
Output is correct |
31 |
Correct |
54 ms |
17912 KB |
Output is correct |
32 |
Correct |
19 ms |
18040 KB |
Output is correct |
33 |
Correct |
64 ms |
17912 KB |
Output is correct |
34 |
Correct |
56 ms |
17912 KB |
Output is correct |
35 |
Correct |
55 ms |
17912 KB |
Output is correct |
36 |
Correct |
59 ms |
17912 KB |
Output is correct |
37 |
Correct |
58 ms |
17912 KB |
Output is correct |
38 |
Correct |
62 ms |
17964 KB |
Output is correct |
39 |
Correct |
58 ms |
17912 KB |
Output is correct |
40 |
Correct |
52 ms |
17912 KB |
Output is correct |
41 |
Correct |
80 ms |
18040 KB |
Output is correct |
42 |
Correct |
94 ms |
17912 KB |
Output is correct |
43 |
Correct |
25 ms |
18040 KB |
Output is correct |
44 |
Correct |
101 ms |
17944 KB |
Output is correct |
45 |
Correct |
15 ms |
17656 KB |
Output is correct |
46 |
Incorrect |
1413 ms |
47500 KB |
Output isn't correct |
47 |
Halted |
0 ms |
0 KB |
- |