QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#621005#5307. Subgraph IsomorphismHirroWA 65ms5724kbC++201.9kb2024-10-07 23:55:492024-10-07 23:55:50

Judging History

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

  • [2024-10-07 23:55:50]
  • 评测
  • 测评结果:WA
  • 用时:65ms
  • 内存:5724kb
  • [2024-10-07 23:55:49]
  • 提交

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);
		vector<int>c;
		for (int i = 1; i <= tc; i++) {
			dfs2(cl[i], 0);
			c.push_back(hsh[cl[i]]);
		}
		if (tc % 2) {
			for (int i = 1; i < c.size();i++)if (c[i] != c[i-1]) {
				puts("NO");
				return;
			}
		}
		else {
			for (int i = 2; i < c.size(); i++)if (c[i] != c[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: 5592kb

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: 65ms
memory: 5724kb

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]