Submission #122887

#TimeUsernameProblemLanguageResultExecution timeMemory
122887MvCDesignated Cities (JOI19_designated_cities)C++11
7 / 100
409 ms70008 KiB
#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 (stderr)

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...