QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#672698#5307. Subgraph Isomorphismji_114514WA 65ms12112kbC++203.6kb2024-10-24 18:11:402024-10-24 18:11:40

Judging History

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

  • [2024-10-24 18:11:40]
  • 评测
  • 测评结果:WA
  • 用时:65ms
  • 内存:12112kb
  • [2024-10-24 18:11:40]
  • 提交

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), s.clear();
        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[i] = 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 if (v.size() == 4) {
            int op = ha[hs[v[0]]];
            for (auto k : v)
            {
                if (ha[hs[k]] != op) {
                    cout << "NO\n";
                    return;
                }
                op ^= 1;
            }
            cout << "YES\n";
        }
        else cout << "NO\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: 12112kb

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: 0
Accepted
time: 46ms
memory: 11816kb

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
YES
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:

ok 33192 token(s): yes count is 58, no count is 33134

Test #3:

score: 0
Accepted
time: 65ms
memory: 11912kb

input:

40000
9 24
1 4
1 6
1 7
1 9
2 5
2 7
2 8
2 9
3 5
3 7
3 8
3 9
4 6
4 8
4 9
5 6
5 8
5 9
6 7
6 8
6 9
7 8
7 9
8 9
9 21
1 4
1 6
1 7
1 8
2 5
2 7
2 8
2 9
3 5
3 7
3 9
4 6
4 8
4 9
5 6
5 9
6 7
6 8
6 9
7 8
8 9
9 21
1 4
1 6
1 7
1 8
2 5
2 7
2 8
2 9
3 5
3 7
3 9
4 6
4 8
4 9
5 6
5 9
6 7
6 8
6 9
7 8
7 9
9 22
1 4
1 6
1 ...

output:

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

result:

ok 40000 token(s): yes count is 0, no count is 40000

Test #4:

score: 0
Accepted
time: 61ms
memory: 11824kb

input:

40000
9 16
1 4
1 6
1 8
1 9
2 5
2 7
2 8
2 9
3 6
3 7
4 7
5 8
6 9
7 8
7 9
8 9
9 16
1 4
1 6
1 8
1 9
2 5
2 7
2 8
2 9
3 6
3 7
4 7
5 8
5 9
7 8
7 9
8 9
9 16
1 4
1 6
1 8
1 9
2 5
2 7
2 8
2 9
3 6
3 7
4 7
5 8
5 9
6 9
7 8
8 9
9 16
1 4
1 6
1 8
1 9
2 5
2 7
2 8
2 9
3 6
3 7
4 7
5 8
5 9
6 9
7 8
7 9
9 17
1 4
1 6
1 8
1...

output:

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

result:

ok 40000 token(s): yes count is 0, no count is 40000

Test #5:

score: 0
Accepted
time: 54ms
memory: 11908kb

input:

40000
9 17
1 5
1 6
1 7
1 8
2 5
2 7
2 9
3 6
3 8
3 9
4 7
4 8
4 9
5 7
5 8
5 9
6 9
9 18
1 5
1 6
1 7
1 8
2 5
2 7
2 9
3 6
3 8
3 9
4 7
4 8
4 9
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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
...

result:

ok 40000 token(s): yes count is 0, no count is 40000

Test #7:

score: 0
Accepted
time: 51ms
memory: 11812kb

input:

40000
9 15
1 5
1 8
1 9
2 6
2 7
2 9
3 6
3 7
4 7
4 8
4 9
5 8
6 9
7 9
8 9
9 13
1 5
1 8
1 9
2 6
2 7
2 9
3 6
3 7
4 7
4 8
4 9
5 8
5 9
9 14
1 5
1 8
1 9
2 6
2 7
2 9
3 6
3 7
4 7
4 8
4 9
5 8
5 9
8 9
9 14
1 5
1 8
1 9
2 6
2 7
2 9
3 6
3 7
4 7
4 8
4 9
5 8
5 9
7 9
9 15
1 5
1 8
1 9
2 6
2 7
2 9
3 6
3 7
4 7
4 8
4 9
5...

output:

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

result:

ok 40000 token(s): yes count is 1, no count is 39999

Test #8:

score: -100
Wrong Answer
time: 48ms
memory: 11824kb

input:

40000
9 8
1 9
2 9
3 9
4 9
5 9
6 9
7 9
8 9
9 8
1 8
1 9
2 9
3 9
4 9
5 9
6 9
7 9
9 9
1 8
1 9
2 9
3 9
4 9
5 9
6 9
7 9
8 9
9 8
1 8
2 8
3 9
4 9
5 9
6 9
7 9
8 9
9 8
1 8
1 9
2 8
3 9
4 9
5 9
6 9
7 9
9 9
1 8
1 9
2 8
3 9
4 9
5 9
6 9
7 9
8 9
9 9
1 8
1 9
2 8
2 9
3 9
4 9
5 9
6 9
7 9
9 10
1 8
1 9
2 8
2 9
3 9
4 9
5...

output:

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

result:

wrong answer expected YES, found NO [673rd token]