제출 #423444

#제출 시각아이디문제언어결과실행 시간메모리
423444codebuster_10경주 (Race) (IOI11_race)C++17
100 / 100
485 ms83776 KiB
#include <bits/stdc++.h> using namespace std ; //#define int int64_t //be careful about this #define endl "\n" #define f(i,a,b) for(int i=int(a);i<int(b);++i) #define pr pair #define ar array #define fr first #define sc second #define vt vector #define pb push_back #define eb emplace_back #define LB lower_bound #define UB upper_bound #define PQ priority_queue #define sz(x) ((int)(x).size()) #define all(a) (a).begin(),(a).end() #define allr(a) (a).rbegin(),(a).rend() #define mem(a,b) memset(a, b, sizeof(a)) template<class A> void rd(vt<A>& v); template<class T> void rd(T& x){ cin >> x; } template<class H, class... T> void rd(H& h, T&... t) { rd(h) ; rd(t...) ;} template<class A> void rd(vt<A>& x) { for(auto& a : x) rd(a) ;} template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } template<typename T> void __p(T a) { cout<<a; } template<typename T, typename F> void __p(pair<T, F> a) { cout<<"{"; __p(a.first); cout<<","; __p(a.second); cout<<"}\n"; } template<typename T> void __p(std::vector<T> a) { cout<<"{"; for(auto it=a.begin(); it<a.end(); it++) __p(*it),cout<<",}\n"[it+1==a.end()]; } template<typename T, typename ...Arg> void __p(T a1, Arg ...a) { __p(a1); __p(a...); } template<typename Arg1> void __f(const char *name, Arg1 &&arg1) { cout<<name<<" : "; __p(arg1); cout<<endl; } template<typename Arg1, typename ... Args> void __f(const char *names, Arg1 &&arg1, Args &&... args) { int bracket=0,i=0; for(;; i++) if(names[i]==','&&bracket==0) break; else if(names[i]=='(') bracket++; else if(names[i]==')') bracket--; const char *comma=names+i; cout.write(names,comma-names)<<" : "; __p(arg1); cout<<" | "; __f(comma+1,args...); } void setIO(string s = "") { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin.exceptions(cin.failbit); cout.precision(15); cout << fixed; if(sz(s)){ freopen((s+".in").c_str(),"r",stdin); freopen((s+".out").c_str(),"w",stdout); } } int best_path(int N, int K, int H[][2], int L[]) { vt<ar<int,2>> g[N]; f(i,0,N-1){ int u = H[i][0], v = H[i][1], w = L[i]; g[u].pb({v,w}); g[v].pb({u,w}); } int ans = N; map<int,int> dp[N]; vt<int> ew(N), el(N); /* current dp[i] is actually {a + extra_weight, b + extra_length} */ function<void(int,int)> dfs = [&](int i,int p){ dp[i][0] = ew[i] = el[i] = 0; for(auto [j,w] : g[i]) if(j != p){ dfs(j,i); if(sz(dp[i]) > sz(dp[j])){ // update ans. for(auto [w_j, l_j] : dp[j]){ int actual_w = w_j + ew[j] + w; int actual_l = l_j + el[j] + 1; int need_w = K - actual_w; int put_i = need_w - ew[i]; if(dp[i].count(put_i)){ ckmin(ans,dp[i][put_i] + el[i] + actual_l); } } // update dp[i]. for(auto [w_j, l_j] : dp[j]){ int actual_w = w_j + ew[j] + w; int actual_l = l_j + el[j] + 1; int put_i = actual_w - ew[i]; int put_l = actual_l - el[i]; if(dp[i].count(put_i)){ ckmin(dp[i][put_i], put_l); }else{ dp[i][put_i] = put_l; } } dp[j].clear(); }else{ // update ans. for(auto [w_i,l_i] : dp[i]){ int actual_w = w_i + ew[i] + w; int actual_l = l_i + el[i] + 1; int need_w = K - actual_w; int put_j = need_w - ew[j]; if(dp[j].count(put_j)){ ckmin(ans,dp[j][put_j] + el[j] + actual_l); } } //update dp[j]. el[j] += 1; ew[j] += w; for(auto [w_i,l_i] : dp[i]){ int actual_w = w_i + ew[i]; int actual_l = l_i + el[i]; int put_j = actual_w - ew[j]; int put_l = actual_l - el[j]; if(dp[j].count(put_j)){ ckmin(dp[j][put_j],put_l); }else{ dp[j][put_j] = put_l; } } // swap dp[i].swap(dp[j]); dp[j].clear(); ew[i] = ew[j]; el[i] = el[j]; } } }; dfs(0,0); if(ans == N) ans = -1; return ans; } /* #include <stdio.h> #include <stdlib.h> #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(2==scanf("%d %d",&H[i][0],&H[i][1])); for(i=0; i<N-1; i++) my_assert(1==scanf("%d",&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; } 3 3 0 1 1 2 1 1 -1 11 12 0 1 0 2 2 3 3 4 4 5 0 6 6 7 6 8 8 9 8 10 3 4 5 4 6 3 2 5 6 7 2 */

컴파일 시 표준 에러 (stderr) 메시지

race.cpp: In function 'void setIO(std::string)':
race.cpp:84:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |    freopen((s+".in").c_str(),"r",stdin);
      |    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
race.cpp:85:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   85 |    freopen((s+".out").c_str(),"w",stdout);
      |    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...