Submission #853549

#TimeUsernameProblemLanguageResultExecution timeMemory
853549YassirSalamaRace (IOI11_race)C++17
9 / 100
221 ms74228 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 ll MAXN=2e5+100; const ll MAXT=200; const ll INF=1e9; vector<pair<ll,ll>> v[MAXN]; ll n,k; ll depth[MAXN]; map<ll,ll> mp[MAXN]; ll ans=INF; ll dfs(ll node,ll par){ if(v[node].size()==1&&v[node][0].F==par) return -1; // return -1; // mp[node].clear(); mp[node][0]=0; for(auto x:v[node]){ if(x.F==par) continue; ll a=dfs(x.F,node); // dbg(x.F,node,a); if(x.S>k) continue; if(a==-1){ // dbg(x.F,node); mp[node][x.S]=1; }else{ // dbg(node); map<ll,ll> newmp; for(auto & [key,value]:mp[x.F]){ if(key==0) continue; newmp[key+x.S]=value+1; } for(auto &[key,value]:newmp){ ll target=k-key; // dbg(key,value,target,mp[node].count(target)); if(target<0) continue; // if(target==0){ // ans=min(ans,value); // continue; // } if(mp[node].count(target)){ ans=min(ans,mp[node][target]+value); } } for(auto [key,val]:mp[x.F]){ if(mp[node].count(key+x.S)) mp[node][key+x.S]=min(mp[node][key+x.S],val+1); else mp[node][key+x.S]=val+1; } } } 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); // dbg(ans); 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...