#pragma GCC optimize("O2")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define pb push_back
#define sz(x) (int)(x.size())
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define vi vector<int>
#define vp vector<pii>
#define vvi vector<vi>
#define ykh mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count())
#define __lg(x) 63-__builtin_clzll(x)
#define pow2(x) (1LL<<x)
void __print(int x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef local
void setio(){freopen("/Users/iantsai/Library/Mobile Documents/com~apple~CloudDocs/cpp/Empty.md","r",stdin);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
void setio(){}
#define debug(x...)
#endif
const int mxn=2e5+5;
inline char gc()
{
const static int SZ = 1 << 16;
static char buf[SZ], *p1, *p2;
if (p1 == p2 && (p2 = buf + fread(p1 = buf, 1, SZ, stdin), p1 == p2))
return -1;
return *p1++;
}
inline void rd() {}
template <typename T, typename... U>
inline void rd(T &x, U &...y)
{
x = 0;
bool f = 0;
char c = gc();
while (!isdigit(c))
f ^= !(c ^ 45), c = gc();
while (isdigit(c))
x = (x << 1) + (x << 3) + (c ^ 48), c = gc();
f && (x = -x), rd(y...);
}
template <typename T>
inline void prt(T x)
{
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
prt(x / 10);
putchar((x % 10) ^ 48);
}
vector<tuple<int,int,int>>adj[mxn];
ll dp[mxn][2];
pair<ll,int> dp2[mxn];
ll dep[mxn];
int p1;
void dfs(int v,int pa){
for(auto [u,d,d2]:adj[v]){
if(u==pa)continue;
dp[u][0]+=dp[v][0]+d;
dep[u]=dep[v]+d;
dfs(u,v);
dp[v][1]+=d2+dp[u][1];
}
}
ll all;
ll ans[mxn];
void calc(int v,int pa){
ll mx=0;
dp2[v].F=dep[v];
dp2[v].S=v;
int t1=0;
for(auto [u,d,d2]:adj[v]){
if(u==pa)continue;
dp[u][1]+=dp[v][1]-dp[u][1]-d2;
calc(u,v);
if(dp2[v].F+dp2[u].F-2*dep[v]>mx){
t1=dp2[v].S;
mx=dp2[v].F+dp2[u].F-2*dep[v];
}
dp2[v]=max(dp2[v],dp2[u]);
}
ans[1]=min(ans[1],all-dp[v][0]-dp[v][1]);
if(ans[2]>all-dp[v][0]-dp[v][1]-mx){
p1=t1;
ans[2]=all-dp[v][0]-dp[v][1]-mx;
}
}
ll len[mxn];
int idx;
ll dfs2(int v,int pa){
vector<ll>temp;
for(auto [u,d,d2]:adj[v]){
if(u==pa)continue;
temp.pb(dfs2(u,v)+d);
}
if(sz(temp)==0)return 0;
auto it=max_element(temp.begin(),temp.end());
for(auto it2=temp.begin();it2!=temp.end();it2++){
if(it2==it)continue;
len[idx++]=*it2;
}
return *it;
}
signed main(){
setio();
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int n;
rd(n);
ans[2]=1e18;
ans[1]=1e18;
for(int i=1;i<n;i++){
int a,b,c,d;
rd(a),rd(b),rd(c),rd(d);
adj[a].pb({b,c,d});
adj[b].pb({a,d,c});
all+=c+d;
}
dfs(1,1);
calc(1,1);
ll v=dfs2(p1,p1);
len[idx++]=v;
sort(len,len+idx,greater<ll>());
for(int i=1;i<idx;i++){
ans[2+i]=ans[1+i]-len[i];
}
int q;
rd(q);
while(q--){
int x;
rd(x);
prt(ans[x]);
putchar('\n');
}
}
/*
input:
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
5200 KB |
Output is correct |
2 |
Correct |
5 ms |
5204 KB |
Output is correct |
3 |
Correct |
4 ms |
5204 KB |
Output is correct |
4 |
Correct |
4 ms |
5072 KB |
Output is correct |
5 |
Correct |
4 ms |
5204 KB |
Output is correct |
6 |
Correct |
5 ms |
5204 KB |
Output is correct |
7 |
Correct |
4 ms |
5204 KB |
Output is correct |
8 |
Correct |
5 ms |
5204 KB |
Output is correct |
9 |
Correct |
5 ms |
5204 KB |
Output is correct |
10 |
Correct |
5 ms |
5368 KB |
Output is correct |
11 |
Correct |
5 ms |
5204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
5200 KB |
Output is correct |
2 |
Correct |
153 ms |
28776 KB |
Output is correct |
3 |
Correct |
241 ms |
47636 KB |
Output is correct |
4 |
Correct |
174 ms |
27544 KB |
Output is correct |
5 |
Correct |
164 ms |
29420 KB |
Output is correct |
6 |
Correct |
218 ms |
31808 KB |
Output is correct |
7 |
Correct |
144 ms |
30392 KB |
Output is correct |
8 |
Correct |
235 ms |
46924 KB |
Output is correct |
9 |
Correct |
97 ms |
33184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5200 KB |
Output is correct |
2 |
Correct |
153 ms |
28860 KB |
Output is correct |
3 |
Correct |
214 ms |
47536 KB |
Output is correct |
4 |
Correct |
139 ms |
27524 KB |
Output is correct |
5 |
Correct |
165 ms |
29500 KB |
Output is correct |
6 |
Correct |
165 ms |
32620 KB |
Output is correct |
7 |
Correct |
110 ms |
32624 KB |
Output is correct |
8 |
Correct |
255 ms |
42168 KB |
Output is correct |
9 |
Correct |
105 ms |
32760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
5200 KB |
Output is correct |
2 |
Correct |
5 ms |
5204 KB |
Output is correct |
3 |
Correct |
4 ms |
5204 KB |
Output is correct |
4 |
Correct |
4 ms |
5072 KB |
Output is correct |
5 |
Correct |
4 ms |
5204 KB |
Output is correct |
6 |
Correct |
5 ms |
5204 KB |
Output is correct |
7 |
Correct |
4 ms |
5204 KB |
Output is correct |
8 |
Correct |
5 ms |
5204 KB |
Output is correct |
9 |
Correct |
5 ms |
5204 KB |
Output is correct |
10 |
Correct |
5 ms |
5368 KB |
Output is correct |
11 |
Correct |
5 ms |
5204 KB |
Output is correct |
12 |
Correct |
4 ms |
5024 KB |
Output is correct |
13 |
Correct |
5 ms |
5460 KB |
Output is correct |
14 |
Correct |
6 ms |
5460 KB |
Output is correct |
15 |
Correct |
6 ms |
5460 KB |
Output is correct |
16 |
Correct |
5 ms |
5460 KB |
Output is correct |
17 |
Correct |
5 ms |
5460 KB |
Output is correct |
18 |
Correct |
5 ms |
5360 KB |
Output is correct |
19 |
Correct |
5 ms |
5716 KB |
Output is correct |
20 |
Correct |
5 ms |
5412 KB |
Output is correct |
21 |
Correct |
4 ms |
5460 KB |
Output is correct |
22 |
Correct |
5 ms |
5460 KB |
Output is correct |
23 |
Correct |
5 ms |
5460 KB |
Output is correct |
24 |
Correct |
5 ms |
5460 KB |
Output is correct |
25 |
Correct |
5 ms |
5472 KB |
Output is correct |
26 |
Correct |
5 ms |
5460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
5200 KB |
Output is correct |
2 |
Correct |
153 ms |
28776 KB |
Output is correct |
3 |
Correct |
241 ms |
47636 KB |
Output is correct |
4 |
Correct |
174 ms |
27544 KB |
Output is correct |
5 |
Correct |
164 ms |
29420 KB |
Output is correct |
6 |
Correct |
218 ms |
31808 KB |
Output is correct |
7 |
Correct |
144 ms |
30392 KB |
Output is correct |
8 |
Correct |
235 ms |
46924 KB |
Output is correct |
9 |
Correct |
97 ms |
33184 KB |
Output is correct |
10 |
Correct |
4 ms |
5200 KB |
Output is correct |
11 |
Correct |
153 ms |
28860 KB |
Output is correct |
12 |
Correct |
214 ms |
47536 KB |
Output is correct |
13 |
Correct |
139 ms |
27524 KB |
Output is correct |
14 |
Correct |
165 ms |
29500 KB |
Output is correct |
15 |
Correct |
165 ms |
32620 KB |
Output is correct |
16 |
Correct |
110 ms |
32624 KB |
Output is correct |
17 |
Correct |
255 ms |
42168 KB |
Output is correct |
18 |
Correct |
105 ms |
32760 KB |
Output is correct |
19 |
Correct |
5 ms |
5172 KB |
Output is correct |
20 |
Correct |
148 ms |
29004 KB |
Output is correct |
21 |
Correct |
228 ms |
47436 KB |
Output is correct |
22 |
Correct |
145 ms |
27468 KB |
Output is correct |
23 |
Correct |
148 ms |
29040 KB |
Output is correct |
24 |
Correct |
133 ms |
27820 KB |
Output is correct |
25 |
Correct |
188 ms |
28784 KB |
Output is correct |
26 |
Correct |
170 ms |
27828 KB |
Output is correct |
27 |
Correct |
162 ms |
29044 KB |
Output is correct |
28 |
Correct |
159 ms |
32068 KB |
Output is correct |
29 |
Correct |
184 ms |
28748 KB |
Output is correct |
30 |
Correct |
168 ms |
28704 KB |
Output is correct |
31 |
Correct |
159 ms |
30720 KB |
Output is correct |
32 |
Correct |
222 ms |
42820 KB |
Output is correct |
33 |
Correct |
105 ms |
33144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
5200 KB |
Output is correct |
2 |
Correct |
5 ms |
5204 KB |
Output is correct |
3 |
Correct |
4 ms |
5204 KB |
Output is correct |
4 |
Correct |
4 ms |
5072 KB |
Output is correct |
5 |
Correct |
4 ms |
5204 KB |
Output is correct |
6 |
Correct |
5 ms |
5204 KB |
Output is correct |
7 |
Correct |
4 ms |
5204 KB |
Output is correct |
8 |
Correct |
5 ms |
5204 KB |
Output is correct |
9 |
Correct |
5 ms |
5204 KB |
Output is correct |
10 |
Correct |
5 ms |
5368 KB |
Output is correct |
11 |
Correct |
5 ms |
5204 KB |
Output is correct |
12 |
Correct |
5 ms |
5200 KB |
Output is correct |
13 |
Correct |
153 ms |
28776 KB |
Output is correct |
14 |
Correct |
241 ms |
47636 KB |
Output is correct |
15 |
Correct |
174 ms |
27544 KB |
Output is correct |
16 |
Correct |
164 ms |
29420 KB |
Output is correct |
17 |
Correct |
218 ms |
31808 KB |
Output is correct |
18 |
Correct |
144 ms |
30392 KB |
Output is correct |
19 |
Correct |
235 ms |
46924 KB |
Output is correct |
20 |
Correct |
97 ms |
33184 KB |
Output is correct |
21 |
Correct |
4 ms |
5200 KB |
Output is correct |
22 |
Correct |
153 ms |
28860 KB |
Output is correct |
23 |
Correct |
214 ms |
47536 KB |
Output is correct |
24 |
Correct |
139 ms |
27524 KB |
Output is correct |
25 |
Correct |
165 ms |
29500 KB |
Output is correct |
26 |
Correct |
165 ms |
32620 KB |
Output is correct |
27 |
Correct |
110 ms |
32624 KB |
Output is correct |
28 |
Correct |
255 ms |
42168 KB |
Output is correct |
29 |
Correct |
105 ms |
32760 KB |
Output is correct |
30 |
Correct |
4 ms |
5024 KB |
Output is correct |
31 |
Correct |
5 ms |
5460 KB |
Output is correct |
32 |
Correct |
6 ms |
5460 KB |
Output is correct |
33 |
Correct |
6 ms |
5460 KB |
Output is correct |
34 |
Correct |
5 ms |
5460 KB |
Output is correct |
35 |
Correct |
5 ms |
5460 KB |
Output is correct |
36 |
Correct |
5 ms |
5360 KB |
Output is correct |
37 |
Correct |
5 ms |
5716 KB |
Output is correct |
38 |
Correct |
5 ms |
5412 KB |
Output is correct |
39 |
Correct |
4 ms |
5460 KB |
Output is correct |
40 |
Correct |
5 ms |
5460 KB |
Output is correct |
41 |
Correct |
5 ms |
5460 KB |
Output is correct |
42 |
Correct |
5 ms |
5460 KB |
Output is correct |
43 |
Correct |
5 ms |
5472 KB |
Output is correct |
44 |
Correct |
5 ms |
5460 KB |
Output is correct |
45 |
Correct |
5 ms |
5172 KB |
Output is correct |
46 |
Correct |
148 ms |
29004 KB |
Output is correct |
47 |
Correct |
228 ms |
47436 KB |
Output is correct |
48 |
Correct |
145 ms |
27468 KB |
Output is correct |
49 |
Correct |
148 ms |
29040 KB |
Output is correct |
50 |
Correct |
133 ms |
27820 KB |
Output is correct |
51 |
Correct |
188 ms |
28784 KB |
Output is correct |
52 |
Correct |
170 ms |
27828 KB |
Output is correct |
53 |
Correct |
162 ms |
29044 KB |
Output is correct |
54 |
Correct |
159 ms |
32068 KB |
Output is correct |
55 |
Correct |
184 ms |
28748 KB |
Output is correct |
56 |
Correct |
168 ms |
28704 KB |
Output is correct |
57 |
Correct |
159 ms |
30720 KB |
Output is correct |
58 |
Correct |
222 ms |
42820 KB |
Output is correct |
59 |
Correct |
105 ms |
33144 KB |
Output is correct |
60 |
Correct |
4 ms |
5204 KB |
Output is correct |
61 |
Correct |
190 ms |
31564 KB |
Output is correct |
62 |
Correct |
254 ms |
49108 KB |
Output is correct |
63 |
Correct |
183 ms |
30284 KB |
Output is correct |
64 |
Correct |
201 ms |
31700 KB |
Output is correct |
65 |
Correct |
168 ms |
30284 KB |
Output is correct |
66 |
Correct |
174 ms |
31560 KB |
Output is correct |
67 |
Correct |
164 ms |
30284 KB |
Output is correct |
68 |
Correct |
154 ms |
31824 KB |
Output is correct |
69 |
Correct |
143 ms |
34652 KB |
Output is correct |
70 |
Correct |
223 ms |
31036 KB |
Output is correct |
71 |
Correct |
193 ms |
31712 KB |
Output is correct |
72 |
Correct |
163 ms |
33916 KB |
Output is correct |
73 |
Correct |
257 ms |
44916 KB |
Output is correct |
74 |
Correct |
134 ms |
37592 KB |
Output is correct |