QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#252258#5307. Subgraph IsomorphismricofxML 0ms0kbC++202.3kb2023-11-15 17:21:332023-11-15 17:21:33

Judging History

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

  • [2023-11-15 17:21:33]
  • 评测
  • 测评结果:ML
  • 用时:0ms
  • 内存:0kb
  • [2023-11-15 17:21:33]
  • 提交

answer

#include "bits/stdc++.h"
 
using namespace std;
using i64 = long long;
 
using u64 = unsigned long long;

const u64 mask = chrono::steady_clock::now().time_since_epoch().count();

u64 shift(u64 x) {
    x ^= mask;
    x ^= x << 13;
    x ^= x >> 7;
    x ^= x << 17;
    x ^= mask;
    return x;
}
 
void solve() {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> g(n + 1);
    vector<int> deg(n + 1);
    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
        deg[u]++;
        deg[v]++;
    }

    if (m == n - 1) {
        cout << "YES" << '\n';
        return;
    }

    if (m > n) {
        cout << "NO" << '\n';
        return;
    }

    queue<int> q;
    vector<bool> vis(n + 1);
    for (int i = 1; i <= n; i++) {
        if (deg[i] == 1) {
            q.push(i);
        }
    }

    int SIZE = n;
    while (!q.empty()) {
        int cur = q.front();
        q.pop();
        vis[cur] = 1;
        SIZE--;
        for (auto nex : g[cur]) {
            if (!vis[nex] && --deg[nex] == 1) {
                q.push(nex);
            }
        }
    }

    auto TreeHash = [&](int root) {
        function<u64(int, int)> dfs = [&](int cur, int pre) {
            u64 H = 1;
            for (auto &nex : g[cur]) {
                if (nex != pre) {
                    H += shift(dfs(nex, cur));
                }
            }
            return H;
        };
        return dfs(root, 0);
    };     

    vector<u64> h;
    int pre = 0;
    int cur = 0;
    for (int i = 1; i <= n; i++) {
        if (!vis[i]) {
            cur = i;
            break;
        }
    }

    for (int i = 0; i < SIZE; i++) {
        h.push_back(TreeHash(cur));
        int now = 0;
        for (auto nex : g[cur]) {
            if (!vis[nex] && nex != pre) {
                now = nex;
                break;
            }
        }
        pre = cur;
        cur = now;
    }

    for (int i = 0; i < SIZE; i++) {
        if (h[i] != h[(i + 2) % SIZE]) {
            cout << "NO" << '\n';
            return;
        }
    }
    cout << "YES" << '\n';
}
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}

詳細信息

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:


result: