QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#620407#5307. Subgraph IsomorphismHirroWA 11ms5696kbC++201.7kb2024-10-07 18:00:262024-10-07 18:00:27

Judging History

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

  • [2024-10-07 18:00:27]
  • 评测
  • 测评结果:WA
  • 用时:11ms
  • 内存:5696kb
  • [2024-10-07 18:00:26]
  • 提交

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];
vector<int> E[maxn], V[maxn];
int dfs1(int x, int fa) {
	if (vis[x]) {
		int now = x;
		do {
			cir[now] = 1;
			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 (i>1&&hsh[i]!= hsh[i-1]) {
				puts("NO");
				return;
			}
		}
		puts("YES");
	}
}
signed main() {
	//freopen("1.txt","r",stdin);
	int T = 1;
	T = read();
	while (T--) solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5696kb

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: 5692kb

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]