100272024-03-24 15:26:25111Széfnyitáscpp17Wrong answer 0/1008ms7696 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N;
#ifdef DEBUG
start:
	N=2+rand()%99;
	// N=4;
#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);
		for(int i=0;i<N;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),c(N*2);
			int x=z;
			for(int i=0;i<N;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);
				int x=z;
				int y=0;
				for(int i=0;i<N;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]];
				}
			}
			cout<<t.size()<<'\n';
			for(int i=0;i<t.size();i++){
				cout<<r[i]<<' '<<t[i][0]+1<<' '<<t[i][1]+1<<'\n';
			}
			cout<<1<<'\n';
#ifdef DEBUG
			for(int z=1;z<=N;z++){
				int x=z,y=0;
				int k=0;
				for(int i=0;i<10000000;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){
#ifdef DEBUG
					cout<<z<<' '<<k<<'\n';
#endif
					return 1;
				}
			}
			goto start;
#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
1Wrong answer3ms1896 KiB
2Wrong answer3ms2088 KiB
subtask20/16
3Wrong answer3ms2320 KiB
4Wrong answer3ms2508 KiB
5Wrong answer2ms2592 KiB
6Wrong answer3ms2720 KiB
7Wrong answer2ms2832 KiB
8Wrong answer2ms2904 KiB
9Wrong answer3ms3032 KiB
10Wrong answer3ms3404 KiB
11Wrong answer3ms3520 KiB
12Wrong answer3ms3720 KiB
13Wrong answer3ms3904 KiB
subtask30/24
14Wrong answer3ms4016 KiB
15Wrong answer3ms4296 KiB
16Wrong answer3ms4252 KiB
17Wrong answer3ms4500 KiB
18Wrong answer3ms4788 KiB
19Wrong answer3ms4868 KiB
20Wrong answer3ms5164 KiB
21Wrong answer3ms5104 KiB
subtask40/23
22Wrong answer3ms4984 KiB
23Wrong answer3ms5060 KiB
24Wrong answer3ms5148 KiB
25Wrong answer3ms5008 KiB
26Wrong answer3ms4988 KiB
27Wrong answer3ms5084 KiB
28Wrong answer3ms5220 KiB
29Wrong answer3ms5144 KiB
subtask50/37
30Wrong answer3ms2320 KiB
31Wrong answer3ms2508 KiB
32Wrong answer2ms2592 KiB
33Wrong answer3ms2720 KiB
34Wrong answer2ms2832 KiB
35Wrong answer2ms2904 KiB
36Wrong answer3ms3032 KiB
37Wrong answer3ms3404 KiB
38Wrong answer3ms3520 KiB
39Wrong answer3ms3720 KiB
40Wrong answer3ms3904 KiB
41Wrong answer3ms4016 KiB
42Wrong answer3ms4296 KiB
43Wrong answer3ms4252 KiB
44Wrong answer3ms4500 KiB
45Wrong answer3ms4788 KiB
46Wrong answer3ms4868 KiB
47Wrong answer3ms5164 KiB
48Wrong answer3ms5104 KiB
49Wrong answer3ms4984 KiB
50Wrong answer3ms5060 KiB
51Wrong answer3ms5148 KiB
52Wrong answer3ms5008 KiB
53Wrong answer3ms4988 KiB
54Wrong answer3ms5084 KiB
55Wrong answer3ms5220 KiB
56Wrong answer3ms5144 KiB
57Wrong answer4ms5616 KiB
58Wrong answer7ms6652 KiB
59Wrong answer6ms6448 KiB
60Wrong answer7ms6644 KiB
61Wrong answer8ms7256 KiB
62Wrong answer8ms7504 KiB
63Wrong answer8ms6936 KiB
64Wrong answer8ms7592 KiB
65Wrong answer7ms6732 KiB
66Wrong answer8ms7212 KiB
67Wrong answer8ms7696 KiB
68Wrong answer8ms7048 KiB