QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#630374#5307. Subgraph Isomorphismlzx2017WA 42ms10096kbC++202.2kb2024-10-11 18:11:122024-10-11 18:11:13

Judging History

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

  • [2024-10-11 18:11:13]
  • 评测
  • 测评结果:WA
  • 用时:42ms
  • 内存:10096kb
  • [2024-10-11 18:11:12]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+10;
const int mod=1e15+7;
int i,j,n,m,k,l,x,y,t,vis[N],bz[N],dl[N],yes,ha[N],kk,mask;
vector<int> G[N];
stack<int> dq;
int shift(int x)
{
    x^=mask;x^=x<<13;
    x^=x>>7;x^=x<<17;
    x^=mask;
    return x;
}
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]=1;
    for(auto v:G[x]) if(v!=father&&!bz[v]){
        dfs1(v,x);
        ha[x]+=shift(ha[v]);ha[x]%=mod;
    }
}
void solve()
{
	kk++;
    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;
    	if(kk==19) cout<<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;
    mt19937 myrand(time(0));
    mask=myrand();
    while(t--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 42ms
memory: 9792kb

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
1|3|1|5|2|4|2|5|3|5|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
...

result:

wrong output format YES or NO expected, but 1|3|1|5|2|4|2|5|3|5|NO found [19th token]