Submission #978028

#TimeUsernameProblemLanguageResultExecution timeMemory
978028simona1230Race (IOI11_race)C++17
0 / 100
25 ms24920 KiB
#include <bits/stdc++.h> #include "race.h" using namespace std; int n,k; vector<int> v[200001]; vector<int> w[200001]; map<int,int> mp[200001]; int sz[200001]; int used[200001]; int ans; void dfs(int i,int p) { for(int j=0;j<v[i].size();j++) { int nb=v[i][j],wg=w[i][j]; if(nb==p||used[nb])continue; dfs(nb,i); //cout<<nb<<" --- "<<wg<<endl; for(auto it=mp[nb].begin();it!=mp[nb].end();it++) { int vl=it->first+wg; int cnt=it->second+1; if(mp[i][k-vl]) ans=min(ans,mp[i][k-vl]+cnt); } for(auto it=mp[nb].begin();it!=mp[nb].end();it++) { int vl=it->first+wg; int cnt=it->second+1; mp[i][vl]=min(mp[i][vl],cnt); if(mp[i][vl]==0)mp[i][vl]=cnt; //cout<<i<<"->"<<vl<<" "<<mp[i][vl]<<endl; } if(mp[i][k-wg]) ans=min(ans,mp[i][k-wg]+1); mp[i][wg]=1; if(wg==k)ans=1; } } void calc(int i,int p) { //cout<<i<<endl; sz[i]=1; for(int j=0;j<v[i].size();j++) { int nb=v[i][j]; if(nb==p||used[nb])continue; calc(nb,i); sz[i]+=sz[nb]; } } int centroid(int i,int p) { //cout<<"in "<<i<<endl; for(int j=0;j<v[i].size();j++) { int nb=v[i][j]; if(nb==p||used[nb])continue; if(sz[nb]>n/2)return centroid(nb,i); } return i; } void solve(int i) { //cout<<i<<endl; calc(i,n); int c=centroid(i,n); //cout<<"* "<<c<<endl; dfs(c,n); used[c]=1; for(int j=0;j<v[c].size();j++) { int nb=v[c][j]; if(!used[nb])solve(nb); } } int best_path(int N, int K, int H[][2], int L[]) { n=N; k=K; ans=n; for(int i=0;i<n-1;i++) { v[H[i][0]].push_back(H[i][1]); v[H[i][1]].push_back(H[i][0]); w[H[i][0]].push_back(L[i]); w[H[i][1]].push_back(L[i]); //cout<<H[i][0]<<" "<<H[i][1]<<endl; } solve(0); if(ans==n)ans=-1; return ans; }

Compilation message (stderr)

race.cpp: In function 'void dfs(int, int)':
race.cpp:15:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   15 |     for(int j=0;j<v[i].size();j++)
      |                 ~^~~~~~~~~~~~
race.cpp: In function 'void calc(int, int)':
race.cpp:51:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |     for(int j=0;j<v[i].size();j++)
      |                 ~^~~~~~~~~~~~
race.cpp: In function 'int centroid(int, int)':
race.cpp:64:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |     for(int j=0;j<v[i].size();j++)
      |                 ~^~~~~~~~~~~~
race.cpp: In function 'void solve(int)':
race.cpp:82:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |     for(int j=0;j<v[c].size();j++)
      |                 ~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...