답안 #14671

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
14671 2015-06-01T15:00:41 Z minsu 경주 (Race) (IOI11_race) C++14
100 / 100
1080 ms 28488 KB
#include <bits/stdc++.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define RREP(I, N) for (int I= (N)-1; I>=0; --I)
#define RREPP(I, A, B) for(int I = (B)-1; I>=A; --I)
#define CASET int ___T, case_n = 1; scanf("%d ", &___T); while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define debug(A) REP(i, A.size()) cerr << A[i] << ' '; cerr << '\n'
using namespace std;
 
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
 
typedef long long lld;
typedef unsigned long long ulld;
typedef pair<lld, lld> plld;
typedef vector<lld> vlld;
typedef vector<vlld> vvlld;
 
typedef long double ld;
typedef vector<ld> vld;
typedef vector<vld> vvld;

const int MAXN=5e5+10;
const int MAXK=1e6+10;
const int INF=987654321;
struct Edge{ int to, co; };
typedef vector< vector<Edge> > vve;
vve linked; vi deleted;
int n, k, size[MAXN], par[MAXN], A[MAXK], AR[MAXK];

int solve(int root){
	struct Sres{ int now, nowk, len, iter, par; };
	int ret=INF;
	for(auto it : linked[root]){
		if( deleted[it.to] ) continue;
		vii paths; stack<Sres> st; st.push({it.to, it.co, 1, 0, root});
		while(!st.empty()){
			Sres& here=st.top();
			if(SZ(linked[here.now]) == here.iter) {
				paths.PB( {here.nowk, here.len} );
				st.pop(); continue;
			}
			Edge there=linked[here.now][here.iter]; here.iter++;
			if( there.to==here.par || deleted[there.to] ) { continue; }
			if( here.nowk + there.co <= k )
				st.push({there.to, here.nowk + there.co, here.len+1, 0, here.now});
		}
		for(auto it : paths)
			if(AR[k-it.F]==root && A[k-it.F]) ret=min(ret, A[k-it.F] + it.S);
		for(auto it : paths){
			A[it.F]=min( ( ( AR[it.F] != root || A[it.F]==0 ) ? INF : A[it.F] ), it.S);
			AR[it.F]=root;
		}
	} if(AR[k]==root && A[k]) ret=min(ret, A[k]);
	return ret;
}
int dnc(int root){
	struct Sres{ int now, iter; };
	vi vs; stack<Sres> st; st.push({root, 0}); par[root]=-1; size[root]=1;
	while(!st.empty()){
		Sres& here=st.top();
		if(SZ(linked[here.now])==here.iter){
			vs.PB(here.now);
			if(par[here.now]!=-1) size[par[here.now]]+=size[here.now];
			st.pop(); continue;
		}
		Edge there=linked[here.now][here.iter]; here.iter++;
		if(deleted[there.to] || there.to==par[here.now]) { continue; }
		par[there.to]=here.now; st.push({there.to, 0}); size[there.to]=1;
	}
	int sn=SZ(vs), nroot=-1;
	if(sn==1) return INF;
	for(auto it : vs){
		int maxi=0;
		for(auto there : linked[it]){
			if(deleted[there.to]) continue;
			if(par[it] == there.to) maxi=max(maxi, sn-size[it]);
			else maxi=max(maxi, size[there.to]);
		}
		if(maxi<=sn/2) { nroot=it; break; }
	} assert(nroot!=-1);
	int ret=solve(nroot); deleted[nroot]=1;
	for(auto it : linked[nroot]){
		if(deleted[it.to]) continue;
		ret=min(ret, dnc(it.to));
	}
	return ret;
}
int best_path(int N, int K, int H[][2], int L[]){
	vve(N).swap(linked); vi(N, 0).swap(deleted); MS0(A); MS0(AR);
	for(int i=0;i<N-1;i++){
		linked[H[i][0]].PB({H[i][1], L[i]});
		linked[H[i][1]].PB({H[i][0], L[i]});
	} n=N, k=K;
	int ret=dnc(0);
	return ( ret==INF )? -1 : ret;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 8184 KB Output is correct
2 Correct 10 ms 8360 KB Output is correct
3 Correct 8 ms 8360 KB Output is correct
4 Correct 8 ms 8360 KB Output is correct
5 Correct 7 ms 8360 KB Output is correct
6 Correct 9 ms 8360 KB Output is correct
7 Correct 7 ms 8360 KB Output is correct
8 Correct 7 ms 8388 KB Output is correct
9 Correct 7 ms 8388 KB Output is correct
10 Correct 9 ms 8404 KB Output is correct
11 Correct 8 ms 8404 KB Output is correct
12 Correct 8 ms 8404 KB Output is correct
13 Correct 8 ms 8404 KB Output is correct
14 Correct 8 ms 8404 KB Output is correct
15 Correct 8 ms 8404 KB Output is correct
16 Correct 9 ms 8404 KB Output is correct
17 Correct 8 ms 8404 KB Output is correct
18 Correct 7 ms 8404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 8184 KB Output is correct
2 Correct 10 ms 8360 KB Output is correct
3 Correct 8 ms 8360 KB Output is correct
4 Correct 8 ms 8360 KB Output is correct
5 Correct 7 ms 8360 KB Output is correct
6 Correct 9 ms 8360 KB Output is correct
7 Correct 7 ms 8360 KB Output is correct
8 Correct 7 ms 8388 KB Output is correct
9 Correct 7 ms 8388 KB Output is correct
10 Correct 9 ms 8404 KB Output is correct
11 Correct 8 ms 8404 KB Output is correct
12 Correct 8 ms 8404 KB Output is correct
13 Correct 8 ms 8404 KB Output is correct
14 Correct 8 ms 8404 KB Output is correct
15 Correct 8 ms 8404 KB Output is correct
16 Correct 9 ms 8404 KB Output is correct
17 Correct 8 ms 8404 KB Output is correct
18 Correct 7 ms 8404 KB Output is correct
19 Correct 8 ms 8404 KB Output is correct
20 Correct 8 ms 8404 KB Output is correct
21 Correct 9 ms 8528 KB Output is correct
22 Correct 12 ms 8528 KB Output is correct
23 Correct 10 ms 8528 KB Output is correct
24 Correct 11 ms 8528 KB Output is correct
25 Correct 11 ms 8528 KB Output is correct
26 Correct 11 ms 8528 KB Output is correct
27 Correct 10 ms 8528 KB Output is correct
28 Correct 12 ms 8572 KB Output is correct
29 Correct 9 ms 8572 KB Output is correct
30 Correct 10 ms 8572 KB Output is correct
31 Correct 10 ms 8572 KB Output is correct
32 Correct 10 ms 8572 KB Output is correct
33 Correct 11 ms 8572 KB Output is correct
34 Correct 10 ms 8572 KB Output is correct
35 Correct 10 ms 8572 KB Output is correct
36 Correct 10 ms 8572 KB Output is correct
37 Correct 13 ms 8572 KB Output is correct
38 Correct 10 ms 8572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 8184 KB Output is correct
2 Correct 10 ms 8360 KB Output is correct
3 Correct 8 ms 8360 KB Output is correct
4 Correct 8 ms 8360 KB Output is correct
5 Correct 7 ms 8360 KB Output is correct
6 Correct 9 ms 8360 KB Output is correct
7 Correct 7 ms 8360 KB Output is correct
8 Correct 7 ms 8388 KB Output is correct
9 Correct 7 ms 8388 KB Output is correct
10 Correct 9 ms 8404 KB Output is correct
11 Correct 8 ms 8404 KB Output is correct
12 Correct 8 ms 8404 KB Output is correct
13 Correct 8 ms 8404 KB Output is correct
14 Correct 8 ms 8404 KB Output is correct
15 Correct 8 ms 8404 KB Output is correct
16 Correct 9 ms 8404 KB Output is correct
17 Correct 8 ms 8404 KB Output is correct
18 Correct 7 ms 8404 KB Output is correct
19 Correct 407 ms 17784 KB Output is correct
20 Correct 441 ms 17864 KB Output is correct
21 Correct 362 ms 17936 KB Output is correct
22 Correct 431 ms 17936 KB Output is correct
23 Correct 298 ms 17936 KB Output is correct
24 Correct 223 ms 17936 KB Output is correct
25 Correct 410 ms 17936 KB Output is correct
26 Correct 245 ms 18512 KB Output is correct
27 Correct 472 ms 26308 KB Output is correct
28 Correct 737 ms 26532 KB Output is correct
29 Correct 818 ms 26592 KB Output is correct
30 Correct 387 ms 26592 KB Output is correct
31 Correct 379 ms 26592 KB Output is correct
32 Correct 477 ms 26592 KB Output is correct
33 Correct 800 ms 26592 KB Output is correct
34 Correct 695 ms 26592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 8184 KB Output is correct
2 Correct 10 ms 8360 KB Output is correct
3 Correct 8 ms 8360 KB Output is correct
4 Correct 8 ms 8360 KB Output is correct
5 Correct 7 ms 8360 KB Output is correct
6 Correct 9 ms 8360 KB Output is correct
7 Correct 7 ms 8360 KB Output is correct
8 Correct 7 ms 8388 KB Output is correct
9 Correct 7 ms 8388 KB Output is correct
10 Correct 9 ms 8404 KB Output is correct
11 Correct 8 ms 8404 KB Output is correct
12 Correct 8 ms 8404 KB Output is correct
13 Correct 8 ms 8404 KB Output is correct
14 Correct 8 ms 8404 KB Output is correct
15 Correct 8 ms 8404 KB Output is correct
16 Correct 9 ms 8404 KB Output is correct
17 Correct 8 ms 8404 KB Output is correct
18 Correct 7 ms 8404 KB Output is correct
19 Correct 8 ms 8404 KB Output is correct
20 Correct 8 ms 8404 KB Output is correct
21 Correct 9 ms 8528 KB Output is correct
22 Correct 12 ms 8528 KB Output is correct
23 Correct 10 ms 8528 KB Output is correct
24 Correct 11 ms 8528 KB Output is correct
25 Correct 11 ms 8528 KB Output is correct
26 Correct 11 ms 8528 KB Output is correct
27 Correct 10 ms 8528 KB Output is correct
28 Correct 12 ms 8572 KB Output is correct
29 Correct 9 ms 8572 KB Output is correct
30 Correct 10 ms 8572 KB Output is correct
31 Correct 10 ms 8572 KB Output is correct
32 Correct 10 ms 8572 KB Output is correct
33 Correct 11 ms 8572 KB Output is correct
34 Correct 10 ms 8572 KB Output is correct
35 Correct 10 ms 8572 KB Output is correct
36 Correct 10 ms 8572 KB Output is correct
37 Correct 13 ms 8572 KB Output is correct
38 Correct 10 ms 8572 KB Output is correct
39 Correct 407 ms 17784 KB Output is correct
40 Correct 441 ms 17864 KB Output is correct
41 Correct 362 ms 17936 KB Output is correct
42 Correct 431 ms 17936 KB Output is correct
43 Correct 298 ms 17936 KB Output is correct
44 Correct 223 ms 17936 KB Output is correct
45 Correct 410 ms 17936 KB Output is correct
46 Correct 245 ms 18512 KB Output is correct
47 Correct 472 ms 26308 KB Output is correct
48 Correct 737 ms 26532 KB Output is correct
49 Correct 818 ms 26592 KB Output is correct
50 Correct 387 ms 26592 KB Output is correct
51 Correct 379 ms 26592 KB Output is correct
52 Correct 477 ms 26592 KB Output is correct
53 Correct 800 ms 26592 KB Output is correct
54 Correct 695 ms 26592 KB Output is correct
55 Correct 29 ms 26592 KB Output is correct
56 Correct 29 ms 26592 KB Output is correct
57 Correct 260 ms 26592 KB Output is correct
58 Correct 91 ms 26592 KB Output is correct
59 Correct 252 ms 26592 KB Output is correct
60 Correct 986 ms 28488 KB Output is correct
61 Correct 406 ms 28488 KB Output is correct
62 Correct 424 ms 28488 KB Output is correct
63 Correct 539 ms 28488 KB Output is correct
64 Correct 1016 ms 28488 KB Output is correct
65 Correct 621 ms 28488 KB Output is correct
66 Correct 1080 ms 28488 KB Output is correct
67 Correct 298 ms 28488 KB Output is correct
68 Correct 622 ms 28488 KB Output is correct
69 Correct 576 ms 28488 KB Output is correct
70 Correct 583 ms 28488 KB Output is correct