#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#include<bits/stdc++.h>
//#include "rail.h"
#define rc(x) return cout<<x<<endl,0
#define pb push_back
#define mkp make_pair
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
typedef long long ll;
typedef long double ld;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll llinf=(1LL<<62);
const int inf=(1<<30);
const int nmax=2e5+50;
const int mod=1e9+7;
using namespace std;
int a[nmax],b[nmax],i,x,q,m,sz[nmax],n;
ll c[nmax],d[nmax],tot,cur[nmax],ans,tmp[nmax];
vector<ll>f[nmax],g[nmax];
vector<int>v[nmax];
int oth(int i,int x)
{
return a[i]^b[i]^x;
}
ll cst(int i,int x,int y)
{
if(a[i]==x)return c[i];
return d[i];
}
void dfs(int x,int p,ll l)
{
cur[x]=l;
for(int i=0;i<v[x].size();i++)
{
int y=oth(v[x][i],x);
ll z=cst(v[x][i],y,x)-cst(v[x][i],x,y);
if(y==p)continue;
dfs(y,x,l+z);
}
sz[x]=1;
for(int i=0;i<v[x].size();i++)
{
int y=oth(v[x][i],x);
ll z=cst(v[x][i],x,y);
if(y==p)continue;
tot+=z;
for(int j=1;j<=min(sz[x],m);j++)
{
for(int t=1;t<=min(sz[y],m);t++)
{
f[x][j+t]=max(f[x][j+t],g[x][j]+f[y][t]+z);
}
}
for(int j=0;j<=min(sz[x]+sz[y],m);j++)tmp[j]=0;
for(int j=0;j<=min(sz[x],m);j++)
{
for(int t=1;t<=min(sz[y],m);t++)
{
tmp[j+t]=max(tmp[j+t],g[x][j]+f[y][t]+z);
}
}
sz[x]+=sz[y];
for(int j=0;j<=min(sz[x],m);j++)
{
g[x][j]=max(g[x][j],tmp[j]);
}
}
}
int main()
{
//freopen("sol.in","r",stdin);
//freopen("sol.out","w",stdout);
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
cin>>n;
for(i=1;i<n;i++)
{
cin>>a[i]>>b[i]>>c[i]>>d[i];
v[a[i]].pb(i);
v[b[i]].pb(i);
}
if(n>2000)m=2;
else m=n;
for(i=1;i<=n;i++)
{
f[i].assign(m+2,0);
g[i].assign(m+2,0);
}
dfs(1,0,0);
cin>>q;
while(q--)
{
cin>>x;
ans=llinf;
for(i=1;i<=n;i++)ans=min(ans,tot-f[i][min(sz[i],x)]+cur[i]);
ans=max(ans,0LL);
cout<<ans<<endl;
}
return 0;
}
Compilation message
designated_cities.cpp:2:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization("O3")
designated_cities.cpp:3:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization("unroll-loops")
designated_cities.cpp: In function 'void dfs(int, int, ll)':
designated_cities.cpp:38:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v[x].size();i++)
~^~~~~~~~~~~~
designated_cities.cpp:46:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v[x].size();i++)
~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
14456 KB |
Output is correct |
2 |
Incorrect |
16 ms |
14456 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
14456 KB |
Output is correct |
2 |
Correct |
353 ms |
46680 KB |
Output is correct |
3 |
Correct |
401 ms |
69560 KB |
Output is correct |
4 |
Correct |
334 ms |
46696 KB |
Output is correct |
5 |
Correct |
342 ms |
46836 KB |
Output is correct |
6 |
Correct |
341 ms |
49656 KB |
Output is correct |
7 |
Correct |
315 ms |
46960 KB |
Output is correct |
8 |
Correct |
409 ms |
70008 KB |
Output is correct |
9 |
Correct |
277 ms |
47336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
14456 KB |
Output is correct |
2 |
Incorrect |
369 ms |
46672 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
14456 KB |
Output is correct |
2 |
Incorrect |
16 ms |
14456 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
14456 KB |
Output is correct |
2 |
Correct |
353 ms |
46680 KB |
Output is correct |
3 |
Correct |
401 ms |
69560 KB |
Output is correct |
4 |
Correct |
334 ms |
46696 KB |
Output is correct |
5 |
Correct |
342 ms |
46836 KB |
Output is correct |
6 |
Correct |
341 ms |
49656 KB |
Output is correct |
7 |
Correct |
315 ms |
46960 KB |
Output is correct |
8 |
Correct |
409 ms |
70008 KB |
Output is correct |
9 |
Correct |
277 ms |
47336 KB |
Output is correct |
10 |
Correct |
16 ms |
14456 KB |
Output is correct |
11 |
Incorrect |
369 ms |
46672 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
14456 KB |
Output is correct |
2 |
Incorrect |
16 ms |
14456 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |