QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#672686#5307. Subgraph Isomorphismji_114514WA 48ms12092kbC++203.6kb2024-10-24 18:07:562024-10-24 18:07:57

Judging History

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

  • [2024-10-24 18:07:57]
  • 评测
  • 测评结果:WA
  • 用时:48ms
  • 内存:12092kb
  • [2024-10-24 18:07:56]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;

using ull = unsigned long long;
constexpr int N = 1e6 + 10;
mt19937_64 rng(time(0));
ull rad = rng();
ull shift(ull x) {
    x ^= rad;
    x ^= x << 13;
    x ^= x >> 7;
    x ^= x << 17;
    x ^= rad;
    return x;
}
ull hs[N];
int sz[N], cnte[N], col[N];
vector<int>ver[N];
bool st[N];
void dfs1(int u, int fa) {
    hs[u] = 1, st[u] = 1;
    for (auto v : ver[u]) {
        if (st[v]) continue;
        dfs1(v, u);
        hs[u] += shift(hs[v]);
    }
}

struct EDCC {
    int n, now, cnt;

    vector<int>dfn, low, s;

    EDCC(int n) : n(n), low(n + 1) {
        for (int i = 1; i <= n; i++)ver[i].clear(), st[i] = 0, hs[i] = 0, col[i] = -1;
        dfn.resize(n + 1, -1);
        now = cnt = 0;
    }

    void add(int x, int y)
    {
        ver[x].push_back(y);
        ver[y].push_back(x);
    }

    void tarjan(int x, int fa)
    {
        dfn[x] = low[x] = now++;
        s.push_back(x);
        for (auto y : ver[x])
        {
            if (y == fa)continue;
            if (dfn[y] == -1) {
                tarjan(y, x);
                low[x] = min(low[x], low[y]);
            }
            else if (col[y] == -1 && dfn[y] < dfn[x])
            {
                low[x] = min(low[x], dfn[y]);
            }
        }
        if (dfn[x] == low[x])
        {
            int pre;
            cnt++;
            do {
                pre = s.back();
                col[pre] = cnt;
                s.pop_back();
            } while (pre != x);
        }
    }

    int work()
    {
        tarjan(1, 0);
        for (int i = 1; i <= cnt; i++)sz[cnt] = 0, cnte[i] = 0;
        for (int i = 1; i <= n; i++)
        {
            sz[col[i]]++;
            for (auto j : ver[i])
            {
                int x = col[i], y = col[j];
                if (x == y) {
                    cnte[x]++;
                }
            }
        }
        return cnt;
    }
};

vector<int>v;

void dfs(int u, int t)
{
    v.push_back(u), st[u] = 1;
    for (auto v : ver[u])
    {
        if (col[v] == t && !st[v])
        {
            dfs(v, t);
        }
    }
}

void solve() {
    int n, m; cin >> n >> m;
    EDCC e(n);
    while (m--) {
        int u, v; cin >> u >> v;
        e.add(u, v);
    }
    m = e.work();
    int j = 0;
    for (int i = 1; i <= m; i++)
    {
        if (sz[i] > 1) {
            if (j || cnte[i] / 2 != sz[i])
            {
                cout << "NO\n";
                return;
            }
            else j = i;
        }
    }
    if (!j)cout << "YES\n";
    else {

        v.clear();
        int u;
        for (int i = 1; i <= n; i++)
        {
            if (col[i] == j) {
                u = i;
                break;
            }
        }
        dfs(u, j);
        map<ull, int>ha; int tot = 0;
        for (auto k : v) {
            dfs1(k, 0);
            if (!ha.count(hs[k])) {
                ha[hs[k]] = tot++;
            }
        }
        if (tot > 2)cout << "NO\n";
        else if (tot == 1)
        {
            cout << "YES\n";
        }
        else {
            int op = ha[hs[v[0]]];
            for (auto k : v)
            {
                if (ha[hs[k]] != op) {
                    cout << "NO\n";
                    return;
                }
                op ^= 1;
            }
            cout << "YES\n";
        }
    }
}


int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t = 1; cin >> t;
    while (t--)solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 12092kb

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: 48ms
memory: 11880kb

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

result:

wrong answer expected YES, found NO [2nd token]