QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#432666 | #8686. Zoo Management | grass8cow | WA | 7ms | 75200kb | C++17 | 3.6kb | 2024-06-07 14:58:51 | 2024-06-07 14:58:52 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int n,m;
const int N=8e5+10,M=1e6+10;
int U[M],V[M],p[N],q[N];
vector<int>g[N],G[N];
#define pb push_back
int dfn[N],low[N],sta[N],top,O;
int ve[N],fa[N];
void dfs(int x){
dfn[x]=low[x]=++dfn[0],sta[++top]=x;
for(int v:g[x]){
if(!dfn[v]){
dfs(v),low[x]=min(low[x],low[v]);
if(low[v]>=dfn[x]){
fa[++O]=x,G[x].pb(O);
while(sta[top]!=v)fa[sta[top]]=O,G[O].pb(sta[top--]);
fa[v]=O,G[O].pb(v),top--;
}
}
low[x]=min(low[x],dfn[v]);
}
}
#define vi vector<int>
vi p1,p2;
bool fu;int fk;
int hj[N];
vi E[N];
void dfs2(int x){
if(x>n&&G[x].size()==1)return;
for(int v:G[x])dfs2(v);
if(x<=n)p1.pb(p[x]),p2.pb(q[x]);
else{
if(fk==0)fk=x;
else fk=-1;
if(hj[x]>G[x].size()+1)fu=1;
if(G[x].size()&1)fu=1;
}
}
int tk[801000];
bool ch1(vi a,vi b){
for(int x:a)tk[x]++;for(int x:b)tk[x]--;
bool oo=1;
for(int x:a){if(tk[x]!=0)oo=0;tk[x]=0;}
for(int x:b){if(tk[x]!=0)oo=0;tk[x]=0;}
return oo;
}
int fz[N],z[N];bool ov[N];
bool ch2(vi a,vi b){
int ai=a.size();
for(int i=0;i<ai;i++)fz[a[i]]=0,ov[i+1]=0;
for(int i=0;i<ai;i++){
if(fz[a[i]])return 1;
fz[a[i]]=i+1;
}
for(int i=0;i<ai;i++)z[fz[b[i]]]=i+1;
int t=(ai&1);
for(int i=1;i<=ai;i++)if(!ov[i]){
t^=1;int u=i;
while(!ov[u])ov[u]=1,u=z[u];
}
return !t;
}
int kmp[N];
bool ch3(vi a,vi b){
int ai=a.size(),j=0;
for(int i=2;i<=ai;i++){
while(j&&b[j]!=b[i-1])j=kmp[j];
if(b[j]==b[i-1])j++;kmp[i]=j;
}
j=0;
for(int st=0;st<2;st++)for(int i=0;i<ai;i++){
while(j&&b[j]!=a[i])j=kmp[j];
if(b[j]==a[i])j++;if(j==ai)return 1;
}
return 0;
}
bool gg;
int ks[N][2];
bool ig[N];
void doi(int x){
vi Q;Q.pb(fa[x]);
for(int v:G[x])Q.pb(v);
for(int v:Q)ks[v][0]=ks[v][1]=0,ig[v]=0;
for(int i:E[x]){
int u=U[i],v=V[i];
if(!ks[u][0])ks[u][0]=v;else ks[u][1]=v;
if(!ks[v][0])ks[v][0]=u;else ks[v][1]=u;
}
vi V_;int u=Q[0];ig[u]=1;
while(1){
V_.pb(u);
int v=-1;
for(int i=0;i<2;i++)if(!ig[ks[u][i]])v=ks[u][i];
if(v==-1)break;
u=v,ig[u]=1;
}
vi A,B;for(int x:V_)A.pb(p[x]),B.pb(q[x]);
if(!ch3(A,B))gg=1;
}
int qc[N],cn;
void rd(int &x){
x=0;char c=getchar();
while(c<'0'||c>'9')c=getchar();
while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();
}
int main(){
rd(n),rd(m);O=n;
for(int i=1;i<=n;i++)rd(p[i]),rd(q[i]),qc[++cn]=p[i];
sort(qc+1,qc+cn+1);cn=unique(qc+1,qc+cn+1)-qc-1;
for(int i=1;i<=n;i++){
p[i]=lower_bound(qc+1,qc+cn+1,p[i])-qc;
int tt=lower_bound(qc+1,qc+cn+1,q[i])-qc;
if(tt<=cn&&qc[tt]==q[i])q[i]=tt;
else{puts("impossible");return 0;}
}
for(int i=1;i<=m;i++)rd(U[i]),rd(V[i]),
g[U[i]].pb(V[i]),g[V[i]].pb(U[i]);
dfs(1);
for(int i=1;i<=m;i++){
int fp=-1;
if(fa[V[i]]==fa[U[i]])fp=fa[U[i]];
else if(fa[fa[V[i]]]==U[i])fp=fa[V[i]];
else fp=fa[U[i]];
E[fp].pb(i),hj[fp]++;
}
return 0;
for(int i=1;i<=n;i++)if(i==1||G[fa[i]].size()==1){
fu=0,fk=0,p1.clear(),p2.clear();dfs2(i);
if(!ch1(p1,p2)){gg=1;continue;}
if(fk==-1){if(!fu&&!ch2(p1,p2))gg=1;}
else if(fk){
if(hj[fk]>G[fk].size()+1)continue;
doi(fk);
}
}
if(gg)puts("impossible");
else puts("possible");
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 7ms
memory: 75200kb
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:
result:
wrong answer 1st lines differ - expected: 'possible', found: ''