我的代码过不了样例一却可以AC
查看原帖
我的代码过不了样例一却可以AC
544773
JoeZ009楼主2024/6/25 18:08

RT

#include<bits/stdc++.h>
#define int long long
using namespace std;
int m,n;
const int N=1e5+10;
int f[N],d[N],p[N];
void tree2prufer() {
	for(int i=1;i<n;i++) {
		cin>>f[i];
		d[f[i]]++;
	}
	for(int i=1,j=1;i<=n-2;j++) {
		while(d[j])j++;
		p[i++]=f[j];
		while(i<=n-2&&--d[p[i-1]]==0&&p[i-1]<j)p[i++]=f[p[i-1]];
	}
	int ans=0;
	for(int i=1;i<=n-2;i++)ans^=i*p[i];
	cout<<ans<<endl;
	return;
}
void prufer2tree() {
	for(int i=1;i<=n-2;i++) {
		cin>>p[i];
		d[p[i]]++;
	}
	p[n-1]=n;
	for(int i=1,j=1;i<n;i++,j++) {
		while(d[j])j++;
		f[j]=p[i];
		while(i<n-1&&--d[p[i]]==0&&p[i]<j)f[p[i]]=p[i+1],i++;
	}
	int ans=0;
    for(int i=1;i<n;i++)ans^=i*f[i];
	cout<<ans<<endl;
    return;
}
signed main() {
	cin>>n>>m;
	if(m==1)tree2prufer();
	else prufer2tree();
	return 0;
}

who know why?

2024/6/25 18:08
加载中...