QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#621000 | #5307. Subgraph Isomorphism | Hirro | WA | 11ms | 5752kb | C++20 | 1.9kb | 2024-10-07 23:51:30 | 2024-10-07 23:51:30 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read() {
int x = 0, f = 1; char c = getchar();
while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); }
while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
return x * f;
}
const int maxn = 1e5 + 20;
const int mod = 998244353;
int n, m;
int l[maxn],hsh[maxn];
bool vis[maxn], cir[maxn];
int cl[maxn], tc = 0;
vector<int> E[maxn], V[maxn];
int dfs1(int x, int fa) {
if (vis[x]) {
int now = x;
do {
cir[now] = 1;
cl[++tc] = now;
now = l[now];
} while (now != x);
return 1;
}
vis[x] = 1;
for (int y : E[x]) {
if (y == fa) continue;
l[x] = y;
if(dfs1(y, x))return 1;
}
return 0;
}
bool cmp(int a, int b) {
return hsh[a] < hsh[b];
}
void dfs2(int x, int fa) {
hsh[x] = 0;
for (int y : E[x]) {
if (y == fa|| cir[y]) continue;
V[x].push_back(y);
dfs2(y, x);
}
if (V[x].size() == 0) hsh[x] = 1;
else {
sort(V[x].begin(), V[x].end(), cmp);
hsh[x] = 1908537;
for (int y : V[x]) {
hsh[x] = ((hsh[x] * 91107) ^ hsh[y]) % mod;
}
}
}
void solve() {
n = read(); m = read();
for (int i = 1; i <= n; i++) E[i].clear(), V[i].clear();
for (int i = 1; i <= n; i++) l[i] =vis[i] = cir[i] = hsh[i]=0;
for (int i = 1; i <= m; i++) {
int x = read(), y = read();
E[x].push_back(y);
E[y].push_back(x);
}
if (m + 1 == n) puts("YES");
else if (m >= n + 1) puts("NO");
else {
dfs1(1, 0);
for (int i = 1; i <= n; i++) {
if (!cir[i]) continue;
dfs2(i, 0);
}
if (tc % 2) {
for (int i = 2; i <= tc; i++)if (hsh[cl[i]] != hsh[cl[i-1]]) {
puts("NO");
return;
}
}
else {
for (int i = 3; i <= tc; i++)if (hsh[cl[i]] != hsh[cl[i - 2]]) {
puts("NO");
return;
}
}
puts("YES");
}
}
signed main() {
//freopen("1.txt","r",stdin);
int T = 1;
T = read();
while (T--) solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 5620kb
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: 11ms
memory: 5752kb
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 NO NO NO NO NO NO YES 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 YES 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 [39th token]