Submission #423444

# Submission time Handle Problem Language Result Execution time Memory
423444 2021-06-11T06:37:05 Z codebuster_10 Race (IOI11_race) C++17
100 / 100
485 ms 83776 KB
#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

*/





Compilation message

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 time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 296 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 296 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 296 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 296 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 2 ms 460 KB Output is correct
22 Correct 2 ms 460 KB Output is correct
23 Correct 2 ms 460 KB Output is correct
24 Correct 2 ms 460 KB Output is correct
25 Correct 2 ms 460 KB Output is correct
26 Correct 2 ms 460 KB Output is correct
27 Correct 2 ms 332 KB Output is correct
28 Correct 2 ms 440 KB Output is correct
29 Correct 2 ms 460 KB Output is correct
30 Correct 2 ms 440 KB Output is correct
31 Correct 2 ms 460 KB Output is correct
32 Correct 2 ms 460 KB Output is correct
33 Correct 2 ms 460 KB Output is correct
34 Correct 2 ms 460 KB Output is correct
35 Correct 1 ms 460 KB Output is correct
36 Correct 1 ms 460 KB Output is correct
37 Correct 1 ms 460 KB Output is correct
38 Correct 1 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 296 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 296 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 135 ms 14360 KB Output is correct
20 Correct 133 ms 14332 KB Output is correct
21 Correct 132 ms 14304 KB Output is correct
22 Correct 128 ms 14392 KB Output is correct
23 Correct 160 ms 14748 KB Output is correct
24 Correct 125 ms 14584 KB Output is correct
25 Correct 146 ms 28832 KB Output is correct
26 Correct 82 ms 42052 KB Output is correct
27 Correct 221 ms 28868 KB Output is correct
28 Correct 327 ms 83776 KB Output is correct
29 Correct 325 ms 81048 KB Output is correct
30 Correct 207 ms 28932 KB Output is correct
31 Correct 203 ms 28968 KB Output is correct
32 Correct 304 ms 29136 KB Output is correct
33 Correct 257 ms 28552 KB Output is correct
34 Correct 379 ms 38912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 296 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 296 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 2 ms 460 KB Output is correct
22 Correct 2 ms 460 KB Output is correct
23 Correct 2 ms 460 KB Output is correct
24 Correct 2 ms 460 KB Output is correct
25 Correct 2 ms 460 KB Output is correct
26 Correct 2 ms 460 KB Output is correct
27 Correct 2 ms 332 KB Output is correct
28 Correct 2 ms 440 KB Output is correct
29 Correct 2 ms 460 KB Output is correct
30 Correct 2 ms 440 KB Output is correct
31 Correct 2 ms 460 KB Output is correct
32 Correct 2 ms 460 KB Output is correct
33 Correct 2 ms 460 KB Output is correct
34 Correct 2 ms 460 KB Output is correct
35 Correct 1 ms 460 KB Output is correct
36 Correct 1 ms 460 KB Output is correct
37 Correct 1 ms 460 KB Output is correct
38 Correct 1 ms 460 KB Output is correct
39 Correct 135 ms 14360 KB Output is correct
40 Correct 133 ms 14332 KB Output is correct
41 Correct 132 ms 14304 KB Output is correct
42 Correct 128 ms 14392 KB Output is correct
43 Correct 160 ms 14748 KB Output is correct
44 Correct 125 ms 14584 KB Output is correct
45 Correct 146 ms 28832 KB Output is correct
46 Correct 82 ms 42052 KB Output is correct
47 Correct 221 ms 28868 KB Output is correct
48 Correct 327 ms 83776 KB Output is correct
49 Correct 325 ms 81048 KB Output is correct
50 Correct 207 ms 28932 KB Output is correct
51 Correct 203 ms 28968 KB Output is correct
52 Correct 304 ms 29136 KB Output is correct
53 Correct 257 ms 28552 KB Output is correct
54 Correct 379 ms 38912 KB Output is correct
55 Correct 15 ms 1980 KB Output is correct
56 Correct 8 ms 1708 KB Output is correct
57 Correct 73 ms 14528 KB Output is correct
58 Correct 63 ms 14236 KB Output is correct
59 Correct 115 ms 41836 KB Output is correct
60 Correct 385 ms 81804 KB Output is correct
61 Correct 250 ms 30304 KB Output is correct
62 Correct 211 ms 28948 KB Output is correct
63 Correct 243 ms 29124 KB Output is correct
64 Correct 485 ms 35532 KB Output is correct
65 Correct 481 ms 39164 KB Output is correct
66 Correct 332 ms 74892 KB Output is correct
67 Correct 215 ms 29548 KB Output is correct
68 Correct 352 ms 37364 KB Output is correct
69 Correct 379 ms 40940 KB Output is correct
70 Correct 334 ms 35760 KB Output is correct