QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#425396 | #8686. Zoo Management | by_chance | WA | 3ms | 48424kb | C++14 | 3.2kb | 2024-05-30 10:23:20 | 2024-05-30 10:23:20 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
void ed(int type){fprintf(stderr,"%d\n",type);printf("impossible\n");exit(0);}
const int N=4e5+5,M=1e6+5;
int n,m,a[N],b[N],head[N],nxt[N<<1],ver[N<<1],tot;
void add(int u,int v){ver[++tot]=v;nxt[tot]=head[u];head[u]=tot;}
int dfn[N],low[N],dcnt,st[N],top,id[N],num;
void tarjan(int u,int fa){
dfn[u]=low[u]=++dcnt;st[++top]=u;
for(int i=head[u],v;i;i=nxt[i])if((v=ver[i])!=fa){
if(!dfn[v])tarjan(v,u),low[u]=min(low[u],low[v]);
else low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u]){
++num;int y;
do id[y=st[top--]]=num; while(y!=u);
}
}
vector<int> p[N],G[N];
int flag[N],ecnt[N],c1[M],c2[M],s[N],t[N],len,fa[N],sum[N],dep[N],res,odd;
void dfs0(int u){
s[len]=a[u];t[len]=b[u];++len;dfn[u]=1;
for(int v:G[u])if(!dfn[v])dfs0(v);
}
void dfs(int u){
dfn[u]=++dcnt;s[++len]=a[u];t[len]=b[u];
for(int v:G[u])if(v!=fa[u]){
if(!dfn[v])dep[v]=dep[u]+1,fa[v]=u,dfs(v);
else if(dfn[u]<dfn[v]){
if(dep[v]-dep[u]&1)res=1;
--sum[u],++sum[v];
}
}
}
void dfs1(int u){for(int v:G[u])if(fa[v]==u)dfs1(v),sum[u]+=sum[v];}
namespace BIT{
int c[M];
void add(int x){for(;x<M;x+=x&-x)c[x]^=1;}
int ask(int x){int s=0;for(;x;x-=x&-x)s^=c[x];return s;}
}
void check(int st){
int x=id[st];len=0;res=0;odd=0;flag[x]=1;
for(int u:p[x])++c1[a[u]],++c2[b[u]];
for(int u:p[x]){
if(c1[a[u]]!=c2[a[u]])ed(1);
if(c1[b[u]]!=c2[b[u]])ed(1);
//if(c1[a[u]]>=2||c1[b[u]]>=2)odd=1;
c1[a[u]]=c2[a[u]]=c1[b[u]]=c2[b[u]]=0;
}
if(p[x].size()==ecnt[x]){
dfs0(st);int p1=0,p2=0,i=0,j=1,k=0;
while(k<len&&i<len&&j<len){
if(s[(i+k)%len]==s[(j+k)%len])++k;
else{
if(s[(i+k)%len]>s[(j+k)%len])i=i+k+1;
else j=j+k+1; if(i==j)++i; k=0;
}
} p1=min(i,j); i=0;j=1;k=0;
while(k<len&&i<len&&j<len){
if(t[(i+k)%len]==t[(j+k)%len])++k;
else{
if(t[(i+k)%len]>t[(j+k)%len])i=i+k+1;
else j=j+k+1; if(i==j)++i; k=0;
}
} p2=min(i,j);
for(k=0;k<len;k++)if(s[(p1+k)%len]!=t[(p2+k)%len])ed(2);
return;
}
if(odd)return;dcnt=0;dfs(st);dfs1(st);
for(int u:p[x])if(sum[u]>=2)res=1;if(res)return;
for(int i=len;i>=1;i--)odd^=BIT::ask(a[i]),BIT::add(a[i]);
for(int i=len;i>=1;i--)BIT::add(a[i]);
for(int i=len;i>=1;i--)odd^=BIT::ask(b[i]),BIT::add(b[i]);
for(int i=len;i>=1;i--)BIT::add(b[i]);
if(odd)ed(3);
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%d%d",a+i,b+i);
for(int i=1,u,v;i<=m;i++){
scanf("%d%d",&u,&v);
add(u,v);add(v,u);
}
for(int i=1;i<=n;i++)if(!dfn[i])tarjan(i,0);
for(int i=1;i<=n;i++)p[id[i]].push_back(i);
for(int i=1;i<=m;i++){
int u=ver[2*i-1],v=ver[2*i];
if(id[u]==id[v]){++ecnt[id[u]];G[u].push_back(v);G[v].push_back(u);}
}
memset(dfn,0,sizeof(dfn));
for(int i=1;i<=n;i++)if(!flag[id[i]])check(i);
printf("possible\n");
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 40180kb
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: 48424kb
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: -100
Wrong Answer
time: 0ms
memory: 44324kb
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:
impossible
result:
wrong answer 1st lines differ - expected: 'possible', found: 'impossible'