답안 #1087149

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1087149 2024-09-12T08:52:11 Z quangminh412 경주 (Race) (IOI11_race) C++14
21 / 100
3000 ms 20884 KB
#include <bits/stdc++.h>
using namespace std;

/*
  John Watson
  https://codeforces.com/profile/quangminh98

  Mua Code nhu mua Florentino !!
*/

#define faster() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long

const int maxn = 2e5 + 9;

vector<int> adj[maxn];
int sz[maxn], par[20][maxn], h[maxn];
ll val[maxn], dist[maxn];
int n;
ll k;

void DFS(int u, int p = -1)
{
	sz[u] = 1;
	
	for (int v : adj[u])
	{
		if (v == p) continue;
		
		h[v] = h[u] + 1;
		par[0][v] = u;
		for (int i = 1; i < 20; i++)
			par[i][v] = par[i - 1][par[i - 1][v]];
			
		DFS(v, u);
		
		sz[u] += sz[v];
	}
}

int LCA(int u, int v)
{
	if (h[u] < h[v]) swap(u, v);
	int k = h[u] - h[v];
	
	for (int i = 0; i < 20; i++)
		if (k >> i & 1)
			u = par[i][u];
			
	if (u == v) return u;
	
	for (int i = 19; i >= 0; i--)
		if (par[i][u] != par[i][v])
		{
			u = par[i][u];
			v = par[i][v];
		}
	
	return par[0][u];
}

void DFS_compute(int u, int p = -1)
{
  for (int v : adj[u])
  {
    if (v == p) continue;
    
    dist[v] = dist[u] + val[v];
    DFS_compute(v, 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 u = H[i][0], v = H[i][1];
		u++; v++;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	
	DFS(1);
	for (int i = 0; i < n - 1; i++)
	{
		ll w = l[i];
		int u = H[i][0], v = H[i][1];
		u++; v++;
		val[h[u] > h[v] ? u : v] = w;
	}
  DFS_compute(1);
	
	int ans = 1e9;
	
  for (int u = 1; u <= n; u++)
	  for (int v = u + 1; v <= n; v++) 
	  {
	    int lca = LCA(u, v);
	        
	    if (dist[v] + dist[u] - 2 * dist[lca] == k) ans = min(ans, h[u] + h[v] - 2 * h[lca]);
	   // cout << u - 1 << ' ' << v - 1 << ' ' << dist[v] + dist[u] - 2 * dist[lca] << '\n';
	  }
	
	return (ans == 1e9 ? -1 : ans);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5144 KB Output is correct
4 Correct 4 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 3 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 4 ms 5212 KB Output is correct
10 Correct 3 ms 5208 KB Output is correct
11 Correct 2 ms 5208 KB Output is correct
12 Correct 3 ms 5188 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 2 ms 5296 KB Output is correct
15 Correct 2 ms 5208 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 3 ms 5212 KB Output is correct
18 Correct 2 ms 5212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5144 KB Output is correct
4 Correct 4 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 3 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 4 ms 5212 KB Output is correct
10 Correct 3 ms 5208 KB Output is correct
11 Correct 2 ms 5208 KB Output is correct
12 Correct 3 ms 5188 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 2 ms 5296 KB Output is correct
15 Correct 2 ms 5208 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 3 ms 5212 KB Output is correct
18 Correct 2 ms 5212 KB Output is correct
19 Correct 2 ms 5212 KB Output is correct
20 Correct 2 ms 5212 KB Output is correct
21 Correct 31 ms 5440 KB Output is correct
22 Correct 33 ms 5212 KB Output is correct
23 Correct 32 ms 5440 KB Output is correct
24 Correct 29 ms 5212 KB Output is correct
25 Correct 33 ms 5380 KB Output is correct
26 Correct 28 ms 5212 KB Output is correct
27 Correct 37 ms 5436 KB Output is correct
28 Correct 30 ms 5208 KB Output is correct
29 Correct 29 ms 5212 KB Output is correct
30 Correct 35 ms 5212 KB Output is correct
31 Correct 29 ms 5212 KB Output is correct
32 Correct 29 ms 5448 KB Output is correct
33 Correct 32 ms 5208 KB Output is correct
34 Correct 35 ms 5208 KB Output is correct
35 Correct 31 ms 5212 KB Output is correct
36 Correct 25 ms 5212 KB Output is correct
37 Correct 34 ms 5212 KB Output is correct
38 Correct 35 ms 5212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5144 KB Output is correct
4 Correct 4 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 3 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 4 ms 5212 KB Output is correct
10 Correct 3 ms 5208 KB Output is correct
11 Correct 2 ms 5208 KB Output is correct
12 Correct 3 ms 5188 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 2 ms 5296 KB Output is correct
15 Correct 2 ms 5208 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 3 ms 5212 KB Output is correct
18 Correct 2 ms 5212 KB Output is correct
19 Execution timed out 3072 ms 20884 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5144 KB Output is correct
4 Correct 4 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 3 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 4 ms 5212 KB Output is correct
10 Correct 3 ms 5208 KB Output is correct
11 Correct 2 ms 5208 KB Output is correct
12 Correct 3 ms 5188 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 2 ms 5296 KB Output is correct
15 Correct 2 ms 5208 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 3 ms 5212 KB Output is correct
18 Correct 2 ms 5212 KB Output is correct
19 Correct 2 ms 5212 KB Output is correct
20 Correct 2 ms 5212 KB Output is correct
21 Correct 31 ms 5440 KB Output is correct
22 Correct 33 ms 5212 KB Output is correct
23 Correct 32 ms 5440 KB Output is correct
24 Correct 29 ms 5212 KB Output is correct
25 Correct 33 ms 5380 KB Output is correct
26 Correct 28 ms 5212 KB Output is correct
27 Correct 37 ms 5436 KB Output is correct
28 Correct 30 ms 5208 KB Output is correct
29 Correct 29 ms 5212 KB Output is correct
30 Correct 35 ms 5212 KB Output is correct
31 Correct 29 ms 5212 KB Output is correct
32 Correct 29 ms 5448 KB Output is correct
33 Correct 32 ms 5208 KB Output is correct
34 Correct 35 ms 5208 KB Output is correct
35 Correct 31 ms 5212 KB Output is correct
36 Correct 25 ms 5212 KB Output is correct
37 Correct 34 ms 5212 KB Output is correct
38 Correct 35 ms 5212 KB Output is correct
39 Execution timed out 3072 ms 20884 KB Time limit exceeded
40 Halted 0 ms 0 KB -