53702023-04-26 19:07:40kohumarkKülönböző katicákcpp17Time limit exceeded 0/100600ms130744 KiB
#include <iostream>
#include <vector>
#include <set>
using namespace std;
vector<vector<int>> con;
set<int> act; set<int> visited;

void actual(int i, int p[], int l){
	visited.insert(i);
	set<int> now;
	for(auto it=con[i].begin(); it!=con[i].end(); it++){
		if(p[*it]!=-1){
			int a=-l;
			while(a!=l){
				if(p[*it]+a>=0) now.insert(p[*it]+a);
				a+=2;
			}
		}
		if(!visited.count(*it)) actual(*it, p, l+1);
	}
	//for(auto it=now.begin(); it!=now.end(); it++) cout << *it << '\n';
	for(auto it=act.begin(); it!=act.end(); it++) if(!now.count(*it)){act.erase(*it); it=act.begin(); if(act.size()==0) break;}
}

int solve(int i, int p[]){
	//cout << '\n' << i << '\n';
	visited.insert(i);
	for(auto it=con[i].begin(); it!=con[i].end(); it++){
		if(p[*it]!=-1){act.insert(p[*it]+1); act.insert(abs(p[*it]-1));}
		actual(*it, p, 2);
	}
	if(act.size()==0) return -1;
	int x=*(act.begin()); act.clear(); visited.clear();
	return x;
}

int main(){
	bool ok=true;
	int n; cin >> n;
	int p[n]; //f[n];
	con.assign(n, vector<int>());
	for(int i=0; i<n; i++){
		int x; cin >> x;
		if(x!=0){con[i].push_back(x-1); con[x-1].push_back(i);}
	}
	for(int i=0; i<n; i++) cin >> p[i];
	for(int i=0; i<n; i++){
		if(p[i]==-1) p[i]=solve(i, p);
		if(p[i]==-1){ok=false; break;}
	}
	if(ok){
		cout << "IGEN\n";
		for(int i=0; i<n; i++){
			cout << p[i] << ' ';
		}
	}
	else cout << "NEM";
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1812 KiB
2Accepted3ms2060 KiB
3Time limit exceeded600ms13568 KiB
subtask20/10
4Wrong answer100ms18848 KiB
5Wrong answer112ms20700 KiB
6Wrong answer122ms22212 KiB
7Wrong answer140ms24372 KiB
subtask30/15
8Runtime error237ms130744 KiB
9Runtime error230ms130504 KiB
10Runtime error240ms130272 KiB
11Runtime error234ms130192 KiB
12Runtime error238ms129996 KiB
13Runtime error232ms129780 KiB
14Wrong answer128ms51532 KiB
subtask40/35
15Wrong answer4ms4356 KiB
16Accepted4ms4508 KiB
17Accepted3ms4344 KiB
18Wrong answer3ms4472 KiB
19Wrong answer4ms4784 KiB
20Wrong answer3ms4552 KiB
21Wrong answer4ms4704 KiB
22Wrong answer4ms4720 KiB
subtask50/40
23Wrong answer123ms23704 KiB
24Time limit exceeded558ms16512 KiB
25Accepted305ms27500 KiB
26Wrong answer134ms24400 KiB
27Wrong answer370ms26116 KiB
28Time limit exceeded560ms16456 KiB
29Wrong answer112ms25644 KiB
30Wrong answer133ms25192 KiB
31Time limit exceeded565ms18160 KiB
32Time limit exceeded550ms17680 KiB
33Time limit exceeded577ms15588 KiB