//#pragma GCC optimize("Ofast")
//#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <array>
#include <set>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back //emplace_back
#define pp pop_back
#define pii pair<ll, ll> //pair<int, int>
#define all(x) (x).begin(),(x).end()
#define mp(a,b) make_pair(a , b)
#define lb lower_bound
#define ub upper_bound
#define sz(x) (ll)(x).size()
#define F first
#define S second
#define bg(x) (x).begin()
#define For(x, n) for(int (x) = 0 ; (x) < (n) ; (x)++)
#define debug(x) cout << #x << " : " << x << endl
#define endl '\n'
#define arr(x) array<ll , (x)>
#define yes cout << "YES\n"
#define no cout << "NO\n"
#define FAST ios_base::sync_with_stdio(0);cin.tie(0);
ll Sum(ll a , ll b , ll MOD)
{
a %= MOD;
b %= MOD;
a += b;
return a % MOD;
}
ll Mul(ll a , ll b , ll MOD)
{
a %= MOD;
b %= MOD;
a *= b;
return a % MOD;
}
ll Pow(ll a , ll b , ll MOD)
{
ll res = 1;
while(b)
{
if((b & 1))res = Mul(res , a , MOD);
a = Mul(a , a , MOD);
b >>= 1;
}
return res;
}
ll Min(ll a , ll b)
{
if(a > b)return b;
return a;
}
ll Max(ll a , ll b)
{
if(a > b)return a;
return b;
}
ll Ceil(ll a , ll b)
{
if(a % b)return (a/b)+1;
return a/b;
}
/////////////////////
//VALS
const ll maxn = 2e5;
const ll INF = 1e18;
ll n;
set<arr(3)> adj[maxn];
ll q;
ll barg;
ll par[maxn];
ll dp[maxn];
ll dp1;
ll root;
/////////////////////
//FUNCTIONS
void dfs_1_1(ll u, ll p)
{
par[u] = p;
for(auto v : adj[u])
{
if(v[0] == p)continue;
dp1 += v[1];
dfs_1_1(v[0], u);
}
}
void dfs_1_2(ll u, ll val)
{
for(auto v : adj[u])
{
if(v[0] == par[u])continue;
//debug(u);
//debug(v[0]);
dp[1] = Min(dp[1], val - v[1] + v[2]);
//debug(val - v[1] + v[2]);
dfs_1_2(v[0], val - v[1] + v[2]);
}
}
void compress(ll u)
{
if(u != root and sz(adj[u]) == 2)
{
auto ch = (*adj[u].begin());
auto p = (*adj[u].rbegin());
if(ch[0] == par[u])
swap(ch,p);
par[ch[0]] = p[0];
adj[ch[0]].erase(adj[ch[0]].find({u, ch[2], ch[1]}));
adj[p[0]].erase(adj[p[0]].find({u, p[2], p[1]}));
adj[p[0]].insert({ch[0], p[2]+ch[1], p[1]+ch[2]});
adj[ch[0]].insert({p[0], p[1]+ch[2], p[2]+ch[1]});
}
for(auto v : adj[u])
if(v[0] != par[u])
compress(v[0]);
}
/////////////////////
//SOLVE
void solve()
{
cin >> n;
For(i,n-1)
{
ll a, b ,c ,d;
cin >> a >> b >> c >> d;
a--;
b--;
adj[a].insert({b,c,d});
adj[b].insert({a,d,c});
}
barg = 0;
For(i,n)if(sz(adj[i]) == 1)barg++;
for(int i = barg; i <= n; i++)dp[i] = 0;
root = 0;
For(i,n)
if(sz(adj[i]) != 1)
{
root = i;
break;
}
dfs_1_1(root, root);
dp[1] = dp1;
//debug(dp[1]);
dfs_1_2(root, dp1);
compress(root);
multiset<arr(3)> s;
For(i,n)
if(sz(adj[i]) == 1)
{
auto u = (*adj[i].begin());
s.insert({u[1],u[2], i});
}
ll cur = 0;
barg--;
for(int i = barg; i >= 1; i--)
{
auto bst = (*s.begin());
s.erase(bst);
cur += bst[0];
dp[i] = cur;
//bst -> {0 -> bottom to top, 1 -> top to bottom, 2 -> index}
adj[par[bst[2]]].erase(adj[par[bst[2]]].find({bst[2], bst[1], bst[0]}));
if(sz(adj[par[bst[2]]]) == 2)
{
ll u = par[bst[2]];
auto ch = (*adj[u].begin());
auto p = (*adj[u].rbegin());
if(ch[0] == par[u])
swap(ch,p);
par[ch[0]] = p[0];
adj[ch[0]].erase(adj[ch[0]].find({u, ch[2], ch[1]}));
adj[p[0]].erase(adj[p[0]].find({u, p[2], p[1]}));
adj[p[0]].insert({ch[0], p[2]+ch[1], p[1]+ch[2]});
adj[ch[0]].insert({p[0], p[1]+ch[2], p[2]+ch[1]});
s.insert({p[1]+ch[2], p[2]+ch[1], p[0]});
}
}
cin >> q;
while(q--)
{
ll e;
cin >> e;
cout << dp[e] << endl;
}
}
/////////////////////
//MAIN
int main()
{
FAST;
ll t = 1;
// cin >> t;
while(t--)
{
solve();
}
}
/////////////////////
/*
ZZZZZZZ A M M IIIIIII N N
Z A A M M M M I NN N
Z A A M M M M I N N N
Z AAAAAAA M M M I N N N
Z A A M M I N N N
Z A A M M I N NN
ZZZZZZZ A A M M IIIIIII N N TREE
*/
Compilation message
designated_cities.cpp: In function 'void solve()':
designated_cities.cpp:26:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
26 | #define For(x, n) for(int (x) = 0 ; (x) < (n) ; (x)++)
| ^
designated_cities.cpp:148:2: note: in expansion of macro 'For'
148 | For(i,n-1)
| ^~~
designated_cities.cpp:26:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
26 | #define For(x, n) for(int (x) = 0 ; (x) < (n) ; (x)++)
| ^
designated_cities.cpp:160:2: note: in expansion of macro 'For'
160 | For(i,n)if(sz(adj[i]) == 1)barg++;
| ^~~
designated_cities.cpp:26:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
26 | #define For(x, n) for(int (x) = 0 ; (x) < (n) ; (x)++)
| ^
designated_cities.cpp:165:2: note: in expansion of macro 'For'
165 | For(i,n)
| ^~~
designated_cities.cpp:26:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
26 | #define For(x, n) for(int (x) = 0 ; (x) < (n) ; (x)++)
| ^
designated_cities.cpp:182:2: note: in expansion of macro 'For'
182 | For(i,n)
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Runtime error |
14 ms |
25524 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12636 KB |
Output is correct |
2 |
Runtime error |
305 ms |
93144 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Runtime error |
309 ms |
93012 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Runtime error |
14 ms |
25524 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12636 KB |
Output is correct |
2 |
Runtime error |
305 ms |
93144 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Runtime error |
14 ms |
25524 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |