QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#630574 | #5307. Subgraph Isomorphism | 333zhan | WA | 55ms | 3860kb | C++20 | 2.5kb | 2024-10-11 19:19:11 | 2024-10-11 19:19:11 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
using namespace std;
using u64 = unsigned long long;
mt19937 rng (time (0));
u64 mask = rng ();
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>> e (n);
for (int i = 0; i < m; i ++) {
int x, y;
cin >> x >> y;
x --; y --;
e[x].push_back (y);
e[y].push_back (x);
}
if (m > n) {
cout << "NO\n";
return;
}
if (m < n) {
cout << "YES\n";
return;
}
vector <int> pre (n, -1);
vector <int> path;
vector <int> dfn (n);
int ts = 0;
auto dfs = [&] (auto &&self, int x, int fa) -> void {
dfn[x] = ++ ts;
for (auto y : e[x]) {
if (y == fa) {
continue;
}
if (! dfn[y]) {
pre[y] = x;
self (self, y, x);
} else {
if (dfn[y] < dfn[x]) {
continue;
}
int t = y;
path.push_back (t);
while (t != x) {
path.push_back (t);
t = pre[t];
}
}
}
};
dfs (dfs, 0, -1);
vector <int> ok (n);
for (auto x : path) {
ok[x] = true;
}
vector <u64> ans;
vector <u64> hash (n);
auto dfs2 = [&] (auto &&self, int x, int fa) -> void {
hash[x] = 1;
for (auto y : e[x]) {
if (y == fa) {
continue;
}
if (ok[y]) {
continue;
}
self (self, y, x);
hash[x] += shift (hash[y]);
}
};
for (auto x : path) {
dfs2 (dfs2, x, -1);
ans.push_back (hash[x]);
}
if (count (ans.begin (), ans.end (), ans[0]) == path.size ()) {
cout << "YES\n";
return;
}
if (path.size () % 2 == 1) {
cout << "NO\n";
return;
}
for (int i = 2; i < ans.size (); i ++) {
if (ans[i] != ans[i - 2]) {
cout << "NO\n";
return;
}
}
cout << "YES\n";
}
signed main () {
ios::sync_with_stdio (false);
cin.tie (nullptr);
int T = 1;
cin >> T;
while (T --) {
solve ();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3660kb
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: 55ms
memory: 3860kb
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 NO NO NO YES YES NO YES NO NO NO NO YES NO NO NO NO NO NO NO NO NO NO NO NO YES YES NO YES YES NO NO NO NO 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 NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO...
result:
wrong answer expected YES, found NO [7th token]