QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#209148#6638. Treelectionc20230201WA 150ms39132kbC++141.2kb2023-10-10 10:48:552023-10-10 10:48:56

Judging History

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

  • [2023-10-10 10:48:56]
  • 评测
  • 测评结果:WA
  • 用时:150ms
  • 内存:39132kb
  • [2023-10-10 10:48:55]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

const int maxn=1e6+5;

int siz[maxn], f[maxn], ans[maxn], mn[maxn];
vector<int> g[maxn];

void dfs(int x) {
	siz[x]=1;
	for(int y:g[x]) dfs(y), siz[x]+=siz[y];
	return ;
}

int check(int x,int v) {
	f[x]=0;
	for(int y:g[x]) {
		f[x]=f[x]+f[y]+1;
		check(y,v);
	}
	f[x]=max(f[x]-v,0);
	if(x==1) return !f[x];
	return 0;
}

void dfs2(int x) {
	for(int y:g[x]) {
		if(x!=1) mn[y]=min(mn[y],mn[x]);
		dfs2(y);
	}
	return ;
}

void solve() {
	int n; cin>>n;
	for(int i=1;i<=n;i++) g[i].clear();
	for(int i=2;i<=n;i++) {
		int x; cin>>x;
		g[x].push_back(i);
	}
	dfs(1);
	int l=1, r=n, w=n+1;
	while(l<=r) {
		int mid=l+r>>1;
		if(check(1,mid-1)) r=mid-1, w=mid;
		else l=mid+1;
	}
	vector<int> vec;
	check(1,w-2);
	for(int i=1;i<=n;i++) mn[i]=f[i];
	dfs2(1);
	for(int i=1;i<=n;i++) {
		if(siz[i]<w) ans[i]=0;
		else if(siz[i]>w) ans[i]=1;
		else {
			if(mn[i]>0&&f[1]==1) ans[i]=1;
			else ans[i]=0;
		}
		cout<<ans[i];
	}
	cout<<'\n';
	return ;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int T; cin>>T;
	while(T--) solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 33292kb

input:

2
4
1 2 3
5
1 1 2 2

output:

1100
10000

result:

ok 2 lines

Test #2:

score: -100
Wrong Answer
time: 150ms
memory: 39132kb

input:

10
100000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 10...

output:

111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...

result:

wrong answer 1st lines differ - expected: '111111111111111111111111111111...0000000000000000000000000000000', found: '111111111111111111111111111111...0000000000000000000000000000000'