QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#630643#5307. Subgraph IsomorphismxzinML 0ms0kbC++142.3kb2024-10-11 19:43:592024-10-11 19:44:01

Judging History

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

  • [2024-10-11 19:44:01]
  • 评测
  • 测评结果:ML
  • 用时:0ms
  • 内存:0kb
  • [2024-10-11 19:43:59]
  • 提交

answer

#include <bits/stdc++.h>

#define N 201000
#define ull unsigned long long
using namespace std;

vector<int> vec[N];
const ull mask = std::mt19937_64(time(nullptr))();

int d[N], n, m;
int vis[N];
vector<int> nd;
ull hs[N];
void topo() {
    queue<int> q;

    for(int i = 1;i <= n; i++) 
        if(d[i] == 1) q.push(i);
    while (!q.empty())
    {
        int p = q.front(); q.pop(); vis[p] = 1;
        for(int i = 0; i < vec[p].size(); i++) {
            int v = vec[p][i];
            d[v]--;
            if(d[i] == 1) q.push(v);
            
        }
    }
    
}

ull shift(ull x) {
  x ^= mask;
  x ^= x << 13;
  x ^= x >> 7;
  x ^= x << 17;
  x ^= mask;
  return x;
}


void getHash(int x, int p) {
  hs[x] = 1;
  for (int i = 0 ; i < vec[x].size(); i++) {
    int v = vec[x][i];
    if (v == p || vis[v] == 0) continue;
    
    getHash(i, x);
    hs[x] += shift(hs[v]);
  }
}
void dfs(int p, int fa) {
    nd.push_back(p);
    for (int i = 0 ; i < vec[p].size(); i++) {
        int v = vec[p][i];
        if (v == fa || vis[v] == 1) continue;
        dfs(v, p);
    }
}
void sl() {
    cin>>n>>m;
    for(int i = 0; i <= n; i++) vec[i].clear(), hs[i] = 0, vis[i] = 0, d[i] = 0;
    map<pair<int, int>, bool> mp;
    int mm = m;
    for(int i = 1;i <= m; i++) {
        int u, v;
        cin>>u>>v;
        if(u > v) swap(u, v);
        if(mp[{u,v}]) {mm--; continue;}
        mp[{u,v}] = 1;
        vec[u].push_back(v);
        vec[v].push_back(u);
        d[u]++; d[v]++;
    }
    if(mm == n - 1) {
        cout<<"YES"<<endl;
        return;
    }
    if(mm > n) {
        cout<<"NO"<<endl;
        return;
    }
    topo();
    for(int i = 1;i <= n; i++) {
        if(vis[i] == 0) {
            if(nd.size() == 0) dfs(i, 0);
            getHash(i, 0);
        }
    }
    if(nd.size() % 2) {
        for(int i = 1; i < nd.size(); i++) {
            if(hs[nd[i]] != hs[nd[i - 1]]) {
                cout<<"NO"<<endl;
                return;
            }
        }
    }
    else {
        for(int i = 2; i < nd.size(); i++) {
            if(hs[nd[i]] != hs[nd[i - 2]]) {
                cout<<"NO"<<endl;
                return;
            }
        }
    }
    cout<<"YES"<<endl;
    return;
}
int main() {
    int T; cin>>T;
    while(T--) sl();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Memory Limit Exceeded

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

result: