QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#432669 | #8686. Zoo Management | grass8cow | WA | 19ms | 87572kb | C++17 | 3.6kb | 2024-06-07 14:59:40 | 2024-06-07 14:59:40 |
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]++;
}
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: 100
Accepted
time: 19ms
memory: 85540kb
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: 11ms
memory: 85532kb
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: 11ms
memory: 87572kb
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: 8ms
memory: 79388kb
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: 0
Accepted
time: 10ms
memory: 81380kb
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:
possible
result:
ok single line: 'possible'
Test #6:
score: 0
Accepted
time: 11ms
memory: 83528kb
input:
23 29 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 160 20 220 60 190 120 40 130 100 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 17 3 18 18 19 19 20 20 21 3 2...
output:
impossible
result:
ok single line: 'impossible'
Test #7:
score: 0
Accepted
time: 12ms
memory: 81368kb
input:
23 29 70 170 210 230 160 130 180 110 40 200 140 120 90 30 30 70 230 140 190 80 30 180 80 60 170 50 50 90 200 20 10 10 100 210 150 150 110 160 20 30 60 190 120 40 130 100 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 17 3 18 18 19 19 20 20 21 3 21 ...
output:
possible
result:
ok single line: 'possible'
Test #8:
score: -100
Wrong Answer
time: 7ms
memory: 83480kb
input:
27 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 88887 88887 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 ...
output:
possible
result:
wrong answer 1st lines differ - expected: 'impossible', found: 'possible'