QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#219609#5307. Subgraph IsomorphismisaunoyaWA 64ms7376kbC++232.2kb2023-10-19 16:43:472023-10-19 16:43:48

Judging History

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

  • [2023-10-19 16:43:48]
  • 评测
  • 测评结果:WA
  • 用时:64ms
  • 内存:7376kb
  • [2023-10-19 16:43:47]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<vector>
#include<random>
#include<chrono>
#include<queue>
#include<map>
using namespace std;
const int N=100005;
const unsigned long long S=1;
unsigned long long shift(unsigned long long x)
{
  static const unsigned long long mask=std::chrono::steady_clock::now().time_since_epoch().count();
  x^=mask;
  x^=x<<13;
  x^=x>>7;
  x^=x<<17;
  x^=mask;
  return x;
}
int n,m;
vector<int>G[N];
int deg[N];
bool vis[N];
unsigned long long f[N];
void dfs(int u)
{
  f[u]=S;
  vis[u]=true;
  for(int v:G[u])
  {
    if(vis[v]) continue;
    dfs(v);
    f[u]+=shift(f[v]);
  }
  return;
}
void solve()
{
  cin>>n>>m;
  for(int i=1;i<=n;i++)
    G[i].clear(),deg[i]=0;
  map<pair<int,int>,bool>book;
  for(int i=1;i<=m;i++)
  {
    int x,y;
    cin>>x>>y;
    if(book.count({x,y})) continue;
    book[{x,y}]=true;
    G[x].emplace_back(y);
    G[y].emplace_back(x);
    deg[x]++,deg[y]++;
  }
  if(m==n-1)
  {
    cout<<"YES\n";
    return;
  }
  if(m>n)
  {
    cout<<"NO\n";
    return;
  }
  queue<int>q;
  for(int i=1;i<=n;i++)
    if(deg[i]==0) q.emplace(i);
  while(!q.empty())
  {
    int u=q.front();
    q.pop();
    for(int v:G[u])
    {
      deg[v]--;
      if(deg[v]==1) q.emplace(v);
    }
  }
  for(int i=1;i<=n;i++)
    if(deg[i]==2) vis[i]=true;
    else vis[i]=false;
  for(int i=1;i<=n;i++)
    if(deg[i]==2) dfs(i);
  vector<int>pos;
  for(int i=1;i<=n;i++)
    if(deg[i]==2)
    {
      q.emplace(i);
      break;
    }
  while(!q.empty())
  {
    int u=q.front();
    q.pop();
    for(int v:G[u])
    {
      deg[v]--;
      if(deg[v]==1) q.emplace(v),pos.emplace_back(v);
    }
  }
  int l=pos.size();
  bool all_same=true;
  for(int i=1;i<l;i++)
    if(f[pos[i]]!=f[pos[i-1]])
    {
      all_same=false;
      break;
    }
  bool gap_same=false;
  if(l%2==0)
  {
    gap_same=true;
    for(int i=2;i<l;i++)
      if(f[pos[i]]!=f[pos[i-2]])
      {
        gap_same=false;
        break;
      }
  }
  if(all_same||gap_same) cout<<"YES\n";
  else cout<<"NO\n";
  return;
}
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr),cout.tie(nullptr);
  int T;
  cin>>T;
  while(T--)
    solve();
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 64ms
memory: 6044kb

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

result:

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