Submission #133497

# Submission time Handle Problem Language Result Execution time Memory
133497 2019-07-20T23:11:56 Z KieranHorgan Race (IOI11_race) C++17
0 / 100
8 ms 5112 KB
#include "race.h"
#include <bits/stdc++.h>
using namespace std;

int n, k;
vector<pair<int,int>> AdjList[200005];
bitset<200005> marked;
int sz[200005];

int getSz(int u, int p) {
	sz[u] = 1;
	for(auto v: AdjList[u]) {
		if(v.first==p) continue;
		if(marked[v.first]) continue;
		sz[u] += getSz(v.first, u);
	}
	return sz[u];
}

int getCentroid(int u, int p, int compSz) {
	if(sz[u]*2 < compSz)
		return -1;
	for(auto v: AdjList[u]) {
		if(v.first==p) continue;
		if(marked[v.first]) continue;
		if(sz[v.first]*2 > compSz)
			return getCentroid(v.first, u, compSz);
	}
	return u;
}

int dp[1000005];
int ans = 1<<29;

void checkSecondLeg(int u, int p, int d, int l) {
	ans = min(ans, l + dp[k-d]);
	for(auto v: AdjList[u]) {
		if(v.first==p) continue;
		if(marked[v.first]) continue;
		if(d+v.second > k) continue;
		checkSecondLeg(v.first, u, d+v.second, l+1);
	}
}
void markFirstLeg(int u, int p, int d, int l) {
	dp[d] = l;
	for(auto v: AdjList[u]) {
		if(v.first==p) continue;
		if(marked[v.first]) continue;
		if(d+v.second > k) continue;
		markFirstLeg(v.first, u, d+v.second, l+1);
	}
}
void unmarkFirstLeg(int u, int p, int d, int l) {
	dp[d] = 1<<29;
	for(auto v: AdjList[u]) {
		if(v.first==p) continue;
		if(marked[v.first]) continue;
		if(d+v.second > k) continue;
		unmarkFirstLeg(v.first, u, d+v.second, l+1);
	}
}

int best_path(int N, int K, int H[][2], int L[]) {
	n = N;
	k = K;
	for(int i = 0; i < N-1; i++) {
		AdjList[H[i][0]].push_back({H[i][1], L[i]});
		AdjList[H[i][1]].push_back({H[i][0], L[i]});
	}

	ans = 1<<29;

	for(int i = 1; i <= K; i++)
		dp[i] = 1<<29;

	queue<int> q;
	q.push(0);
	while(!q.empty()) {
		int u = q.front();
		q.pop();

		getSz(u, -1);

		int centre = getCentroid(u, -1, sz[u]);

		for(auto v: AdjList[centre]) {
			if(marked[v.first])
				continue;
			if(v.second > k) continue;
			checkSecondLeg(v.first, centre, v.second, 1);
			markFirstLeg(v.first, centre, v.second, 1);
		}
		for(auto v: AdjList[centre]) {
			if(marked[v.first])
				continue;
			q.push(v.first);
			if(v.second > k) continue;
			unmarkFirstLeg(v.first, centre, v.second, 1);
		}
		marked[centre]=1;
	}

	if(ans == 1<<29)
		ans = -1;
	return ans;
}

# Verdict Execution time Memory Grader output
1 Correct 6 ms 4988 KB Output is correct
2 Correct 7 ms 4984 KB Output is correct
3 Correct 7 ms 5112 KB Output is correct
4 Correct 6 ms 4984 KB Output is correct
5 Correct 6 ms 5112 KB Output is correct
6 Correct 8 ms 4984 KB Output is correct
7 Correct 6 ms 5112 KB Output is correct
8 Correct 7 ms 4984 KB Output is correct
9 Correct 6 ms 4984 KB Output is correct
10 Correct 7 ms 5084 KB Output is correct
11 Correct 6 ms 4984 KB Output is correct
12 Correct 6 ms 5112 KB Output is correct
13 Correct 6 ms 5112 KB Output is correct
14 Correct 6 ms 5112 KB Output is correct
15 Correct 7 ms 5112 KB Output is correct
16 Correct 7 ms 5112 KB Output is correct
17 Incorrect 8 ms 5112 KB Output isn't correct
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4988 KB Output is correct
2 Correct 7 ms 4984 KB Output is correct
3 Correct 7 ms 5112 KB Output is correct
4 Correct 6 ms 4984 KB Output is correct
5 Correct 6 ms 5112 KB Output is correct
6 Correct 8 ms 4984 KB Output is correct
7 Correct 6 ms 5112 KB Output is correct
8 Correct 7 ms 4984 KB Output is correct
9 Correct 6 ms 4984 KB Output is correct
10 Correct 7 ms 5084 KB Output is correct
11 Correct 6 ms 4984 KB Output is correct
12 Correct 6 ms 5112 KB Output is correct
13 Correct 6 ms 5112 KB Output is correct
14 Correct 6 ms 5112 KB Output is correct
15 Correct 7 ms 5112 KB Output is correct
16 Correct 7 ms 5112 KB Output is correct
17 Incorrect 8 ms 5112 KB Output isn't correct
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4988 KB Output is correct
2 Correct 7 ms 4984 KB Output is correct
3 Correct 7 ms 5112 KB Output is correct
4 Correct 6 ms 4984 KB Output is correct
5 Correct 6 ms 5112 KB Output is correct
6 Correct 8 ms 4984 KB Output is correct
7 Correct 6 ms 5112 KB Output is correct
8 Correct 7 ms 4984 KB Output is correct
9 Correct 6 ms 4984 KB Output is correct
10 Correct 7 ms 5084 KB Output is correct
11 Correct 6 ms 4984 KB Output is correct
12 Correct 6 ms 5112 KB Output is correct
13 Correct 6 ms 5112 KB Output is correct
14 Correct 6 ms 5112 KB Output is correct
15 Correct 7 ms 5112 KB Output is correct
16 Correct 7 ms 5112 KB Output is correct
17 Incorrect 8 ms 5112 KB Output isn't correct
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4988 KB Output is correct
2 Correct 7 ms 4984 KB Output is correct
3 Correct 7 ms 5112 KB Output is correct
4 Correct 6 ms 4984 KB Output is correct
5 Correct 6 ms 5112 KB Output is correct
6 Correct 8 ms 4984 KB Output is correct
7 Correct 6 ms 5112 KB Output is correct
8 Correct 7 ms 4984 KB Output is correct
9 Correct 6 ms 4984 KB Output is correct
10 Correct 7 ms 5084 KB Output is correct
11 Correct 6 ms 4984 KB Output is correct
12 Correct 6 ms 5112 KB Output is correct
13 Correct 6 ms 5112 KB Output is correct
14 Correct 6 ms 5112 KB Output is correct
15 Correct 7 ms 5112 KB Output is correct
16 Correct 7 ms 5112 KB Output is correct
17 Incorrect 8 ms 5112 KB Output isn't correct
18 Halted 0 ms 0 KB -