100302024-03-24 15:43:06111Széfnyitáscpp17Time limit exceeded 0/1001.085s4528 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long

#define DEBUG

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N;
#ifdef DEBUG
start:
	N=2+rand()%149;
	N=2;
#else
	cin>>N;
#endif
	int a[N+1],g[N+1][2];
	for(int i=1;i<=N;i++){
#ifdef DEBUG
		a[i]=rand()&1,g[i][0]=1+rand()%N,g[i][1]=1+rand()%N;
#else
		cin>>a[i]>>g[i][0]>>g[i][1];
#endif
	}
	int K;
#ifdef DEBUG
	K=N;
#else
	cin>>K;
#endif
	for(int _=0;_<1000;_++){
		vector<int>b(N-1);
		for(int i=0;i<N-1;i++){
			b[i]=rand()&1;
		}
		map<vector<int>,vector<int>>m;
		int ok=1;
		for(int z=1;z<=N;z++){
			vector<int>v(N-1),c(N*2);
			int x=z;
			for(int i=0;i<N-1;i++){
				v[i]=a[x];
				x=g[x][b[i]];
			}
			for(int i=0;i<N*2;i++){
				c[i]=a[x];
				x=g[x][a[x]];
			}
			if(m.count(v)&&m[v]!=c&&count(c.begin(),c.end(),0)&&count(c.begin(),c.end(),1)){
				// for(int i:v)cout<<i<<' ';cout<<endl;
				// for(int i:c)cout<<i<<' ';cout<<endl;
				// for(int i:m[v])cout<<i<<' ';cout<<endl;
				ok=0;
				break;
			}
			m[v]=c;
		}
		if(ok){
			vector<array<int,2>>t(1);
			vector<int>r(1);
			for(int z=1;z<=N;z++){
				vector<int>v(N-1);
				int x=z;
				int y=0;
				for(int i=0;i<N-1;i++){
					v[i]=a[x];
					if(t[y][a[x]]==0){
						t[y][a[x]]=t.size();
						t.emplace_back();
						r.push_back(-1);
					}
					r[y]=b[i];
					y=t[y][a[x]];
					x=g[x][b[i]];
				}
				if(r[y]!=-1){
					continue;
				}
				int xx=x;
				vector<int>c(N*2);
				for(int i=0;i<N*2;i++){
					c[i]=a[x];
					x=g[x][a[x]];
				}
				if(!count(c.begin(),c.end(),0)){
					r[y]=1;
					t[y][1]=y;
					continue;
				}
				if(!count(c.begin(),c.end(),1)){
					r[y]=0;
					t[y][0]=y;
					continue;
				}
				x=xx;
				vector<int>w(N+1);
				for(int i=0;i<N*2;i++){
					r[y]=a[x];
					if(w[g[x][a[x]]]){
						t[y][a[x]]=w[g[x][a[x]]];
						break;
					}
					w[x]=y;
					t[y][a[x]]=t.size();
					t.emplace_back();
					r.push_back(-1);
					y=t[y][a[x]];
					x=g[x][a[x]];
				}
				if(r[y]==-1){
					return 1;
				}
			}
			if(r.size()!=t.size()||count(r.begin(),r.end(),0)+count(r.begin(),r.end(),1)!=r.size()){
				return 1;
			}
			if(t.size()>50000){
				return 1;
			}
			for(int z=1;z<=N;z++){
				int x=z,y=0;
				int k=0;
				for(int i=0;i<100000;i++){
					k+=r[y]!=a[x];
					int xx=g[x][r[y]];
					int yy=t[y][a[x]];
					x=xx;
					y=yy;
				}
				if(k>=K){ /* HAS TO BE LESS!!! */
					cout<<z<<' '<<k<<'\n';
					return 1;
				}
			}
#ifdef DEBUG
			cout<<"OK"<<endl;
			goto start;
#else
			cout<<t.size()<<' '<<1<<'\n';
			for(int i=0;i<t.size();i++){
				cout<<r[i]<<' '<<t[i][0]+1<<' '<<t[i][1]+1<<'\n';
			}
#endif
			return 0;
		}
	}
