QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#621007#5307. Subgraph IsomorphismHirroWA 61ms5768kbC++202.1kb2024-10-07 23:59:502024-10-07 23:59:52

Judging History

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

  • [2024-10-07 23:59:52]
  • 评测
  • 测评结果:WA
  • 用时:61ms
  • 内存:5768kb
  • [2024-10-07 23:59:50]
  • 提交

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;
		}
	}
}
int CASE = 0;
void solve() {
	CASE++;
	
	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;
	vector<pair<int,int>> h(m + 1);
	for (int i = 1; i <= m; i++) {
		int x = read(), y = read();
		E[x].push_back(y);
		E[y].push_back(x);
		h[i] = { x,y };
	}
	if (CASE == 39) {
		for (int i = 1; i <= m; i++)cout << h[i].first << ' ' << h[i].second << ' ';
		cout << '\n';
	}
	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: 5768kb

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: 61ms
memory: 5588kb

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
1 5 1 6 2 5 2 6 3 5 4 6 
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
...

result:

wrong output format YES or NO expected, but 1 found [39th token]