제출 #853567

#제출 시각아이디문제언어결과실행 시간메모리
853567YassirSalama경주 (Race) (IOI11_race)C++17
31 / 100
401 ms170404 KiB
#include "race.h" #include <iostream> #include <vector> #include <algorithm> #include <unordered_map> #include <set> #include <unordered_set> #include <iomanip> #include <cmath> #include <limits> #include <map> #include <utility> #include <cctype> #include <string> #include <cstring> #include <stack> #include <queue> #include <functional> #include <iterator> using namespace std; #define OVL(x,s) for(auto y:x) cout<<y<<s; cout<<"\n"; void dbg_out() { cout << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); } #define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__); #define endl "\n" #define pb push_back #define F first #define S second #define ll long long #define mod 1000000007 #define all(v) v.begin(),v.end() const int MAXN=2e5+1; const int MAXT=110; const int INF=1e9; vector<pair<int,int>> v[MAXN]; int n,k; unordered_map<int,int> mp[MAXN]; vector<vector<int>> arr(MAXN,vector<int>(MAXT,-1)); int ans=INF; int dfs(int node,int par){ if(v[node].size()==1&&v[node][0].F==par) return -1; mp[node][0]=0; arr[node][0]=0; for(auto x:v[node]){ if(x.F==par) continue; int a=dfs(x.F,node); if(x.S>k) continue; if(a==-1){ int target=k-x.S; if(target>=0&&arr[node][target]!=-1){ ans=min(ans,arr[node][target]+1); } arr[node][x.S]=1; // mp[node][x.S]=1; }else{ // dbg(node); for(int i=0;i<MAXT;i++){ if(i==0) continue; int target=k-(i+x.S); if(target<0) continue; if(arr[x.F][i]==-1) continue; // dbg(i,target,arr[node][target],arr[x.F][i]); if(arr[node][target]!=-1){ ans=min(ans,arr[node][target]+arr[x.F][i]+1); } } for(int key=0;key<MAXT;key++){ if(arr[x.F][key]==-1) continue; if(key+x.S>=MAXT) continue; if(arr[node][key+x.S]!=-1){ arr[node][key+x.S]=min(arr[node][key+x.S],arr[x.F][key]+1); }else arr[node][key+x.S]=arr[x.F][key]+1; } } } // dbg(node,"last"); for(int i=0;i<MAXT;i++){ if(arr[node][i]==-1) continue; // dbg(i,arr[node][i]); } return 1; } int best_path(int N, int K, int H[][2], int L[]) { n=N; k=K; for(int i=0;i<n-1;i++){ int a=H[i][0]; int b=H[i][1]; int c=L[i]; v[a].pb({b,c}); v[b].pb({a,c}); } dfs(0,0); if(ans==INF) return -1; return ans; } #ifdef IOI #define MAX_N 500000 static int N, K; static int H[MAX_N][2]; static int L[MAX_N]; static int solution; inline void my_assert(int e) {if (!e) abort();} void read_input() { int i; my_assert(2==scanf("%d %d",&N,&K)); for(i=0; i<N-1; i++) my_assert(3==scanf("%d %d %d",&H[i][0],&H[i][1],&L[i])); my_assert(1==scanf("%d",&solution)); } int main() { int ans; read_input(); ans = best_path(N,K,H,L); if(ans==solution) printf("Correct.\n"); else printf("Incorrect. Returned %d, Expected %d.\n",ans,solution); return 0; } #endif
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...