QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#623899#5307. Subgraph Isomorphism0x3fffffffWA 58ms3632kbC++202.9kb2024-10-09 14:20:282024-10-09 14:20:37

Judging History

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

  • [2024-10-09 14:20:37]
  • 评测
  • 测评结果:WA
  • 用时:58ms
  • 内存:3632kb
  • [2024-10-09 14:20:28]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;

using ULL = unsigned long long;
const ULL mask = mt19937_64(chrono::steady_clock::now().time_since_epoch().count())();

ULL shift(ULL x) {
    x ^= mask;
    x ^= x << 13;
    x ^= x >> 7;
    x ^= x << 17;
    x ^= mask;
    return x;
}

void solve(){
    int n,m;cin>>n>>m;
    vector<vector<int>>e(n+1);
    vector<int>in(n+1);    
    for(int i=1;i<=m;i++){
        int a,b;cin>>a>>b;
        e[a].push_back(b);
        e[b].push_back(a);
        in[a]++;in[b]++;
    }
    if(m==n-1){
        cout<<"YES\n";return;
    }
    if(m>n){
        cout<<"NO\n";return;
    }

    queue<int>q;
    vector<int>vis(n+1);
    for(int i=1;i<=n;i++){
        if(in[i]==1){
            q.push(i);
        }
    }
    int cnt=n;
    while(not q.empty()){
        int u=q.front();
        q.pop();
        vis[u]=1;cnt--;
        for(int v:e[u]){
            if(!vis[v] and --in[v]==1){
                q.push(v);
            }
        }
    }

    int pre=0,cur=0;
    for(int i=1;i<=n;i++){
        if(!vis[i]){
            cur=i;break;
        }
    }

    vector<vector<int>>g(n+1);
    vector<int>st(n+1);

    auto dfs2=[&](auto &&ff,int u,int fa)->void{
        st[u]=1;
        for(int v:e[u]){
            if(st[v]||vis[v])continue;
            g[u].push_back(v);
            g[v].push_back(u);
            ff(ff,v,u);
        }
    };
    dfs2(dfs2,cur,0);


    vector<ULL>hase(n + 1);
    
    auto get = [&](auto&& ff, int u, int fa)->void {
        for (int v : g[u]) {
            if (v == fa)continue;
            // cout<<u<<"->"<<v<<"\n";
            ff(ff, v, u);
            hase[u] += shift(hase[v]);
        }
    };
    get(get, cur, 0);
    vector<ULL>ha(n + 1);
    ha[cur] = hase[cur];
    auto dfs1 = [&](auto&& ff, int u, int fa)->void {
        for (int v : g[u]) {
            if (v == fa)continue;
            ha[v] = hase[v] + shift(ha[u] - shift(hase[v]));
            ff(ff, v, u);
        }
    };
    dfs1(dfs1, cur, 0);//换根

    // for(int i=1;i<=n;i++){
    //     cout<<ha[i]<<"\n";
    // }
   

    vector<int>h;
    for(int i=1;i<=cnt;i++){
        h.push_back(cur);
        int now=0;
        for(int v:e[cur]){
            if(!vis[v] and v!=pre){
                now=v;break;
            }
        }
        pre=cur;
        cur=now;
    }
    for(int i=0;i+2<cnt;i++){
        int p=i+2;
        if(p>cnt)p-=cnt;
        // cout<<h[i]<<" "<<h[p]<<"\n";
        if(ha[h[i]]!=ha[h[p]]){
            cout<<"NO\n";return;
        }
    }
    cout<<"YES\n";


    // cout<<cnt<<"\n";
    // for(int i=1;i<=n;i++){
    //     cout<<vis[i]<<" ";
    // }
    // cout<<"\n";
}

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int tt=1;
    cin>>tt;
    while(tt--)
        solve();
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3632kb

input:

4
7 6
1 2
2 3
3 4
4 5
5 6
3 7
3 3
1 2
2 3
3 1
5 5
1 2
2 3
3 4
4 1
1 5
1 0

output:

YES
YES
NO
YES

result:

ok 4 token(s): yes count is 3, no count is 1

Test #2:

score: -100
Wrong Answer
time: 58ms
memory: 3632kb

input:

33192
2 1
1 2
3 2
1 3
2 3
3 3
1 2
1 3
2 3
4 3
1 4
2 4
3 4
4 3
1 3
1 4
2 4
4 4
1 3
1 4
2 4
3 4
4 4
1 3
1 4
2 3
2 4
4 5
1 3
1 4
2 3
2 4
3 4
4 6
1 2
1 3
1 4
2 3
2 4
3 4
5 4
1 5
2 5
3 5
4 5
5 4
1 4
1 5
2 5
3 5
5 5
1 4
1 5
2 5
3 5
4 5
5 5
1 4
1 5
2 4
3 5
4 5
5 5
1 4
1 5
2 4
2 5
3 5
5 6
1 4
1 5
2 4
2 5
3 ...

output:

YES
YES
YES
YES
YES
YES
NO
NO
NO
YES
YES
YES
YES
NO
NO
NO
NO
YES
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
YES
YES
YES
YES
YES
NO
NO
NO
NO
NO
NO
NO
NO
YES
YES
NO
YES
YES
YES
YES
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO...

result:

wrong answer expected NO, found YES [6th token]