QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#620218 | #9230. Routing K-Codes | free_windy | WA | 44ms | 19732kb | C++20 | 2.2kb | 2024-10-07 17:02:12 | 2024-10-07 17:02:13 |
Judging History
answer
//utl
#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read(){
int s=0,z=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-')z=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
s=(s<<3)+(s<<1)+(c^48);
c=getchar();
}
return s*z;
}
const int N = 2e5+5;
int fr[N],to[N*2],nt[N*2],cnt;
int n,m;
int r[N];
int son[N][2];
bool vis[N];
bool sf;
int ts[N],f[N];
long long ans;
bool vis2[N];
void ct(int x,int y){
nt[++cnt]=fr[x];
fr[x]=cnt;
to[cnt]=y;
return ;
}
bool isson(int x,int y){
return y==son[x][0]||y==son[x][1];
}//fa ->son
int wz(int x,int y){
return y==son[x][1];
}//fa ->son
void dfs1(int x,int fa){
vis[x]=1;
for(int i=fr[x];i;i=nt[i]){
if(to[i]==fa) continue;
if(vis[to[i]]==0) dfs1(to[i],x);
else sf=1;
}
return ;
}
void dfs2(int x,int fa){
ts[x]=1;
int tot=0;
f[x]=fa;
for(int i=fr[x];i;i=nt[i]){
if(to[i]==fa) continue;
dfs2(to[i],x);
son[x][tot++]=to[i];
}
ts[x]=ts[son[x][1]]*2+ts[son[x][0]]*2+min(ts[son[x][1]],ts[son[x][0]])+1;
return ;
}
void cg(int x){
vis2[x]=1;
if(r[x]==1){
int y=to[fr[x]];
ans=min(ans,ts[y]);
ts[x]=1;
for(int i=fr[x];i;i=nt[i]){
if(vis2[to[i]]) continue;
cg(to[i]);
}
return ;
}
if(r[x]==2){
ts[x]=(ts[son[x][0]]+ts[f[x]])*2+min(ts[son[x][0]],ts[f[x]])+1;
ans=min(ans,ts[x]);
for(int i=fr[x];i;i=nt[i]){
if(vis2[to[i]]) continue;
ts[x]=ts[f[x]]*2+1;
cg(to[i]);
}
ts[x]=ts[son[x][0]]*2+1;
return ;
}
for(int i=fr[x];i;i=nt[i]){
if(vis2[to[i]]) continue;
int sw=wz(x,to[i]);
ts[x]=ts[son[x][sw^1]]*2+ts[f[x]]*2+min(ts[f[x]],ts[son[x][sw^1]])+1;
cg(to[i]);
}
ts[x]=ts[son[x][0]]*2+ts[son[x][1]]*2+min(ts[son[x][0]],ts[son[x][1]])+1;
return ;
}
signed main(){
n=read(),m=read();
ans=1e18;
for(int x,y,i=1;i<=m;i++){
x=read(),y=read();
r[x]++;
r[y]++;
if(r[x]>3||r[y]>3){
cout<<"NIE";
return 0;
}
ct(x,y);
ct(y,x);
}
dfs1(1,0);
if(sf==1){
cout<<"NIE";
return 0;
}
for(int i=1;i<=n;i++){
if(r[i]<3){
dfs2(i,0);
cg(i);
break;
}
}
if(ans<0) cout<<"NIE";
else cout<<ans<<"\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 11796kb
input:
4 3 1 2 1 3 1 4
output:
6
result:
ok single line: '6'
Test #2:
score: 0
Accepted
time: 0ms
memory: 11916kb
input:
4 6 1 2 2 3 3 4 4 1 1 3 2 4
output:
NIE
result:
ok single line: 'NIE'
Test #3:
score: 0
Accepted
time: 0ms
memory: 11780kb
input:
10 10 9 3 5 10 1 4 10 8 8 3 7 3 9 6 8 6 7 2 4 5
output:
NIE
result:
ok single line: 'NIE'
Test #4:
score: 0
Accepted
time: 44ms
memory: 19548kb
input:
200000 199999 172774 188052 195063 155577 38023 148303 30605 155047 170238 19344 46835 58255 154268 109062 197059 116041 136424 12058 42062 149807 109545 115380 132322 51106 16706 162612 62234 31319 195735 80435 173898 84051 19876 102910 186924 9136 123094 117097 145054 173049 137364 152731 82662 18...
output:
NIE
result:
ok single line: 'NIE'
Test #5:
score: -100
Wrong Answer
time: 41ms
memory: 19732kb
input:
200000 199999 168192 30733 164413 46673 175210 2329 69389 67102 33991 152553 91061 55265 166724 117726 90148 157176 34831 12213 60756 105488 121891 155192 82202 155666 102083 156661 38968 75200 190004 107321 72436 92732 64314 65004 39210 91106 70455 173568 179742 29844 126232 19552 133509 110602 131...
output:
298865693448477
result:
wrong answer 1st lines differ - expected: '20980030044349', found: '298865693448477'