#ifdef DEBUG
	cout<<N<<endl;
	for(int i=1;i<=N;i++){
		cout<<a[i]<<' '<<g[i][0]<<' '<<g[i][1]<<endl;
	}
#endif
	return 1;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Time limit exceeded1.065s1792 KiB
2Time limit exceeded1.052s2016 KiB
subtask20/16
3Time limit exceeded1.052s1520 KiB
4Time limit exceeded1.065s2404 KiB
5Time limit exceeded1.049s2620 KiB
6Time limit exceeded1.08s2980 KiB
7Time limit exceeded1.052s2980 KiB
8Time limit exceeded1.06s3204 KiB
9Time limit exceeded1.052s3288 KiB
10Time limit exceeded1.065s3296 KiB
11Time limit exceeded1.085s3372 KiB
12Time limit exceeded1.085s3340 KiB
13Time limit exceeded1.049s3340 KiB
subtask30/24
14Time limit exceeded1.065s3560 KiB
15Time limit exceeded1.057s3572 KiB
16Time limit exceeded1.036s2852 KiB
17Time limit exceeded1.08s3548 KiB
18Time limit exceeded1.036s3652 KiB
19Time limit exceeded1.052s3552 KiB
20Time limit exceeded1.072s3648 KiB
21Time limit exceeded1.074s3880 KiB
subtask40/23
22Time limit exceeded1.065s3764 KiB
23Time limit exceeded1.065s3992 KiB
24Time limit exceeded1.06s4092 KiB
25Time limit exceeded1.065s3408 KiB
26Time limit exceeded1.057s4328 KiB
27Time limit exceeded1.074s4300 KiB
28Time limit exceeded1.06s4300 KiB
29Time limit exceeded1.057s4404 KiB
subtask50/37
30Time limit exceeded1.052s1520 KiB
31Time limit exceeded1.065s2404 KiB
32Time limit exceeded1.049s2620 KiB
33Time limit exceeded1.08s2980 KiB
34Time limit exceeded1.052s2980 KiB
35Time limit exceeded1.06s3204 KiB
36Time limit exceeded1.052s3288 KiB
37Time limit exceeded1.065s3296 KiB
38Time limit exceeded1.085s3372 KiB
39Time limit exceeded1.085s3340 KiB
40Time limit exceeded1.049s3340 KiB
41Time limit exceeded1.065s3560 KiB
42Time limit exceeded1.057s3572 KiB
43Time limit exceeded1.036s2852 KiB
44Time limit exceeded1.08s3548 KiB
45Time limit exceeded1.036s3652 KiB
46Time limit exceeded1.052s3552 KiB
47Time limit exceeded1.072s3648 KiB
48Time limit exceeded1.074s3880 KiB
49Time limit exceeded1.065s3764 KiB
50Time limit exceeded1.065s3992 KiB
51Time limit exceeded1.06s4092 KiB
52Time limit exceeded1.065s3408 KiB
53Time limit exceeded1.057s4328 KiB
54Time limit exceeded1.074s4300 KiB
55Time limit exceeded1.06s4300 KiB
56Time limit exceeded1.057s4404 KiB
57Time limit exceeded1.082s4420 KiB
58Time limit exceeded1.077s4412 KiB
59Time limit exceeded1.065s3592 KiB
60Time limit exceeded1.072s4292 KiB
61Time limit exceeded1.049s3624 KiB
62Time limit exceeded1.052s4528 KiB
63Time limit exceeded1.077s4512 KiB
64Time limit exceeded1.06s4380 KiB
65Time limit exceeded1.06s4488 KiB
66Time limit exceeded1.069s3692 KiB
67Time limit exceeded1.065s4300 KiB
68Time limit exceeded1.065s4296 KiB