QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#474249#8134. LCA CountingtoanWA 0ms20088kbC++171.6kb2024-07-12 16:55:142024-07-12 16:55:14

Judging History

你现在查看的是最新测评结果

  • [2024-07-12 16:55:14]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:20088kb
  • [2024-07-12 16:55:14]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;
#define int long long
int n, m, u, v, a[200005], subtree[200005], ans[200005];
multiset<int> mp[200005];
vector<int> adj[200005];
void build(int u, int par) {
	subtree[u] = 1;
	for (int node: adj[u]) {
		if(node == par) continue;
		build(node, u);
		subtree[u] += subtree[node];
	}
}
void dfs(int u, int par) {
	int mx=0, p=-1;
	for (int node: adj[u]) {
		if(node == par) continue;
		dfs(node, u);
		if(mx < subtree[node]) mx = subtree[node], p = node;
	}
	if(p==-1) {
		mp[u].insert(0);
		return;
	}
	for (int node: adj[u]) {
		if(node == par || node == p) continue;
		for (auto it: mp[node]) mp[p].insert(it);
		mp[node].clear();
	}
	swap(mp[p], mp[u]);
	if(mp[u].size() >= 2) {
		int num = *(prev(mp[u].end())) + *(prev(prev(mp[u].end())));
		mp[u].erase(prev(mp[u].end()));
		mp[u].erase(prev(mp[u].end()));
		mp[u].insert(num+1);
	}
}
void solve() {
	cin >> n >> m;
	for (int i = 2; i <= n; i++) {
		cin >> u; v = i;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	int cnt = 0;
	for (int i = 1; i <= n; i++) cnt += (adj[i].size()==1 && i != 1 ? 1 : 0);
	build(1, -1);
	dfs(1,-1);
	int sl = 0;
	ans[1] = 1;
	for (auto i = mp[1].rbegin(); i != mp[1].rend(); i++) {
		int num = *i;
		for (int j = 0; j <= num; j++) ans[sl+1+j] = ans[sl]+1+2*j;
		sl += 1 + num;
	}
	for (int i = 1; i <= cnt; i++) cout << ans[i] << ' ';
}

signed main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	if(fopen("input.inp","r")){
		freopen("input.inp", "r", stdin);
		freopen("output.out", "w", stdout);
	}
	int t = 1;
	// cin >> t;
	while(t--) {
		solve();
	}
}//

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 19376kb

input:

7
1 1 2 4 2 2

output:

1 3 5 6 

result:

ok 4 number(s): "1 3 5 6"

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 20088kb

input:

10
1 1 2 2 1 1 1 2 4

output:

1 3 5 7 8 9 10 

result:

wrong answer 4th numbers differ - expected: '6', found: '7'