10031 | 2024-03-24 15:44:07 | 111 | Széfnyitás | cpp17 | Accepted 100/100 | 85ms | 7152 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()%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;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 4ms | 1892 KiB | ||||
2 | Accepted | 4ms | 2376 KiB | ||||
subtask2 | 16/16 | ||||||
3 | Accepted | 4ms | 2544 KiB | ||||
4 | Accepted | 4ms | 2452 KiB | ||||
5 | Accepted | 4ms | 2564 KiB | ||||
6 | Accepted | 4ms | 2604 KiB | ||||
7 | Accepted | 4ms | 2740 KiB | ||||
8 | Accepted | 4ms | 2888 KiB | ||||
9 | Accepted | 4ms | 3100 KiB | ||||
10 | Accepted | 4ms | 3268 KiB | ||||
11 | Accepted | 4ms | 3232 KiB | ||||
12 | Accepted | 4ms | 3480 KiB | ||||
13 | Accepted | 4ms | 3356 KiB | ||||
subtask3 | 24/24 | ||||||
14 | Accepted | 17ms | 3496 KiB | ||||
15 | Accepted | 17ms | 3644 KiB | ||||
16 | Accepted | 17ms | 3604 KiB | ||||
17 | Accepted | 17ms | 4140 KiB | ||||
18 | Accepted | 17ms | 4080 KiB | ||||
19 | Accepted | 17ms | 3844 KiB | ||||
20 | Accepted | 17ms | 3884 KiB | ||||
21 | Accepted | 17ms | 3900 KiB | ||||
subtask4 | 23/23 | ||||||
22 | Accepted | 17ms | 4036 KiB | ||||
23 | Accepted | 17ms | 4052 KiB | ||||
24 | Accepted | 17ms | 4148 KiB | ||||
25 | Accepted | 17ms | 4148 KiB | ||||
26 | Accepted | 17ms | 4128 KiB | ||||
27 | Accepted | 17ms | 4304 KiB | ||||
28 | Accepted | 17ms | 4220 KiB | ||||
29 | Accepted | 17ms | 4164 KiB | ||||
subtask5 | 37/37 | ||||||
30 | Accepted | 4ms | 2544 KiB | ||||
31 | Accepted | 4ms | 2452 KiB | ||||
32 | Accepted | 4ms | 2564 KiB | ||||
33 | Accepted | 4ms | 2604 KiB | ||||
34 | Accepted | 4ms | 2740 KiB | ||||
35 | Accepted | 4ms | 2888 KiB | ||||
36 | Accepted | 4ms | 3100 KiB | ||||
37 | Accepted | 4ms | 3268 KiB | ||||
38 | Accepted | 4ms | 3232 KiB | ||||
39 | Accepted | 4ms | 3480 KiB | ||||
40 | Accepted | 4ms | 3356 KiB | ||||
41 | Accepted | 17ms | 3496 KiB | ||||
42 | Accepted | 17ms | 3644 KiB | ||||
43 | Accepted | 17ms | 3604 KiB | ||||
44 | Accepted | 17ms | 4140 KiB | ||||
45 | Accepted | 17ms | 4080 KiB | ||||
46 | Accepted | 17ms | 3844 KiB | ||||
47 | Accepted | 17ms | 3884 KiB | ||||
48 | Accepted | 17ms | 3900 KiB | ||||
49 | Accepted | 17ms | 4036 KiB | ||||
50 | Accepted | 17ms | 4052 KiB | ||||
51 | Accepted | 17ms | 4148 KiB | ||||
52 | Accepted | 17ms | 4148 KiB | ||||
53 | Accepted | 17ms | 4128 KiB | ||||
54 | Accepted | 17ms | 4304 KiB | ||||
55 | Accepted | 17ms | 4220 KiB | ||||
56 | Accepted | 17ms | 4164 KiB | ||||
57 | Accepted | 68ms | 4692 KiB | ||||
58 | Accepted | 82ms | 5664 KiB | ||||
59 | Accepted | 82ms | 5788 KiB | ||||
60 | Accepted | 82ms | 5924 KiB | ||||
61 | Accepted | 85ms | 6588 KiB | ||||
62 | Accepted | 83ms | 6800 KiB | ||||
63 | Accepted | 82ms | 6120 KiB | ||||
64 | Accepted | 83ms | 6808 KiB | ||||
65 | Accepted | 82ms | 6032 KiB | ||||
66 | Accepted | 83ms | 6524 KiB | ||||
67 | Accepted | 85ms | 7152 KiB | ||||
68 | Accepted | 83ms | 6508 KiB |