QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#432573 | #8686. Zoo Management | World_Creater | WA | 0ms | 22280kb | C++20 | 2.2kb | 2024-06-07 12:39:02 | 2024-06-07 12:39:02 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int n,m,pre[400005],suc[400005];
int vis[1000005];
int nxt[800005],head[400005],go[800005],k;
int low[400005],dfn[400005],idx,sd[400005],sza[400005],szb[400005],cnt;
stack<int> sta;
vector<int> pt[400005];
int v1[400005],v2[800005],ic,kmp[400005];
struct edge{
int u,v;
}a[400005];
void End()
{
cout<<"impossible";
exit(0);
}
void add(int u,int v)
{
nxt[++k]=head[u];
head[u]=k;
go[k]=v;
}
void tarjan(int x,int fa)
{
low[x]=dfn[x]=++idx;
sta.emplace(x);
for(int i=head[x];i;i=nxt[i])
{
int g=go[i];
if(g==fa) continue ;
if(!dfn[g])
{
tarjan(g,x);
low[x]=min(low[x],low[g]);
}
else low[x]=min(low[x],dfn[g]);
}
if(low[x]==dfn[x])
{
int p;
cnt++;
do{
p=sta.top();
sta.pop();
sd[p]=cnt;
pt[cnt].emplace_back(p);
sza[cnt]++;
}while(p!=x);
}
}
void check()
{
int j=0;
for(int i=1;i<=ic;i++) v2[i+ic]=v2[i];
for(int i=2;i<=ic;i++)
{
while(j&&v1[i]!=v1[j+1]) j=kmp[j];
if(v1[j+1]==v1[i]) j++;
kmp[i]=j;
}
j=0;
for(int i=1;i<=2*ic;i++)
{
while(j&&v2[i]!=v1[j+1]) j=kmp[j];
if(v1[j+1]==v2[i]) j++;
if(j==ic) return ;
}
End();
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>pre[i]>>suc[i];
}
for(int i=1;i<=m;i++)
{
int u,v;
cin>>u>>v;
a[i]={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<=m;i++)
{
if(sd[a[i].u]==sd[a[i].v]) szb[sd[a[i].u]]++;
}
for(int i=1;i<=cnt;i++)
{
// cerr<<i<<" "<<sza[i]<<" "<<szb[i]<<"\n";
if(sza[i]==szb[i])
{
ic=0;
int x=pt[i].front(),j=x,lst=-1;
do
{
v1[++ic]=suc[j];
v2[ic]=pre[j];
for(int k=head[j];k;k=nxt[k])
{
int g=go[k];
if(sd[g]==i&&g!=lst)
{
lst=j;
j=g;
break ;
}
}
}while(j!=x);
check();
}
else
{
for(auto j:pt[i])
{
vis[pre[j]]++;
vis[suc[j]]--;
}
for(auto j:pt[i])
{
if(vis[pre[j]]!=0||vis[suc[j]]!=0) End();
}
for(auto j:pt[i])
{
vis[pre[j]]--;
vis[suc[j]]++;
}
}
}
cout<<"possible";
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 22280kb
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: -100
Wrong Answer
time: 0ms
memory: 16132kb
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:
possible
result:
wrong answer 1st lines differ - expected: 'impossible', found: 'possible'