QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#620192 | #9230. Routing K-Codes | free_windy | WA | 26ms | 19452kb | C++20 | 2.1kb | 2024-10-07 16:57:17 | 2024-10-07 16:57:22 |
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;
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=0x3f3f3f3f;
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;
}
}
cout<<ans<<"\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 11900kb
input:
4 3 1 2 1 3 1 4
output:
6
result:
ok single line: '6'
Test #2:
score: 0
Accepted
time: 0ms
memory: 11820kb
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: 1ms
memory: 11828kb
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: -100
Wrong Answer
time: 26ms
memory: 19452kb
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:
-210905534214928376
result:
wrong answer 1st lines differ - expected: 'NIE', found: '-210905534214928376'