QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#758738#5307. Subgraph Isomorphismeinekleine18WA 48ms3792kbC++202.4kb2024-11-17 19:31:532024-11-17 19:31:54

Judging History

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

  • [2024-11-17 19:31:54]
  • 评测
  • 测评结果:WA
  • 用时:48ms
  • 内存:3792kb
  • [2024-11-17 19:31:53]
  • 提交

answer

#include<bits/stdc++.h>

#ifdef DEBUG
#include "debug.h"
#else
#define debug(...) 42
#endif

#define int long long
typedef unsigned long long ull;
const ull mask = std::chrono::steady_clock::now().time_since_epoch().count();
void solve(){
    int n,m;
    std::cin>>n>>m;
    std::vector g(n,std::vector<int>());
    std::vector<int>du(n);
    for(int i=0;i<m;++i){
        int a,b;
        std::cin>>a>>b;
        --a,--b;
        g[a].push_back(b);
        g[b].push_back(a);
        ++du[a];
        ++du[b];
    }
    if(m==n-1){
        std::cout<<"YES\n";
        return;
    }
    if(m>n){
        std::cout<<"NO\n";
        return;
    }
    std::queue<int>q;
    for(int i=0;i<n;++i){
        if(du[i]==1){
            q.push(i);
        }
    }
    while(!q.empty()){
        int t=q.front();q.pop();
        for(auto &x:g[t]){
            if(--du[x]==1){
                q.push(x);
            }
        }
    }
    auto shift=[&](ull x)->ull {
        x^=mask;
        x^=x<<13;
        x^=x>>7;
        x^=x<<17;
        x^=mask;
        return x;
    };
    auto hash=[&](auto self,int u,int fa)->ull {
        ull res=1;
        for(auto &x:g[u]){
            if(x==fa||du[x]!=1) continue;
            ull son=self(self,x,u);
            res+=shift(son);
        }
        return res;
    };
    std::vector<ull>tr;
    int sz=std::count(du.begin(),du.end(),2);
    int root=-1;
    for(int i=0;i<n;++i){
        if(du[i]==2){
            root=i;
            break;
        }
    }
    int last=-1;
    for(int i=0;i<sz;++i){
        tr.push_back(hash(hash,root,-1));
        for(auto &x:g[root]){
            if(x!=last&&du[x]==2){
                last=root;
                root=x;
                break;
            }
        }
    }
    sz=tr.size();
    for(int i=0;i<sz;++i){
        if(tr[i]!=tr[(i+2)%sz]){
            std::cout<<"NO\n";
            return;
        }
    }
    std::cout<<"YES\n";
}
signed main(){
    // freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
    // std::cout<<std::fixed<<std::setprecision(12);
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);std::cout.tie(nullptr);
    int _=1;
    std::cin>>_;
    // clock_t start=clock();
    for(int i=1;i<=_;++i){
        solve();
    }
    // clock_t end=clock();
    // double time=static_cast<double>(end-start)/CLOCKS_PER_SEC;
    // std::cout<<time<<'\n';
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3792kb

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: 48ms
memory: 3564kb

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
NO
YES
NO
NO
YES
YES
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
YES
YES
NO
YES
YES
NO
NO
NO
YES
NO
NO
NO
NO
NO
YES
NO
NO
NO
YES
NO
NO
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
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO...

result:

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