101832024-03-29 10:46:32111Különböző katicákcpp17Accepted 100/10065ms46060 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N;
	cin>>N;
	vector<vector<int>>g(N+1);
	vector<int>p(N+1);
	for(int i=1;i<=N;i++){
		cin>>p[i];
		if(i==1){
			continue;
		}
		g[p[i]].push_back(i);
		g[i].push_back(p[i]);
	}
	vector<int>v(N+1);
	for(int i=1;i<=N;i++){
		cin>>v[i];
	}
	auto vv=v;
	vector<pair<int,int>>w(N+1);
	auto dfs=[&](auto self,int i)->pair<int,int>{
		pair<int,int>a={v[i],v[i]};
		for(int j:g[i]){
			pair<int,int>b=j==p[i]?v[j]==-1?pair<int,int>{-1,-1}:pair<int,int>{abs(v[j]-1),v[j]+1}:self(self,j);
			if(b.first==-1&&b.second==-1){
				continue;
			}
			if(a.first==-1&&a.second==-1){
				a=b;
				continue;
			}
			if(a.first%2!=b.first%2||b.first>a.second||b.second<a.first){
				cout<<"NEM"<<'\n';
				exit(0);
			}
			a.first=max(a.first,b.first);
			a.second=min(a.second,b.second);
		}
		w[i]=a;
		if(a.first!=-1&&a.second!=-1){
			a.first=a.first==0?a.first+1:a.first-1;
			a.second++;
		}
		return a;
	};
	dfs(dfs,1);
	v[1]=w[1].first==-1?1:w[1].first;
	auto dfs2=[&](auto self,int i)->void{
		for(int j:g[i]){
			if(j==p[i]){
				continue;
			}
			if(w[j].first==-1){
				v[j]=v[i]+1;
			}
			else{
			if(w[j].first<=v[i]+1&&v[i]+1<=w[j].second){
				v[j]=v[i]+1;
			}
			else{
				v[j]=v[i]-1;
			}
			}
			self(self,j);
		}
	};
	dfs2(dfs2,1);
	for(int i=1;i<=N;i++){
		if(v[i]<0)exit(1);
		if(v[i]!=vv[i]&&vv[i]!=-1)exit(1);
		for(int j:g[i])if(abs(v[i]-v[j])!=1){
			cout<<i<<" "<<v[i]<<endl;
			cout<<j<<" "<<v[j]<<endl;
			exit(1);
		}
	}
	cout<<"IGEN"<<'\n';
	for(int i=1;i<=N;i++){
		cout<<v[i]<<' ';
	}
	cout<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1848 KiB
2Accepted3ms1984 KiB
3Accepted48ms19576 KiB
subtask210/10
4Accepted41ms17656 KiB
5Accepted46ms19456 KiB
6Accepted48ms20952 KiB
7Accepted52ms22724 KiB
subtask315/15
8Accepted46ms44860 KiB
9Accepted43ms44684 KiB
10Accepted65ms46060 KiB
11Accepted57ms45032 KiB
12Accepted54ms44100 KiB
13Accepted56ms45028 KiB
14Accepted54ms43644 KiB
subtask435/35
15Accepted3ms3956 KiB
16Accepted3ms4080 KiB
17Accepted3ms4088 KiB
18Accepted3ms4340 KiB
19Accepted3ms4276 KiB
20Accepted3ms4412 KiB
21Accepted3ms4232 KiB
22Accepted3ms4372 KiB
subtask540/40
23Accepted50ms22760 KiB
24Accepted37ms23572 KiB
25Accepted37ms24172 KiB
26Accepted50ms22644 KiB
27Accepted50ms22572 KiB
28Accepted57ms23588 KiB
29Accepted52ms23932 KiB
30Accepted50ms23396 KiB
31Accepted50ms23004 KiB
32Accepted54ms23340 KiB
33Accepted57ms24560 KiB