QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#432636 | #8686. Zoo Management | cqbzly | WA | 17ms | 89652kb | C++14 | 3.5kb | 2024-06-07 14:31:25 | 2024-06-07 14:31:28 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
using namespace std;
const int N=1e6+5;
const int P1=1333331;
const int P2=13333331;
const int mod=1e9+7;
int n,m,tot,pre[N],fa[N],now[N],low[N],dfn[N],pos[N],sz,num,edge[N],cnt;
ll f1[N],f2[N],h1[N],h2[N];
bool vs[N],flg[N],ban[N],ok;
vector<int>p,q;
vector<int>G[N],to[N],vec[N];
stack<int>stk;
pair<int,int>fk[N];
void add(int x,int y){
to[x].pb(y);
to[y].pb(x);
}
void tarjan(int u){
dfn[u]=low[u]=++num,stk.push(u);
for(auto v:G[u]){
if(!dfn[v]){
tarjan(v),low[u]=min(low[u],low[v]);
if(low[v]>=dfn[u]){
tot++;
int tmp;
do{
tmp=stk.top(),stk.pop();
vec[tot].pb(tmp);
add(tmp,tot),fa[tmp]=tot;
}while(tmp!=v);
vec[tot].pb(u);
add(u,tot),fa[tot]=u;
if(vec[tot].size()==2)ban[tot]=1;
}
}
else low[u]=min(low[u],dfn[v]);
}
}
ll get(int l,int r){
ll x=(h1[r]-h1[l-1]*f1[r-l+1]%mod+mod)%mod,y=(h2[r]-h2[l-1]*f2[r-l+1]%mod+mod)%mod;
return x*mod+y;
}
void dfs(int u){
vs[u]=1;
if(u<=n){
p.pb(pre[u]);
q.pb(now[u]);
}
else{
if(!cnt)cnt=u;
else ok=1;
ok|=flg[u];
}
for(auto v:to[u]){
if(vs[v]||ban[v])continue;
dfs(v);
}
}
int main(){
//freopen("zoo.in","r",stdin);
//freopen("zoo.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n>>m,tot=n;
f1[0]=1;for(int i=1;i<=n*2;i++)f1[i]=f1[i-1]*P1%mod;
f2[0]=1;for(int i=1;i<=n*2;i++)f2[i]=f2[i-1]*P2%mod;
for(int i=1;i<=n;i++)cin>>pre[i]>>now[i];
for(int i=1;i<=m;i++){
int u,v;cin>>u>>v;
G[u].pb(v),G[v].pb(u);
fk[i]={u,v};
}
for(int i=1;i<=n;i++){
if(!dfn[i])tarjan(i);
}
for(int i=1;i<=m;i++){
int u=fk[i].fi,v=fk[i].se;
if(fa[u]==fa[v]||fa[fa[u]]==v)edge[fa[u]]++;
else edge[fa[v]]++;
}
for(int i=n+1;i<=tot;i++){
if(edge[i]>vec[i].size())flg[i]=1;
}
for(int i=1;i<=n;i++){
if(!vs[i]){
p.clear(),q.clear(),cnt=ok=0;
dfs(i);
if(ok||p.size()==1){
sort(p.begin(),p.end());
sort(q.begin(),q.end());
for(int j=0;j<p.size();j++){
if(p[j]!=q[j]){
cout<<"impossible";
return 0;
}
}
}
else{
//cout<<cnt<<"\n";
sz=0;for(auto e:vec[cnt])pos[++sz]=e;
for(int j=1;j<=sz;j++)pos[sz+j]=pos[j];
ll sm1=0,sm2=0;
for(int j=1;j<=sz;j++){
sm1=(sm1*P1+now[pos[j]])%mod;
sm2=(sm2*P2+now[pos[j]])%mod;
}
ll sm=sm1*mod+sm2;
for(int j=1;j<=sz*2;j++){
h1[j]=(h1[j-1]*P1+pre[pos[j]])%mod;
h2[j]=(h2[j-1]*P2+pre[pos[j]])%mod;
}
bool o=0;
for(int j=1;j<=sz;j++){
if(get(j,j+sz-1)==sm)o=1;
}
if(!o){
cout<<"impossible";
return 0;
}
}
}
}
cout<<"possible";
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 17ms
memory: 89652kb
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: 11ms
memory: 89636kb
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'