QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#454733 | #8686. Zoo Management | NKheyuxiang | WA | 4ms | 29224kb | C++14 | 2.7kb | 2024-06-25 12:04:32 | 2024-06-25 12:04:33 |
Judging History
answer
#include<bits/stdc++.h>
#define N 1000005
using namespace std;
bool flag=1;
int n,m,h[N],to[N],nxt[N],cnt;
void jb(int u,int v){
++cnt;
to[cnt]=v;
nxt[cnt]=h[u];
h[u]=cnt;
}
int low[N],dfn[N],id[N],cc,c1,c2,hd,st[N];
void tarjan1(int u,int fa){
low[u]=dfn[u]=++cc;
st[++hd]=u;
for(int i=h[u];i!=0;i=nxt[i]){
int v=to[i];
if(v==fa) continue;
if(!dfn[v]){
tarjan1(v,u);
low[u]=min(low[u],low[v]);
}
else low[u]=min(low[u],dfn[v]);
}
if(low[u]>low[fa]){
c1++;
while(1){
int x=st[hd--];
id[x]=c1;
if(x==u) break;
}
}
}
int bel[N],sz[N],es[N];
vector<int > vec[N];
void tarjan2(int u,int fa){
low[u]=dfn[u]=++cc;
st[++hd]=u;
for(int i=h[u];i!=0;i=nxt[i]){
int v=to[i];
if(v==fa||id[v]!=id[u]) continue;
if(!dfn[v]){
tarjan2(v,u);
if(low[v]>=dfn[u]){
bel[u]=u;sz[++c2]=1;
while(1){
int x=st[hd--];
bel[x]=u;sz[c2]++;
for(int j=h[x];j!=0;j=nxt[j])
if(bel[to[j]]==u) es[c2]++;
if(x==v) break;
}
}
low[u]=min(low[u],low[v]);
}
else low[u]=min(low[u],dfn[v]);
}
}
int b[N],c[N],col[N],go[N];
bool tag[N];
int S[N],T[N],fail[N];
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d%d",&b[i],&c[i]);
for(int i=1;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
jb(u,v);jb(v,u);
}
for(int i=1;i<=n;i++) if(dfn[i]==0) tarjan1(i,0);
for(int i=1;i<=n;i++) dfn[i]=low[i]=0,vec[id[i]].push_back(i);
for(int i=1;i<=n;i++){
if(dfn[i]!=0) continue;
hd=cc=c2=0;tarjan2(i,0);
int p=id[i];
bool bol=0;
if(c2==1&&sz[c2]==es[c2]){
int len=0;
for(int v:vec[p]) S[++len]=b[v],T[len]=c[v];
for(int i=len+1;i<len*2;i++) S[i]=S[i-len];
fail[0]=fail[1]=0;
for(int i=2,k=0;i<=len;i++){
while(k>0&&T[k+1]!=T[i]) k=fail[k];
if(T[k+1]==T[i]) k++;
fail[i]=k;
}
for(int i=1,k=0;i<2*len;i++){
while(k>0&&T[k+1]!=S[i]) k=fail[k];
if(T[k+1]==S[i]) k++;
if(k==len){
bol=1;
break;
}
}
if(bol==0){
flag=0;
break;
}
}
else{
for(int v:vec[p]) col[b[v]]=0;
for(int v:vec[p]) col[b[v]]++,col[c[v]]--;
for(int v:vec[p]){
if(col[b[v]]!=0){
flag=0;
break;
}
}
for(int v:vec[p]){
if(col[b[v]]>0) bol=1;
col[b[v]]++;
}
for(int j=1;j<=c2;j++)
if(sz[j]<es[j]||sz[j]%2==0) bol=1;
if(bol==1) continue;
for(int v:vec[p]) col[c[v]]=v;
for(int v:vec[p]) go[v]=col[b[v]];
for(int v:vec[p]){
if(tag[v]) continue;
int len=1,x=go[v];tag[v]=1;
while(x!=v) tag[x]=1,len++,x=go[x];
if(len%2==0){
flag=0;
break;
}
}
}
}
if(flag==0) printf("im");
printf("possible");
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 29224kb
input:
6 7 1 1 2 2 3 3 1 2 2 3 3 1 1 2 2 3 1 3 3 4 4 5 5 6 4 6
output:
possible
result:
ok single line: 'possible'
Test #2:
score: 0
Accepted
time: 3ms
memory: 27308kb
input:
5 6 10 10 20 20 30 30 40 50 50 40 1 2 2 3 1 3 3 4 3 5 4 5
output:
impossible
result:
ok single line: 'impossible'
Test #3:
score: 0
Accepted
time: 4ms
memory: 27332kb
input:
25 32 10 10 20 30 30 20 40 40 50 60 60 70 70 50 80 90 90 130 100 100 110 120 120 110 130 80 140 160 150 170 160 140 170 150 180 190 190 180 200 200 200 200 220 220 230 230 240 240 250 250 1 25 1 3 2 25 2 3 3 25 3 4 4 5 5 6 5 7 6 7 6 10 8 9 8 10 9 10 10 11 11 13 10 12 12 13 10 14 14 15 15 16 16 17 14...
output:
possible
result:
ok single line: 'possible'
Test #4:
score: 0
Accepted
time: 0ms
memory: 27380kb
input:
4 5 1 2 2 3 3 4 4 1 1 2 2 3 1 3 2 4 1 4
output:
possible
result:
ok single line: 'possible'
Test #5:
score: -100
Wrong Answer
time: 0ms
memory: 28880kb
input:
26 31 70 170 210 230 160 130 180 110 40 200 140 120 90 30 220 70 230 140 190 80 30 180 80 60 170 50 50 90 200 20 10 10 100 210 150 150 110 220 20 160 60 190 120 40 130 100 1234 1234 666 666 88888 88888 1 2 2 3 3 4 4 5 5 6 6 7 1 7 2 8 8 9 2 9 3 10 10 11 3 11 10 12 12 13 13 14 14 15 10 15 3 16 16 17 3...
output:
impossible
result:
wrong answer 1st lines differ - expected: 'possible', found: 'impossible'