10030 | 2024-03-24 15:43:06 | 111 | Széfnyitás | cpp17 | Time limit exceeded 0/100 | 1.085s | 4528 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;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Time limit exceeded | 1.065s | 1792 KiB | ||||
2 | Time limit exceeded | 1.052s | 2016 KiB | ||||
subtask2 | 0/16 | ||||||
3 | Time limit exceeded | 1.052s | 1520 KiB | ||||
4 | Time limit exceeded | 1.065s | 2404 KiB | ||||
5 | Time limit exceeded | 1.049s | 2620 KiB | ||||
6 | Time limit exceeded | 1.08s | 2980 KiB | ||||
7 | Time limit exceeded | 1.052s | 2980 KiB | ||||
8 | Time limit exceeded | 1.06s | 3204 KiB | ||||
9 | Time limit exceeded | 1.052s | 3288 KiB | ||||
10 | Time limit exceeded | 1.065s | 3296 KiB | ||||
11 | Time limit exceeded | 1.085s | 3372 KiB | ||||
12 | Time limit exceeded | 1.085s | 3340 KiB | ||||
13 | Time limit exceeded | 1.049s | 3340 KiB | ||||
subtask3 | 0/24 | ||||||
14 | Time limit exceeded | 1.065s | 3560 KiB | ||||
15 | Time limit exceeded | 1.057s | 3572 KiB | ||||
16 | Time limit exceeded | 1.036s | 2852 KiB | ||||
17 | Time limit exceeded | 1.08s | 3548 KiB | ||||
18 | Time limit exceeded | 1.036s | 3652 KiB | ||||
19 | Time limit exceeded | 1.052s | 3552 KiB | ||||
20 | Time limit exceeded | 1.072s | 3648 KiB | ||||
21 | Time limit exceeded | 1.074s | 3880 KiB | ||||
subtask4 | 0/23 | ||||||
22 | Time limit exceeded | 1.065s | 3764 KiB | ||||
23 | Time limit exceeded | 1.065s | 3992 KiB | ||||
24 | Time limit exceeded | 1.06s | 4092 KiB | ||||
25 | Time limit exceeded | 1.065s | 3408 KiB | ||||
26 | Time limit exceeded | 1.057s | 4328 KiB | ||||
27 | Time limit exceeded | 1.074s | 4300 KiB | ||||
28 | Time limit exceeded | 1.06s | 4300 KiB | ||||
29 | Time limit exceeded | 1.057s | 4404 KiB | ||||
subtask5 | 0/37 | ||||||
30 | Time limit exceeded | 1.052s | 1520 KiB | ||||
31 | Time limit exceeded | 1.065s | 2404 KiB | ||||
32 | Time limit exceeded | 1.049s | 2620 KiB | ||||
33 | Time limit exceeded | 1.08s | 2980 KiB | ||||
34 | Time limit exceeded | 1.052s | 2980 KiB | ||||
35 | Time limit exceeded | 1.06s | 3204 KiB | ||||
36 | Time limit exceeded | 1.052s | 3288 KiB | ||||
37 | Time limit exceeded | 1.065s | 3296 KiB | ||||
38 | Time limit exceeded | 1.085s | 3372 KiB | ||||
39 | Time limit exceeded | 1.085s | 3340 KiB | ||||
40 | Time limit exceeded | 1.049s | 3340 KiB | ||||
41 | Time limit exceeded | 1.065s | 3560 KiB | ||||
42 | Time limit exceeded | 1.057s | 3572 KiB | ||||
43 | Time limit exceeded | 1.036s | 2852 KiB | ||||
44 | Time limit exceeded | 1.08s | 3548 KiB | ||||
45 | Time limit exceeded | 1.036s | 3652 KiB | ||||
46 | Time limit exceeded | 1.052s | 3552 KiB | ||||
47 | Time limit exceeded | 1.072s | 3648 KiB | ||||
48 | Time limit exceeded | 1.074s | 3880 KiB | ||||
49 | Time limit exceeded | 1.065s | 3764 KiB | ||||
50 | Time limit exceeded | 1.065s | 3992 KiB | ||||
51 | Time limit exceeded | 1.06s | 4092 KiB | ||||
52 | Time limit exceeded | 1.065s | 3408 KiB | ||||
53 | Time limit exceeded | 1.057s | 4328 KiB | ||||
54 | Time limit exceeded | 1.074s | 4300 KiB | ||||
55 | Time limit exceeded | 1.06s | 4300 KiB | ||||
56 | Time limit exceeded | 1.057s | 4404 KiB | ||||
57 | Time limit exceeded | 1.082s | 4420 KiB | ||||
58 | Time limit exceeded | 1.077s | 4412 KiB | ||||
59 | Time limit exceeded | 1.065s | 3592 KiB | ||||
60 | Time limit exceeded | 1.072s | 4292 KiB | ||||
61 | Time limit exceeded | 1.049s | 3624 KiB | ||||
62 | Time limit exceeded | 1.052s | 4528 KiB | ||||
63 | Time limit exceeded | 1.077s | 4512 KiB | ||||
64 | Time limit exceeded | 1.06s | 4380 KiB | ||||
65 | Time limit exceeded | 1.06s | 4488 KiB | ||||
66 | Time limit exceeded | 1.069s | 3692 KiB | ||||
67 | Time limit exceeded | 1.065s | 4300 KiB | ||||
68 | Time limit exceeded | 1.065s | 4296 KiB |