QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#694835#5307. Subgraph IsomorphismGolem__#WA 19ms59024kbC++172.5kb2024-10-31 18:46:002024-10-31 18:46:09

Judging History

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

  • [2024-10-31 18:46:09]
  • 评测
  • 测评结果:WA
  • 用时:19ms
  • 内存:59024kb
  • [2024-10-31 18:46:00]
  • 提交

answer

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>

template <typename T>
inline void read(T &s)
{
    s=0;int f=1;char ch=getchar();
    for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-f;
    for(;ch>='0'&&ch<='9';ch=getchar())s=s*10+ch-'0';
    s*=f;
}

#define ll long long
#define ull unsigned ll
#define Vint std::vector<int>

struct Hash
{
    ull h1,h2;
    Hash(){}
    Hash(ull ch):h1(ch),h2(ch){}
    Hash(ull _h1,ull _h2):h1(_h1),h2(_h2){}
    Hash operator +(const Hash &rhs)const{return Hash(h1+rhs.h1,h2+rhs.h2);}
    Hash operator *(const ull &rhs)const{return Hash(h1*rhs,h2*rhs);}
    bool operator ==(const Hash &rhs)const{return h1==rhs.h1&&h2==rhs.h2;}
    bool operator !=(const Hash &rhs)const{return !((*this)==rhs);}
};

const int N=2e6+99;
namespace Pr
{
int cnt,vis[N],prime[N];
void init()
{
    for(int i=2;i<N;i++)
    {
        if(!vis[i])vis[i]=prime[++cnt]=i;
        for(int j=1;j<=cnt;j++)
        {
            if(prime[j]>vis[i]||prime[j]*i>=N)break;
            vis[prime[j]*i]=prime[j];
        }
    }
}
}

Vint G[N],stc;
int vis[N];
int dfs1(int u,int fa)
{
    vis[u]=1;
    for(int v:G[u])
    {
        if(v==fa)continue;
        if(vis[v])return v;
        int ans=dfs1(v,u);
        if(ans)return ans;
    }
    return 0;
}
bool dfs2(int u,int fa)
{
    vis[u]=1;
    for(int v:G[u])
    {
        if(v==fa)continue;
        if(vis[v]||dfs2(v,u))
        {
            stc.push_back(u);
            return 1;
        }
    }
    return 0;
}

Hash F[N];
int Size[N];
void dfs3(int u,int fa)
{
    F[u]=Hash(1);
    Size[u]=1;
    for(int v:G[u])
    {
        if(v==fa||vis[v])continue;
        dfs3(v,u);Size[u]+=Size[v];
        F[u]=F[u]+F[v]*Pr::prime[Size[v]];
    }
}

void work()
{
    int n,m;read(n);read(m);
    for(int u=1;u<=n;u++)G[u].clear(),vis[u]=0;
    for(int i=1;i<=m;i++)
    {
        int u,v;read(u);read(v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    if(m>n)return puts("NO"),void();
    if(m==n-1)return puts("YES"),void();
    int rt=dfs1(1,0);
    for(int i=1;i<=n;i++)vis[i]=0;
    stc.clear();dfs2(rt,0);
    for(int u=1;u<=n;u++)vis[u]=0;
    for(int u:stc)vis[u]=1;
    for(int u:stc)dfs3(u,0);
    Hash now=F[stc[0]];
    for(int i=1;i<(int)stc.size();i++)
    if(now!=F[stc[i]])return puts("NO"),void();
    puts("YES");
}

int main()
{
    //freopen("_.in","r",stdin);
    Pr::init();
    int T;read(T);
    while(T--)work();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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
NO
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 YES, found NO [39th token]