Submission #29396

#TimeUsernameProblemLanguageResultExecution timeMemory
29396dereotuRace (IOI11_race)C++14
100 / 100
2226 ms44512 KiB
#include "race.h" #include <bits/stdc++.h> #define pii pair<int,int> #define mp make_pair #define pb push_back #define st first #define nd second #define forr(i,A,B) for(int i=A;i<B;++i) #define space ' ' #define endl '\n' #define LL long long using namespace std; vector <pair<int,int> > adj[200005]; int ans=1e9; int used[200005],sub[200005]; int k; int calc_sub(int x,int y){//ok sub[x]=1; forr(i,0,adj[x].size()){ if(adj[x][i].nd!=y and !used[adj[x][i].nd]){ sub[x]+=calc_sub(adj[x][i].nd,x); } } return sub[x]; } int find_centroid(int x,int y,int treesize){//ok forr(i,0,adj[x].size()){ int u=adj[x][i].nd; if(!used[u] and u!=y and sub[u]>treesize/2){ return find_centroid(u,x,treesize); } } return x; } void dfs(int x,int y,int length,int path,map<int,int> &temp_map){ if(length>k) return; if(temp_map.find(length)==temp_map.end()){ temp_map[length]=path; } else{ temp_map[length]=min(temp_map[length],path); } forr(i,0,adj[x].size()){ int u=adj[x][i].nd; if(u!=y and !used[u]){ dfs(u,x,length+adj[x][i].st,path+1,temp_map); } } } void merge(map<int,int> &a,map<int,int> &b){ for(auto it=b.begin();it!=b.end();++it){ if(a.find(k-it->st)!=a.end()){ ans=min(ans,a[k-it->st]+it->nd); } } for(auto it=b.begin();it!=b.end();++it){ if(a.find(it->st)==a.end()){ a[it->st]=it->nd; } else a[it->st]=min(a[it->st],it->nd); } } void calc(int x){//ok map<int,int> main_map; main_map[0]=0; forr(i,0,adj[x].size()){ int u=adj[x][i].nd; if(!used[u]){ map<int,int> sub_map; dfs(u,x,adj[x][i].st,1,sub_map); merge(main_map,sub_map); } } } void solve(int x){ calc_sub(x,-1); int centroid=find_centroid(x,-1,sub[x]); used[centroid]=1; calc(centroid); forr(i,0,adj[centroid].size()){ int u=adj[centroid][i].nd; if(!used[u]){ solve(u); } } } int best_path(int N, int K, int H[][2], int L[]) { for(int i=0;i<N;i++){ adj[H[i][0]].pb(mp(L[i],H[i][1])); adj[H[i][1]].pb(mp(L[i],H[i][0])); } k=K; solve(0); if(ans==1e9) return -1; else return ans; }

Compilation message (stderr)

race.cpp: In function 'int calc_sub(int, int)':
race.cpp:8:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define forr(i,A,B) for(int i=A;i<B;++i)
race.cpp:22:7:
  forr(i,0,adj[x].size()){
       ~~~~~~~~~~~~~~~~~           
race.cpp:22:2: note: in expansion of macro 'forr'
  forr(i,0,adj[x].size()){
  ^~~~
race.cpp: In function 'int find_centroid(int, int, int)':
race.cpp:8:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define forr(i,A,B) for(int i=A;i<B;++i)
race.cpp:31:7:
  forr(i,0,adj[x].size()){
       ~~~~~~~~~~~~~~~~~           
race.cpp:31:2: note: in expansion of macro 'forr'
  forr(i,0,adj[x].size()){
  ^~~~
race.cpp: In function 'void dfs(int, int, int, int, std::map<int, int>&)':
race.cpp:8:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define forr(i,A,B) for(int i=A;i<B;++i)
race.cpp:48:7:
  forr(i,0,adj[x].size()){
       ~~~~~~~~~~~~~~~~~           
race.cpp:48:2: note: in expansion of macro 'forr'
  forr(i,0,adj[x].size()){
  ^~~~
race.cpp: In function 'void calc(int)':
race.cpp:8:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define forr(i,A,B) for(int i=A;i<B;++i)
race.cpp:73:7:
  forr(i,0,adj[x].size()){
       ~~~~~~~~~~~~~~~~~           
race.cpp:73:2: note: in expansion of macro 'forr'
  forr(i,0,adj[x].size()){
  ^~~~
race.cpp: In function 'void solve(int)':
race.cpp:8:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define forr(i,A,B) for(int i=A;i<B;++i)
race.cpp:88:7:
  forr(i,0,adj[centroid].size()){
       ~~~~~~~~~~~~~~~~~~~~~~~~    
race.cpp:88:2: note: in expansion of macro 'forr'
  forr(i,0,adj[centroid].size()){
  ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...