답안 #29272

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
29272 2017-07-18T21:50:11 Z inqr 경주 (Race) (IOI11_race) C++14
100 / 100
2016 ms 42980 KB
#include "race.h"
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define rt insert
#define st first
#define nd second
#define DB printf("debug\n");
using namespace std;
int n,k,ans=1e9;
vector < pair < int , int > > ed[200005];
vector < bool > cenbef(200005,0);
vector < int  > subcnt(200005,0);
int sub_count(int vn,int vb){//kendisi dahil
	subcnt[vn]=1;
	for(int i=0;i<ed[vn].size();i++){
		int vnx=ed[vn][i].st;
		if(!cenbef[vnx]&&vnx!=vb){
			subcnt[vn]+=sub_count(vnx,vn);
		}
	}
	return subcnt[vn];
}
int find_cen(int vn,int vb,int ts){
	for(int i=0;i<ed[vn].size();i++){
		int vnx=ed[vn][i].st;
		if(!cenbef[vnx]&&vnx!=vb&&subcnt[vnx]>ts/2)
			return find_cen(vnx,vn,ts);
	}
	return vn;
}
void merge(map<int,int>&a,map<int,int>&b){
	map<int,int>::iterator it;
	for(it=b.begin();it!=b.end();it++)
		if(a.find(k-it->st)!=a.end())
			ans=min(ans,a[k-it->st]+it->nd);
	for(it=b.begin();it!=b.end();it++)
		if(a.find(it->st)==a.end())
			a[it->st]=it->nd;
		else a[it->st]=min(a[it->st],it->nd);
}
void dfs(int vn,int vb,int rn,int wn,map<int,int>&sbtm){
	if(wn>k)return;
	if(sbtm.find(wn)==sbtm.end())sbtm[wn]=rn;
	else sbtm[wn]=min(sbtm[wn],rn);
	for(int i=0;i<ed[vn].size();i++){
		int vnx=ed[vn][i].st,edw=ed[vn][i].nd;
		if(!cenbef[vnx]&&vnx!=vb)
			dfs(vnx,vn,rn+1,wn+edw,sbtm);
	}
}
void rou_find(int vn){
	map < int , int > allm;
	allm[0]=0;
	for(int i=0;i<ed[vn].size();i++){
		int vnx=ed[vn][i].st;
		int edl=ed[vn][i].nd;
		if(!cenbef[vnx]){
			map < int , int > sbtm;
			dfs(vnx,vn,1,edl,sbtm);
			merge(allm,sbtm);
		}
	}
}
void solve(int vn){
	sub_count(vn,-1);
	int cen=find_cen(vn,-1,subcnt[vn]);
	cenbef[cen]=1;
	rou_find(cen);
	for(int i=0;i<ed[cen].size();i++){
		int vnx=ed[cen][i].st;
		if(!cenbef[vnx])
			solve(vnx);
	}
}
int best_path(int N, int K, int H[][2], int L[])
{
	n=N;k=K;
  	for(int i=0;i<N-1;i++){
  		ed[H[i][0]].push_back(make_pair(H[i][1],L[i]));
  		ed[H[i][1]].push_back(make_pair(H[i][0],L[i]));
  	}
  	solve(0);
  	if(ans==1e9)
  		return -1;
  	else return ans;
}

Compilation message

