101812024-03-29 10:40:53111Különböző katicákcpp17Wrong answer 0/10061ms44620 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];
	}
	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;
	auto dfs2=[&](auto self,int i)->void{
		for(int j:g[i]){
			if(j==p[i]){
				continue;
			}
			if(v[j]==-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);
	}
	cout<<"IGEN"<<'\n';
	for(int i=1;i<=N;i++){
		cout<<v[i]<<' ';
	}
	cout<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1956 KiB
2Accepted3ms2092 KiB
3Wrong answer46ms18436 KiB
subtask20/10
4Runtime error32ms16540 KiB
5Runtime error37ms18152 KiB
6Runtime error39ms19696 KiB
7Runtime error43ms21584 KiB
subtask30/15
8Accepted46ms43712 KiB
9Accepted48ms43304 KiB
10Wrong answer61ms44620 KiB
11Wrong answer59ms43644 KiB
12Wrong answer54ms42828 KiB
13Wrong answer61ms43912 KiB
14Runtime error45ms42316 KiB
subtask40/35
15Runtime error3ms4060 KiB
16Accepted3ms4064 KiB
17Accepted3ms4060 KiB
18Wrong answer3ms3996 KiB
19Wrong answer3ms4268 KiB
20Wrong answer3ms4128 KiB
21Wrong answer3ms4248 KiB
22Wrong answer3ms4276 KiB
subtask50/40
23Runtime error39ms20872 KiB
24Accepted37ms21580 KiB
25Accepted37ms22368 KiB
26Wrong answer48ms20872 KiB
27Wrong answer48ms20872 KiB
28Wrong answer54ms21892 KiB
29Wrong answer50ms22096 KiB
30Wrong answer50ms21428 KiB
31Wrong answer50ms21208 KiB
32Wrong answer52ms21272 KiB
33Wrong answer54ms22432 KiB