10027 | 2024-03-24 15:26:25 | 111 | Széfnyitás | cpp17 | Wrong answer 0/100 | 8ms | 7696 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;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Wrong answer | 3ms | 1896 KiB | ||||
2 | Wrong answer | 3ms | 2088 KiB | ||||
subtask2 | 0/16 | ||||||
3 | Wrong answer | 3ms | 2320 KiB | ||||
4 | Wrong answer | 3ms | 2508 KiB | ||||
5 | Wrong answer | 2ms | 2592 KiB | ||||
6 | Wrong answer | 3ms | 2720 KiB | ||||
7 | Wrong answer | 2ms | 2832 KiB | ||||
8 | Wrong answer | 2ms | 2904 KiB | ||||
9 | Wrong answer | 3ms | 3032 KiB | ||||
10 | Wrong answer | 3ms | 3404 KiB | ||||
11 | Wrong answer | 3ms | 3520 KiB | ||||
12 | Wrong answer | 3ms | 3720 KiB | ||||
13 | Wrong answer | 3ms | 3904 KiB | ||||
subtask3 | 0/24 | ||||||
14 | Wrong answer | 3ms | 4016 KiB | ||||
15 | Wrong answer | 3ms | 4296 KiB | ||||
16 | Wrong answer | 3ms | 4252 KiB | ||||
17 | Wrong answer | 3ms | 4500 KiB | ||||
18 | Wrong answer | 3ms | 4788 KiB | ||||
19 | Wrong answer | 3ms | 4868 KiB | ||||
20 | Wrong answer | 3ms | 5164 KiB | ||||
21 | Wrong answer | 3ms | 5104 KiB | ||||
subtask4 | 0/23 | ||||||
22 | Wrong answer | 3ms | 4984 KiB | ||||
23 | Wrong answer | 3ms | 5060 KiB | ||||
24 | Wrong answer | 3ms | 5148 KiB | ||||
25 | Wrong answer | 3ms | 5008 KiB | ||||
26 | Wrong answer | 3ms | 4988 KiB | ||||
27 | Wrong answer | 3ms | 5084 KiB | ||||
28 | Wrong answer | 3ms | 5220 KiB | ||||
29 | Wrong answer | 3ms | 5144 KiB | ||||
subtask5 | 0/37 | ||||||
30 | Wrong answer | 3ms | 2320 KiB | ||||
31 | Wrong answer | 3ms | 2508 KiB | ||||
32 | Wrong answer | 2ms | 2592 KiB | ||||
33 | Wrong answer | 3ms | 2720 KiB | ||||
34 | Wrong answer | 2ms | 2832 KiB | ||||
35 | Wrong answer | 2ms | 2904 KiB | ||||
36 | Wrong answer | 3ms | 3032 KiB | ||||
37 | Wrong answer | 3ms | 3404 KiB | ||||
38 | Wrong answer | 3ms | 3520 KiB | ||||
39 | Wrong answer | 3ms | 3720 KiB | ||||
40 | Wrong answer | 3ms | 3904 KiB | ||||
41 | Wrong answer | 3ms | 4016 KiB | ||||
42 | Wrong answer | 3ms | 4296 KiB | ||||
43 | Wrong answer | 3ms | 4252 KiB | ||||
44 | Wrong answer | 3ms | 4500 KiB | ||||
45 | Wrong answer | 3ms | 4788 KiB | ||||
46 | Wrong answer | 3ms | 4868 KiB | ||||
47 | Wrong answer | 3ms | 5164 KiB | ||||
48 | Wrong answer | 3ms | 5104 KiB | ||||
49 | Wrong answer | 3ms | 4984 KiB | ||||
50 | Wrong answer | 3ms | 5060 KiB | ||||
51 | Wrong answer | 3ms | 5148 KiB | ||||
52 | Wrong answer | 3ms | 5008 KiB | ||||
53 | Wrong answer | 3ms | 4988 KiB | ||||
54 | Wrong answer | 3ms | 5084 KiB | ||||
55 | Wrong answer | 3ms | 5220 KiB | ||||
56 | Wrong answer | 3ms | 5144 KiB | ||||
57 | Wrong answer | 4ms | 5616 KiB | ||||
58 | Wrong answer | 7ms | 6652 KiB | ||||
59 | Wrong answer | 6ms | 6448 KiB | ||||
60 | Wrong answer | 7ms | 6644 KiB | ||||
61 | Wrong answer | 8ms | 7256 KiB | ||||
62 | Wrong answer | 8ms | 7504 KiB | ||||
63 | Wrong answer | 8ms | 6936 KiB | ||||
64 | Wrong answer | 8ms | 7592 KiB | ||||
65 | Wrong answer | 7ms | 6732 KiB | ||||
66 | Wrong answer | 8ms | 7212 KiB | ||||
67 | Wrong answer | 8ms | 7696 KiB | ||||
68 | Wrong answer | 8ms | 7048 KiB |