QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#630323#5307. Subgraph Isomorphismlzx2017WA 46ms9976kbC++202.0kb2024-10-11 17:54:532024-10-11 17:54:54

Judging History

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

  • [2024-10-11 17:54:54]
  • 评测
  • 测评结果:WA
  • 用时:46ms
  • 内存:9976kb
  • [2024-10-11 17:54:53]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+10;
int i,j,n,m,k,l,x,y,t,vis[N],bz[N],dl[N],yes,ha[N];
vector<int> G[N];
stack<int> dq;
void dfs(int x,int father)
{
    vis[x]=1;
    for(auto v:G[x]) if(v!=father){
        if(yes) return;
        if(!vis[v]){
            dq.push(v);
            dfs(v,x);
            if(yes) return;
            if(dq.size()) dq.pop();
        } else {
            while(dq.top()!=v){
                bz[dq.top()]=1;
                dl[++dl[0]]=dq.top();
                dq.pop();
            }
            bz[v]=1;
            dl[++dl[0]]=v;
            dq.pop();
            yes=1;
            return;
        }
    }
}
void dfs1(int x,int father){
    ha[x]=1992579;
    for(auto v:G[x]) if(v!=father&&!bz[v]){
        dfs1(v,x);
        ha[x]=(ha[x]+1145147)^ha[v];
    }
}
void solve()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        vis[i]=bz[i]=0;G[i].clear();ha[i]=0;
    }
    for(int i=1;i<=m;i++)
    {
        cin>>x>>y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    if(m==n-1) cout<<"YES"<<endl;
    else if(m>n) cout<<"NO"<<endl;
    else{
        while(dq.size()) dq.pop();
        dq.push(1);dl[0]=0;yes=0;
        dfs(1,0);
        for(int i=1;i<=dl[0];i++) dfs1(dl[i],0);
        if(dl[0]%2==1){
            for(int i=1;i<dl[0];i++) if(ha[dl[i]]!=ha[dl[i+1]]){
                cout<<"NO"<<endl;
                return;
            }
        }
        else {
            for(int i=1;i<dl[0]-1;i+=2) if(ha[dl[i]]!=ha[dl[i+2]]){
                cout<<"NO"<<endl;
                return;
            }
            for(int i=2;i<dl[0]-1;i+=2) if(ha[dl[i]]!=ha[dl[i+2]]){
                cout<<"NO"<<endl;
                return;
            }
        }
        cout<<"YES"<<endl;
    }
}
signed main()
{
   // freopen("1.in","r",stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>t;
    while(t--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 9644kb

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: 46ms
memory: 9976kb

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

result:

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