QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#432577#8686. Zoo ManagementTx_LcyWA 20ms122780kbC++145.5kb2024-06-07 12:43:432024-06-07 12:43:44

Judging History

你现在查看的是最新测评结果

  • [2024-06-07 12:43:44]
  • 评测
  • 测评结果:WA
  • 用时:20ms
  • 内存:122780kb
  • [2024-06-07 12:43:43]
  • 提交

answer

//A tree without skin will surely die.
//A man without face is invincible.
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) (x).begin(),(x).end()
#define rep(i,j,k) for (int i=j;i<=k;++i)
#define per(i,j,k) for (int i=j;i>=k;--i)
int const N=1e6+10;
int p[N],q[N],f[N],vis[N],dep[N],cha[N],pre[N],occupy[N],del[N],n,m,idx;
vector<int>a[N],huan[N],b[N],av[N];
inline int find(int x){return (x==f[x])?(x):(f[x]=find(f[x]));}
inline void dfs(int x,int fa){
    vis[x]=1,pre[x]=fa,dep[x]=dep[fa]+1;
    for (auto v:a[x]){
        if (v==fa) continue;
        if (vis[v]){
            if (dep[v]<dep[x]){
                ++idx,++cha[x],--cha[pre[v]];
                int it=x;
                vector<int>s;
                while (1){
                    if (occupy[it]){
                        del[occupy[it]]=del[idx]=1;
                        s.clear();
                        break;
                    }
                    s.push_back(it);
                    occupy[it]=idx;
                    if (it==v) break;
                    it=pre[it];
                }
                it=x;
                while (1){
                    it=find(it);
                    if (dep[it]>dep[v]) f[it]=pre[it];
                    else break;
                }
                huan[idx]=s;
            }
        }else dfs(v,x),b[x].push_back(v);
    }
}
inline void Dfs(int x){
    for (auto v:b[x]) Dfs(v),cha[x]+=cha[v];
}
struct Hash_table{
    int const mod1=998244353;
    int const mod2=1e9+7;
    int const B=1372549;
    int n,hsh1[N],hsh2[N],bse1[N],bse2[N];
    inline void init(vector<int>vec){
        n=vec.size();
        rep(i,1,n) hsh1[i]=(hsh1[i-1]*B%mod1+vec[i-1])%mod1;
        rep(i,1,n) hsh2[i]=(hsh2[i-1]*B%mod2+vec[i-1])%mod2;
        bse1[0]=bse2[0]=1;
        rep(i,1,n) bse1[i]=bse1[i-1]*B%mod1;
        rep(i,1,n) bse2[i]=bse2[i-1]*B%mod2;
    }
    inline pair<int,int> query(int l,int r){
        return make_pair((hsh1[r]+mod1-hsh1[l-1]*bse1[r-l+1]%mod1)%mod1,(hsh2[r]+mod2-hsh2[l-1]*bse2[r-l+1]%mod2)%mod2);
    }
}T1,T2;
inline bool chk(vector<int>v1,vector<int>v2){
    int sz=v1.size();
    vector<int>k;
    for (auto i:v1) k.push_back(i);
    for (auto i:v1) k.push_back(i);
    T1.init(k),T2.init(v2);
    pair<int,int> S=T2.query(1,sz);
    rep(i,1,sz)
        if (T1.query(i,i+sz-1)==S) return 1;
    return 0;
}
int tong[N];
namespace BF{
    vector< vector<int> >huan;
    bool mp[22][22];int u[22],v[22];
    map< vector<int>,int >sz;
    inline void dfs(vector<int>vec){
        if (sz[vec]) return;
        sz[vec]=1;
        for (auto it:huan){
            vector<int>kk=vec;
            int la=vec[it[0]-1];
            rep(k,0,(int)it.size()-2) vec[it[k]-1]=vec[it[k+1]-1];
            vec[it.back()-1]=la;
            dfs(vec);
            vec=kk;
        }
    }
    inline void work(){
        rep(i,1,n) cin>>p[i]>>q[i];
        rep(i,1,m) cin>>u[i]>>v[i];
        rep(i,1,m)
            mp[u[i]][v[i]]=mp[v[i]][u[i]]=1;
        rep(S,1,(1<<n)-1){
            vector<int>V;
            rep(i,1,n)
                if (S>>(i-1)&1) V.push_back(i);
            if (V.size()<=2) continue;
            do{
                if (!mp[V[0]][V.back()]) continue;
                int tag=1;
                rep(i,1,(int)V.size()-1)
                    if (!mp[V[i-1]][V[i]]){tag=0;break;}
                if (!tag) continue;
                // for (auto i:V) cerr<<i<<' ';
                // cerr<<'\n';
                huan.push_back(V);break;
            }while (next_permutation(all(V)));
        }
        vector<int>v1,v2;
        rep(i,1,n) v1.push_back(p[i]),v2.push_back(q[i]);
        dfs(v1);
        // vector<int>k;
        // rep(i,1,n) k.push_back(i);
        // do{
            // if (!sz[k]) continue;
            // for (auto i:k) cout<<i<<' ';
            // cout<<'\n';
        // }while (next_permutation(all(k)));
        if (sz[v2]) cout<<"possible\n";
        else cout<<"impossible\n";
    }
}
void solve(){
    cin>>n>>m;
    if (n<=5) return BF::work();
    rep(i,1,n) cin>>p[i]>>q[i];
    rep(i,1,n) f[i]=i;
    rep(i,1,m){
        int x,y;cin>>x>>y;
        a[x].push_back(y);
        a[y].push_back(x);
    }
    vector<int>roots;
    rep(i,1,n)
        if (!vis[i]) dfs(i,0),roots.push_back(i);
    for (auto i:roots) Dfs(i);
    rep(i,1,n) av[find(i)].push_back(i);
    rep(i,1,n){
        for (auto j:av[i]) ++tong[p[j]];
        for (auto j:av[i]){
            --tong[q[j]];
            if (tong[q[j]]<0) return cout<<"impossible\n",void();
        }
    }
    rep(i,1,n){
        if (cha[i]) continue;
        if (p[i]!=q[i]) return cout<<"impossible\n",void();
    }
    rep(i,1,idx){
        if (del[i]) continue;
        if (!huan[i].size()) continue;
        int tg=0;
        for (auto it:huan[i]) if (cha[it]>1){++tg;break;}
        if (tg) continue;
        // for (auto it:huan[i]) cerr<<it<<' ';
        // cerr<<'\n';
        vector<int>v1,v2;
        for (auto it:huan[i]) v1.push_back(p[it]),v2.push_back(q[it]);
        for (auto it:huan[i]) p[it]=q[it];
        if (!chk(v1,v2)) return cout<<"impossible\n",void();
    }
    cout<<"possible\n";
}
signed main(){
    // freopen("zoo.in","r",stdin);
    // freopen("zoo.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int t=1;
    // cin>>t;
    while (t--) solve();
    cerr<<"Time: "<<(double)clock()/CLOCKS_PER_SEC<<" s\n";
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 19ms
memory: 122780kb

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: 20ms
memory: 108444kb

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: 18ms
memory: 122628kb

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: 20ms
memory: 108276kb

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: 19ms
memory: 114492kb

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: -100
Wrong Answer
time: 12ms
memory: 112504kb

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:

possible

result:

wrong answer 1st lines differ - expected: 'impossible', found: 'possible'