Submission #1090484

#TimeUsernameProblemLanguageResultExecution timeMemory
1090484simona1230Race (IOI11_race)C++17
31 / 100
3027 ms53036 KiB
#include "race.h" #include <bits/stdc++.h> using namespace std; vector<int> v[200001],w[200001]; int ans=200000; void add(int i,int j,int k) { v[i].push_back(j); v[j].push_back(i); w[i].push_back(k); w[j].push_back(k); } int n,k; int all; int sz[200001]; int mark[200001]; void prec(int i,int p) { sz[i]=1; for(int j=0;j<v[i].size();j++) { int nb=v[i][j]; if(nb==p||mark[nb])continue; prec(nb,i); sz[i]+=sz[nb]; } } int centroid(int i,int p) { for(int j=0;j<v[i].size();j++) { int nb=v[i][j]; if(nb==p||mark[nb])continue; if(sz[nb]>all/2)return centroid(nb,i); } return i; } map<int,int> mp; vector<pair<int,int> > ext; void dfs(int i,int p,int cnt,int wg) { if(wg>k)return; if(wg==k)ans=min(ans,cnt); else { int wg2=k-wg; //cout<<wg2<<" + "<<mp[wg2]<<endl; ans=min(ans,cnt+mp[wg2]); } //cout<<i<<" "<<wg<<endl; ext.push_back({wg,cnt}); for(int j=0;j<v[i].size();j++) { int nb=v[i][j]; if(nb==p||mark[nb])continue; dfs(nb,i,cnt+1,wg+w[i][j]); } } void solve(int beg) { prec(beg,-1); all=sz[beg]; //cout<<all<<endl; int c=centroid(beg,-1); //cout<<"! "<<c<<endl; for(int i=1;i<=k;i++) mp[i]=n; mark[c]=1; for(int i=0;i<v[c].size();i++) { int nb=v[c][i]; if(mark[nb])continue; //cout<<c<<" "<<nb<<endl; dfs(nb,-1,1,w[c][i]); for(int j=0;j<ext.size();j++) { //cout<<"= "<<ext[j].first<<endl; mp[ext[j].first]=min(mp[ext[j].first],ext[j].second); } ext.clear(); } for(int i=0;i<v[c].size();i++) { int nb=v[c][i]; if(!mark[nb])solve(nb); } } int best_path(int N, int K, int H[][2], int L[]) { n=N; k=K; for(int i=0;i<n-1;i++) add(H[i][0],H[i][1],L[i]); solve(0); if(ans>=n)return -1; return ans; }

Compilation message (stderr)

race.cpp: In function 'void prec(int, int)':
race.cpp:25:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   25 |     for(int j=0;j<v[i].size();j++)
      |                 ~^~~~~~~~~~~~
race.cpp: In function 'int centroid(int, int)':
race.cpp:37:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |     for(int j=0;j<v[i].size();j++)
      |                 ~^~~~~~~~~~~~
race.cpp: In function 'void dfs(int, int, int, int)':
race.cpp:67:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |     for(int j=0;j<v[i].size();j++)
      |                 ~^~~~~~~~~~~~
race.cpp: In function 'void solve(int)':
race.cpp:88:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |     for(int i=0;i<v[c].size();i++)
      |                 ~^~~~~~~~~~~~
race.cpp:96:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |         for(int j=0;j<ext.size();j++)
      |                     ~^~~~~~~~~~~
race.cpp:104:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  104 |     for(int i=0;i<v[c].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...