Submission #39252078


Source Code Expand

#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
typedef long long ll;
using namespace std;
const int maxn = 1e5 + 5;
int n, sz[maxn], msz[maxn], fa[maxn], S, G;
vector<int> e[maxn];
bool vis[maxn];
void dfs(int u, int from) {
	sz[u] = 1;
	msz[u] = 0;
	for(int v : e[u]) {
		if(v == from || vis[v]) {
			continue;
		}
		dfs(v, u);
		sz[u] += sz[v];
		msz[u] = max(msz[u], sz[v]);
	}
	msz[u] = max(msz[u], S - sz[u]);
	if(!G || msz[u] < msz[G]) {
		G = u;
	}
}
void solve(int u) {
	vis[u] = 1;
	for(int v : e[u]) {
		if(vis[v]) {
			continue;
		}
		dfs(v, u);
		G = 0, S = sz[v];
		dfs(v, u);
		fa[G] = u;
		solve(G);
	}
}
int main() {
#ifdef DEBUG
	freopen("1.in", "r", stdin);
#endif
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;
	for(int i = 1, u, v; i < n; i++) {
		cin >> u >> v;
		e[u].eb(v), e[v].eb(u);
	}
	G = 0, S = n;
	dfs(1, 0);
	solve(G);
	for(int i = 1; i <= n; i++) {
		if(fa[i]) {
			cout << fa[i] << ' ';
		}
		else {
			cout << "-1 ";
		}
	}
	return 0;
}

Submission Info

Submission Time
Task Ex - Balanced Tree
User yanchengzhi
Language C++ (GCC 9.2.1)
Score 600
Code Size 1118 Byte
Status AC
Exec Time 198 ms
Memory 17048 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 2
AC × 45
Set Name Test Cases
Sample sample_01.txt, sample_02.txt
All min.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt, random_30.txt, random_31.txt, random_32.txt, random_33.txt, random_34.txt, random_35.txt, random_36.txt, random_37.txt, random_38.txt, random_39.txt, random_40.txt, random_41.txt, random_42.txt, sample_01.txt, sample_02.txt
Case Name Status Exec Time Memory
min.txt AC 6 ms 5836 KiB
random_01.txt AC 113 ms 10288 KiB
random_02.txt AC 56 ms 8532 KiB
random_03.txt AC 114 ms 10372 KiB
random_04.txt AC 74 ms 9320 KiB
random_05.txt AC 99 ms 10344 KiB
random_06.txt AC 46 ms 7844 KiB
random_07.txt AC 109 ms 10316 KiB
random_08.txt AC 48 ms 7812 KiB
random_09.txt AC 105 ms 10340 KiB
random_10.txt AC 34 ms 7092 KiB
random_11.txt AC 101 ms 10352 KiB
random_12.txt AC 44 ms 7836 KiB
random_13.txt AC 189 ms 14096 KiB
random_14.txt AC 84 ms 11896 KiB
random_15.txt AC 198 ms 17048 KiB
random_16.txt AC 25 ms 7128 KiB
random_17.txt AC 184 ms 14816 KiB
random_18.txt AC 172 ms 14572 KiB
random_19.txt AC 121 ms 10208 KiB
random_20.txt AC 38 ms 7300 KiB
random_21.txt AC 112 ms 10228 KiB
random_22.txt AC 78 ms 9036 KiB
random_23.txt AC 104 ms 10148 KiB
random_24.txt AC 56 ms 8160 KiB
random_25.txt AC 50 ms 10228 KiB
random_26.txt AC 12 ms 6180 KiB
random_27.txt AC 50 ms 10140 KiB
random_28.txt AC 48 ms 9948 KiB
random_29.txt AC 52 ms 10268 KiB
random_30.txt AC 51 ms 10156 KiB
random_31.txt AC 106 ms 10316 KiB
random_32.txt AC 81 ms 9444 KiB
random_33.txt AC 97 ms 10284 KiB
random_34.txt AC 10 ms 5960 KiB
random_35.txt AC 100 ms 10264 KiB
random_36.txt AC 79 ms 9568 KiB
random_37.txt AC 54 ms 10804 KiB
random_38.txt AC 25 ms 7368 KiB
random_39.txt AC 56 ms 10912 KiB
random_40.txt AC 25 ms 8000 KiB
random_41.txt AC 56 ms 10844 KiB
random_42.txt AC 36 ms 8668 KiB
sample_01.txt AC 6 ms 5824 KiB
sample_02.txt AC 5 ms 5880 KiB