답안 #29424

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
29424 2017-07-19T10:37:07 Z kavun 경주 (Race) (IOI11_race) C++14
100 / 100
2219 ms 40060 KB
#include "race.h"
#include <bits/stdc++.h>
#define pb push_back

using namespace std;
typedef pair<int,int> ii;
typedef vector<ii> vii;
bool mk[200010];
int n,k;
vii adj[200010];
int sub[200010];
int ans = 1e9, cnt;


int subsize(int v, int par)
{
  int sum = 0;
  for(int i = 0; i < adj[v].size(); i++)
    if(adj[v][i].first != par && !mk[adj[v][i].first])
      sum += subsize(adj[v][i].first,v);

  sub[v] = sum + 1;
  return sum + 1;
}

int centroid(int v, int par, int sz)
{
  for(int i = 0; i < adj[v].size(); i++)
    if(adj[v][i].first != par && !mk[adj[v][i].first])
      {
	int u = adj[v][i].first;
	if(sub[u] > sz/2)
	  return centroid(u,v,sz);
      }
  return v;
}

void dfs(int v, int par, int d, int len, map<int,int>& m)
{
  if(len > k)
    return;
  if(m.find(len) != m.end())
    m[len] = min(m[len],d);
  else 
    m[len] = d;
  for(int i = 0; i < adj[v].size(); i++)
    {
      int u = adj[v][i].first;
      int l = adj[v][i].second;
      if(u != par && !mk[u])
	dfs(u,v,d+1,len + l, m);
    }
}

void solveForCen(int v)
{
  map<int,int> mp;
  mp[0] = 0;
  for(int i = 0;i < adj[v].size(); i++)
    {
      if(!mk[adj[v][i].first])
	{
	  map<int,int> m;
	  int u = adj[v][i].first;
	  dfs(u,v,1,adj[v][i].second,m);
	  for(map<int,int>::iterator it = m.begin(); it != m.end(); it++)
	    {
	      int len = it->first;
	      if(mp.find(k - len) != mp.end())
		ans = min(ans,m[len] + mp[k - len]);
	    }
	  for(map<int,int>::iterator it = m.begin(); it != m.end(); it++)
	    {
	      int len = it->first;
	      if(mp.find(len) != mp.end())
		mp[len] = min(mp[len],m[len]);
	      else
		mp[len] = m[len];
	    }
	}
    }
}

int solve(int v)
{
  subsize(v,v);
  int cen = centroid(v,v,sub[v]);
  mk[cen] = true;
  solveForCen(cen);
  for(int i = 0; i < adj[cen].size(); i++)
    {
      int u = adj[cen][i].first;
      if(!mk[u])
	solve(u);
    }
}



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 v = H[i][0],u = H[i][1],l = L[i];
      adj[v].pb(ii(u,l));
      adj[u].pb(ii(v,l));
    }

  solve(0);
  if(ans == 1e9)
    return -1;
  else 
    return ans;
}

Compilation message

race.cpp: In function 'int subsize(int, int)':
race.cpp:18:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < adj[v].size(); i++)
                  ~~^~~~~~~~~~~~~~~
race.cpp: In function 'int centroid(int, int, int)':
race.cpp:28:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < adj[v].size(); i++)
                  ~~^~~~~~~~~~~~~~~
race.cpp: In function 'void dfs(int, int, int, int, std::map<int, int>&)':
race.cpp:46:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < adj[v].size(); i++)
                  ~~^~~~~~~~~~~~~~~
race.cpp: In function 'void solveForCen(int)':
race.cpp:59:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0;i < adj[v].size(); i++)
                 ~~^~~~~~~~~~~~~~~
race.cpp: In function 'int solve(int)':
race.cpp:90:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < adj[cen].size(); i++)
                  ~~^~~~~~~~~~~~~~~~~
