QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#710825#5307. Subgraph IsomorphismlonelywolfWA 38ms3756kbC++202.3kb2024-11-04 22:12:482024-11-04 22:12:48

Judging History

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

  • [2024-11-04 22:12:48]
  • 评测
  • 测评结果:WA
  • 用时:38ms
  • 内存:3756kb
  • [2024-11-04 22:12:48]
  • 提交

answer

#include <bits/stdc++.h>  
using namespace std;  

#define int long long  

using ull = unsigned long long;

const ull mask = mt19937_64(time(nullptr))();

ull shift(ull 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<pair<int, int>> e(m);
	for (auto &[x, y] : e) {
		cin >> x >> y;
	}

	if (n == m + 1 || n <= 4) {
		cout << "YES\n";
		return;
	}

	if (n < m) {
		cout << "NO\n";
		return;
	}

	vector<vector<int>> adj(n + 1);
	vector<int> deg(n + 1);
	for (auto [x, y] : e) {
		adj[x].push_back(y);
		adj[y].push_back(x);
		deg[x]++, deg[y]++;
	}

	vector<bool> cir(n + 1, true);
	queue<int> q;
	for (int i = 1; i <= n; i++) {
		if (deg[i] == 1) {
			cir[i] = false;
			q.push(i);
		}
	}

	while (!q.empty()) {
		int x = q.front();
		q.pop();
		for (auto y : adj[x]) {
			if (!cir[y]) {
				continue;
			}
			deg[y]--;
			if (deg[y] < 2) {
				cir[y] = false;
				q.push(y);
			}
		}
	}

	vector<vector<int>> a(n + 1);
	vector<vector<int>> b(n + 1);
	
	for (auto [x, y] : e) {
		if (cir[x] && cir[y]) {
			b[x].push_back(y), b[y].push_back(x);
			continue;
		}
		a[x].push_back(y), a[y].push_back(x);
	}

	vector<int> rt;
	vector<int> vis(n + 1);
	function<void(int, int)> dfs = [&](int x, int f) {
		vis[x] = true;
		rt.push_back(x);
		for (int y : b[x]) {
			if (y == f || vis[y]) {
				continue;
			}
			dfs(y, x);
		}
	};

	for (int i = 1; i <= n; i++) {
		if (cir[i]) {
			dfs(i, 0);
			break;
		}
	}

	function<ull(int, int)> get = [&](int x, int f) {
		ull ret = 1;
		for (auto y : a[x]) {
			if (y == f) {
				continue;
			}
			ret += shift(get(y, x));
		}
		return ret;
	};

	int sz = rt.size();
	vector<ull> hash(sz);
	for (int i = 0; i < sz; i++) {
		hash[i] = get(rt[i], 0);
	}

	if (hash == vector<ull>(sz, hash[0])) {
		cout << "YES\n";
		return;
	}

	if (sz % 2 == 0) {
		bool ok = true;
		for (int i = 2; i < sz; i++) {
			if (hash[i] != hash[i - 2]) {
				ok = false;
			}
		}
		cout << (ok ? "YES" : "NO") << "\n";
		return;
	}

	cout << "NO\n";
}

signed main() {  
    ios::sync_with_stdio(false);
    cin.tie(nullptr);  

    int t;
    cin >> t;
    while (t--) {
    	solve();
    }

    return 0;
}  
  

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3756kb

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: 38ms
memory: 3712kb

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
YES
YES
YES
YES
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
YES
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
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO...

result:

wrong answer expected NO, found YES [6th token]