race.cpp: In function 'int sub_count(int, int)':
race.cpp:16:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<ed[vn].size();i++){
              ~^~~~~~~~~~~~~~
race.cpp: In function 'int find_cen(int, int, int)':
race.cpp:25:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<ed[vn].size();i++){
              ~^~~~~~~~~~~~~~
race.cpp: In function 'void dfs(int, int, int, int, std::map<int, int>&)':
race.cpp:46:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<ed[vn].size();i++){
              ~^~~~~~~~~~~~~~
race.cpp: In function 'void rou_find(int)':
race.cpp:55:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<ed[vn].size();i++){
              ~^~~~~~~~~~~~~~
race.cpp: In function 'void solve(int)':
race.cpp:70:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<ed[cen].size();i++){
              ~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5880 KB Output is correct
2 Correct 6 ms 5992 KB Output is correct
3 Correct 7 ms 5992 KB Output is correct
4 Correct 7 ms 6096 KB Output is correct
5 Correct 7 ms 6096 KB Output is correct
6 Correct 8 ms 6096 KB Output is correct
7 Correct 6 ms 6096 KB Output is correct
8 Correct 6 ms 6096 KB Output is correct
9 Correct 6 ms 6096 KB Output is correct
10 Correct 6 ms 6096 KB Output is correct
11 Correct 7 ms 6096 KB Output is correct
12 Correct 6 ms 6096 KB Output is correct
13 Correct 6 ms 6096 KB Output is correct
14 Correct 6 ms 6096 KB Output is correct
15 Correct 7 ms 6096 KB Output is correct
16 Correct 8 ms 6096 KB Output is correct
17 Correct 7 ms 6100 KB Output is correct
18 Correct 6 ms 6100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5880 KB Output is correct
2 Correct 6 ms 5992 KB Output is correct
3 Correct 7 ms 5992 KB Output is correct
4 Correct 7 ms 6096 KB Output is correct
5 Correct 7 ms 6096 KB Output is correct
6 Correct 8 ms 6096 KB Output is correct
7 Correct 6 ms 6096 KB Output is correct
8 Correct 6 ms 6096 KB Output is correct
9 Correct 6 ms 6096 KB Output is correct
10 Correct 6 ms 6096 KB Output is correct
11 Correct 7 ms 6096 KB Output is correct
12 Correct 6 ms 6096 KB Output is correct
13 Correct 6 ms 6096 KB Output is correct
14 Correct 6 ms 6096 KB Output is correct
15 Correct 7 ms 6096 KB Output is correct
16 Correct 8 ms 6096 KB Output is correct
17 Correct 7 ms 6100 KB Output is correct
18 Correct 6 ms 6100 KB Output is correct
19 Correct 7 ms 6100 KB Output is correct
20 Correct 7 ms 6100 KB Output is correct
21 Correct 10 ms 6100 KB Output is correct
22 Correct 8 ms 6100 KB Output is correct
23 Correct 9 ms 6100 KB Output is correct
24 Correct 8 ms 6100 KB Output is correct
25 Correct 9 ms 6100 KB Output is correct
26 Correct 9 ms 6100 KB Output is correct
27 Correct 10 ms 6100 KB Output is correct
28 Correct 8 ms 6168 KB Output is correct
29 Correct 8 ms 6168 KB Output is correct
30 Correct 8 ms 6168 KB Output is correct
31 Correct 8 ms 6168 KB Output is correct
32 Correct 9 ms 6168 KB Output is correct
33 Correct 8 ms 6168 KB Output is correct
34 Correct 9 ms 6168 KB Output is correct
35 Correct 9 ms 6172 KB Output is correct
36 Correct 9 ms 6172 KB Output is correct
37 Correct 9 ms 6172 KB Output is correct
38 Correct 8 ms 6184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5880 KB Output is correct
2 Correct 6 ms 5992 KB Output is correct
3 Correct 7 ms 5992 KB Output is correct
4 Correct 7 ms 6096 KB Output is correct
5 Correct 7 ms 6096 KB Output is correct
6 Correct 8 ms 6096 KB Output is correct
7 Correct 6 ms 6096 KB Output is correct
8 Correct 6 ms 6096 KB Output is correct
9 Correct 6 ms 6096 KB Output is correct
10 Correct 6 ms 6096 KB Output is correct
11 Correct 7 ms 6096 KB Output is correct
12 Correct 6 ms 6096 KB Output is correct
13 Correct 6 ms 6096 KB Output is correct
14 Correct 6 ms 6096 KB Output is correct
15 Correct 7 ms 6096 KB Output is correct
16 Correct 8 ms 6096 KB Output is correct
17 Correct 7 ms 6100 KB Output is correct
18 Correct 6 ms 6100 KB Output is correct
19 Correct 293 ms 10980 KB Output is correct
20 Correct 299 ms 10980 KB Output is correct
21 Correct 337 ms 10980 KB Output is correct
22 Correct 328 ms 11108 KB Output is correct
23 Correct 163 ms 11228 KB Output is correct
24 Correct 205 ms 11228 KB Output is correct
25 Correct 290 ms 14516 KB Output is correct
26 Correct 153 ms 18232 KB Output is correct
27 Correct 373 ms 18232 KB Output is correct
28 Correct 494 ms 30436 KB Output is correct
29 Correct 559 ms 30436 KB Output is correct
30 Correct 357 ms 30436 KB Output is correct
31 Correct 364 ms 30436 KB Output is correct
32 Correct 383 ms 30436 KB Output is correct
33 Correct 677 ms 30436 KB Output is correct
34 Correct 407 ms 30436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5880 KB Output is correct
2 Correct 6 ms 5992 KB Output is correct
3 Correct 7 ms 5992 KB Output is correct
4 Correct 7 ms 6096 KB Output is correct
5 Correct 7 ms 6096 KB Output is correct
6 Correct 8 ms 6096 KB Output is correct
7 Correct 6 ms 6096 KB Output is correct
8 Correct 6 ms 6096 KB Output is correct
9 Correct 6 ms 6096 KB Output is correct
10 Correct 6 ms 6096 KB Output is correct
11 Correct 7 ms 6096 KB Output is correct
12 Correct 6 ms 6096 KB Output is correct
13 Correct 6 ms 6096 KB Output is correct
14 Correct 6 ms 6096 KB Output is correct
15 Correct 7 ms 6096 KB Output is correct
16 Correct 8 ms 6096 KB Output is correct
17 Correct 7 ms 6100 KB Output is correct
18 Correct 6 ms 6100 KB Output is correct
19 Correct 7 ms 6100 KB Output is correct
20 Correct 7 ms 6100 KB Output is correct
21 Correct 10 ms 6100 KB Output is correct
22 Correct 8 ms 6100 KB Output is correct
23 Correct 9 ms 6100 KB Output is correct
24 Correct 8 ms 6100 KB Output is correct
25 Correct 9 ms 6100 KB Output is correct
26 Correct 9 ms 6100 KB Output is correct
27 Correct 10 ms 6100 KB Output is correct
28 Correct 8 ms 6168 KB Output is correct
29 Correct 8 ms 6168 KB Output is correct
30 Correct 8 ms 6168 KB Output is correct
31 Correct 8 ms 6168 KB Output is correct
32 Correct 9 ms 6168 KB Output is correct
33 Correct 8 ms 6168 KB Output is correct
34 Correct 9 ms 6168 KB Output is correct
35 Correct 9 ms 6172 KB Output is correct
36 Correct 9 ms 6172 KB Output is correct
37 Correct 9 ms 6172 KB Output is correct
38 Correct 8 ms 6184 KB Output is correct
39 Correct 293 ms 10980 KB Output is correct
40 Correct 299 ms 10980 KB Output is correct
41 Correct 337 ms 10980 KB Output is correct
42 Correct 328 ms 11108 KB Output is correct
43 Correct 163 ms 11228 KB Output is correct
44 Correct 205 ms 11228 KB Output is correct
45 Correct 290 ms 14516 KB Output is correct
46 Correct 153 ms 18232 KB Output is correct
47 Correct 373 ms 18232 KB Output is correct
48 Correct 494 ms 30436 KB Output is correct
49 Correct 559 ms 30436 KB Output is correct
50 Correct 357 ms 30436 KB Output is correct
51 Correct 364 ms 30436 KB Output is correct
52 Correct 383 ms 30436 KB Output is correct
53 Correct 677 ms 30436 KB Output is correct
54 Correct 407 ms 30436 KB Output is correct
55 Correct 50 ms 30436 KB Output is correct
56 Correct 32 ms 30436 KB Output is correct
57 Correct 229 ms 30436 KB Output is correct
58 Correct 83 ms 30436 KB Output is correct
59 Correct 633 ms 30436 KB Output is correct
60 Correct 2016 ms 42980 KB Output is correct
61 Correct 492 ms 42980 KB Output is correct
62 Correct 557 ms 42980 KB Output is correct
63 Correct 696 ms 42980 KB Output is correct
64 Correct 1621 ms 42980 KB Output is correct
65 Correct 371 ms 42980 KB Output is correct
66 Correct 1058 ms 42980 KB Output is correct
67 Correct 198 ms 42980 KB Output is correct
68 Correct 653 ms 42980 KB Output is correct
69 Correct 609 ms 42980 KB Output is correct
70 Correct 625 ms 42980 KB Output is correct