//#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,int l)
{
cur[x]=l;
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)+cst(v[x][i],y,x);
if(y==p)continue;
dfs(y,x,l+z);
}
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=5;
else m=n+10;
for(i=1;i<=n;i++)
{
f[i].assign(m+15,0);
g[i].assign(m+15,0);
}
dfs(1,0,0);
//cout<<tot<<endl;
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: In function 'void dfs(int, int, int)':
designated_cities.cpp:39: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++)
~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
14460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
14456 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
14456 KB |
Output is correct |
2 |
Incorrect |
471 ms |
96728 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
14460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
14456 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
14460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |