QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#474245#8134. LCA CountingtoanWA 2ms12664kbC++171.6kb2024-07-12 16:54:312024-07-12 16:54:31

Judging History

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

  • [2024-07-12 16:54:31]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:12664kb
  • [2024-07-12 16:54:31]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;
#define int long long
int n, m, u, v, a[100005], subtree[100005], ans[100005];
multiset<int> mp[100005];
vector<int> adj[100005];
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();
	}
}//

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 11684kb

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: 2ms
memory: 12664kb

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'