race.cpp:96:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4984 KB Output is correct
2 Correct 6 ms 5224 KB Output is correct
3 Correct 6 ms 5224 KB Output is correct
4 Correct 5 ms 5224 KB Output is correct
5 Correct 5 ms 5224 KB Output is correct
6 Correct 6 ms 5224 KB Output is correct
7 Correct 6 ms 5224 KB Output is correct
8 Correct 6 ms 5228 KB Output is correct
9 Correct 6 ms 5288 KB Output is correct
10 Correct 6 ms 5288 KB Output is correct
11 Correct 6 ms 5288 KB Output is correct
12 Correct 5 ms 5288 KB Output is correct
13 Correct 7 ms 5288 KB Output is correct
14 Correct 6 ms 5296 KB Output is correct
15 Correct 6 ms 5312 KB Output is correct
16 Correct 6 ms 5312 KB Output is correct
17 Correct 6 ms 5312 KB Output is correct
18 Correct 6 ms 5312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4984 KB Output is correct
2 Correct 6 ms 5224 KB Output is correct
3 Correct 6 ms 5224 KB Output is correct
4 Correct 5 ms 5224 KB Output is correct
5 Correct 5 ms 5224 KB Output is correct
6 Correct 6 ms 5224 KB Output is correct
7 Correct 6 ms 5224 KB Output is correct
8 Correct 6 ms 5228 KB Output is correct
9 Correct 6 ms 5288 KB Output is correct
10 Correct 6 ms 5288 KB Output is correct
11 Correct 6 ms 5288 KB Output is correct
12 Correct 5 ms 5288 KB Output is correct
13 Correct 7 ms 5288 KB Output is correct
14 Correct 6 ms 5296 KB Output is correct
15 Correct 6 ms 5312 KB Output is correct
16 Correct 6 ms 5312 KB Output is correct
17 Correct 6 ms 5312 KB Output is correct
18 Correct 6 ms 5312 KB Output is correct
19 Correct 7 ms 5312 KB Output is correct
20 Correct 6 ms 5312 KB Output is correct
21 Correct 10 ms 5348 KB Output is correct
22 Correct 6 ms 5348 KB Output is correct
23 Correct 7 ms 5356 KB Output is correct
24 Correct 7 ms 5360 KB Output is correct
25 Correct 8 ms 5484 KB Output is correct
26 Correct 6 ms 5484 KB Output is correct
27 Correct 6 ms 5484 KB Output is correct
28 Correct 8 ms 5484 KB Output is correct
29 Correct 8 ms 5484 KB Output is correct
30 Correct 9 ms 5484 KB Output is correct
31 Correct 9 ms 5484 KB Output is correct
32 Correct 9 ms 5484 KB Output is correct
33 Correct 10 ms 5484 KB Output is correct
34 Correct 9 ms 5484 KB Output is correct
35 Correct 8 ms 5484 KB Output is correct
36 Correct 8 ms 5484 KB Output is correct
37 Correct 8 ms 5484 KB Output is correct
38 Correct 8 ms 5516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4984 KB Output is correct
2 Correct 6 ms 5224 KB Output is correct
3 Correct 6 ms 5224 KB Output is correct
4 Correct 5 ms 5224 KB Output is correct
5 Correct 5 ms 5224 KB Output is correct
6 Correct 6 ms 5224 KB Output is correct
7 Correct 6 ms 5224 KB Output is correct
8 Correct 6 ms 5228 KB Output is correct
9 Correct 6 ms 5288 KB Output is correct
10 Correct 6 ms 5288 KB Output is correct
11 Correct 6 ms 5288 KB Output is correct
12 Correct 5 ms 5288 KB Output is correct
13 Correct 7 ms 5288 KB Output is correct
14 Correct 6 ms 5296 KB Output is correct
15 Correct 6 ms 5312 KB Output is correct
16 Correct 6 ms 5312 KB Output is correct
17 Correct 6 ms 5312 KB Output is correct
18 Correct 6 ms 5312 KB Output is correct
19 Correct 346 ms 10712 KB Output is correct
20 Correct 355 ms 10748 KB Output is correct
21 Correct 301 ms 10748 KB Output is correct
22 Correct 374 ms 10748 KB Output is correct
23 Correct 170 ms 10988 KB Output is correct
24 Correct 155 ms 10988 KB Output is correct
25 Correct 248 ms 13436 KB Output is correct
26 Correct 135 ms 16364 KB Output is correct
27 Correct 311 ms 16364 KB Output is correct
28 Correct 472 ms 27468 KB Output is correct
29 Correct 437 ms 27468 KB Output is correct
30 Correct 373 ms 27468 KB Output is correct
31 Correct 381 ms 27468 KB Output is correct
32 Correct 403 ms 27468 KB Output is correct
33 Correct 622 ms 27468 KB Output is correct
34 Correct 275 ms 27468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4984 KB Output is correct
2 Correct 6 ms 5224 KB Output is correct
3 Correct 6 ms 5224 KB Output is correct
4 Correct 5 ms 5224 KB Output is correct
5 Correct 5 ms 5224 KB Output is correct
6 Correct 6 ms 5224 KB Output is correct
7 Correct 6 ms 5224 KB Output is correct
8 Correct 6 ms 5228 KB Output is correct
9 Correct 6 ms 5288 KB Output is correct
10 Correct 6 ms 5288 KB Output is correct
11 Correct 6 ms 5288 KB Output is correct
12 Correct 5 ms 5288 KB Output is correct
13 Correct 7 ms 5288 KB Output is correct
14 Correct 6 ms 5296 KB Output is correct
15 Correct 6 ms 5312 KB Output is correct
16 Correct 6 ms 5312 KB Output is correct
17 Correct 6 ms 5312 KB Output is correct
18 Correct 6 ms 5312 KB Output is correct
19 Correct 7 ms 5312 KB Output is correct
20 Correct 6 ms 5312 KB Output is correct
21 Correct 10 ms 5348 KB Output is correct
22 Correct 6 ms 5348 KB Output is correct
23 Correct 7 ms 5356 KB Output is correct
24 Correct 7 ms 5360 KB Output is correct
25 Correct 8 ms 5484 KB Output is correct
26 Correct 6 ms 5484 KB Output is correct
27 Correct 6 ms 5484 KB Output is correct
28 Correct 8 ms 5484 KB Output is correct
29 Correct 8 ms 5484 KB Output is correct
30 Correct 9 ms 5484 KB Output is correct
31 Correct 9 ms 5484 KB Output is correct
32 Correct 9 ms 5484 KB Output is correct
33 Correct 10 ms 5484 KB Output is correct
34 Correct 9 ms 5484 KB Output is correct
35 Correct 8 ms 5484 KB Output is correct
36 Correct 8 ms 5484 KB Output is correct
37 Correct 8 ms 5484 KB Output is correct
38 Correct 8 ms 5516 KB Output is correct
39 Correct 346 ms 10712 KB Output is correct
40 Correct 355 ms 10748 KB Output is correct
41 Correct 301 ms 10748 KB Output is correct
42 Correct 374 ms 10748 KB Output is correct
43 Correct 170 ms 10988 KB Output is correct
44 Correct 155 ms 10988 KB Output is correct
45 Correct 248 ms 13436 KB Output is correct
46 Correct 135 ms 16364 KB Output is correct
47 Correct 311 ms 16364 KB Output is correct
48 Correct 472 ms 27468 KB Output is correct
49 Correct 437 ms 27468 KB Output is correct
50 Correct 373 ms 27468 KB Output is correct
51 Correct 381 ms 27468 KB Output is correct
52 Correct 403 ms 27468 KB Output is correct
53 Correct 622 ms 27468 KB Output is correct
54 Correct 275 ms 27468 KB Output is correct
55 Correct 38 ms 27468 KB Output is correct
56 Correct 33 ms 27468 KB Output is correct
57 Correct 204 ms 27468 KB Output is correct
58 Correct 69 ms 27468 KB Output is correct
59 Correct 699 ms 27468 KB Output is correct
60 Correct 2219 ms 40060 KB Output is correct
61 Correct 518 ms 40060 KB Output is correct
62 Correct 602 ms 40060 KB Output is correct
63 Correct 698 ms 40060 KB Output is correct
64 Correct 1874 ms 40060 KB Output is correct
65 Correct 300 ms 40060 KB Output is correct
66 Correct 1209 ms 40060 KB Output is correct
67 Correct 226 ms 40060 KB Output is correct
68 Correct 721 ms 40060 KB Output is correct
69 Correct 810 ms 40060 KB Output is correct
70 Correct 716 ms 40060 KB Output is correct