QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#622775#5307. Subgraph Isomorphismarthur_9548WA 211ms3692kbC++202.7kb2024-10-09 05:13:172024-10-09 05:13:19

Judging History

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

  • [2024-10-09 05:13:19]
  • 评测
  • 测评结果:WA
  • 用时:211ms
  • 内存:3692kb
  • [2024-10-09 05:13:17]
  • 提交

answer

#include <bits/stdc++.h>

#define int long long
#define endl '\n'
#define pb push_back
#define eb emplace_back
#define rep(i,a,b) for(int i=(int)(a); i < (int)(b);i++)
#define all(x) (x).begin(), (x).end()
#define debug(x) cout << #x << ": " << x << endl
#define esp ' '
#define pii pair<int,int>
#define vi vector<int>
#define sz(x) ((int)(x).size())

using namespace std;

constexpr int oo = 0x3f3f3f3f3f3f3f3f;
constexpr int M = 23785125;
constexpr int mxI = 600;

struct DSU {
	vi p, rnk;
	DSU(int n) : p(n), rnk(n,0) {
		iota(all(p),0);
	}

	int find(int x) {
		return p[x] == x ? x : p[x]=find(p[x]);
	}

	bool merge(int a, int b) {
		a=find(a);
		b=find(b);
		if (a == b) return false;
		if (rnk[a] > rnk[b]) swap(a,b);

		rnk[b] += rnk[a]==rnk[b];
		p[a]=b;
		return true;
	}
};

void solve() {
	int n,m;cin>>n>>m;
	vector<pair<int,int>> edges(m);
	mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
	uniform_int_distribution<int> rnum(0, n-1);

	if (n == 1) {
		cout << "YES\n";
		return;
	}

	rep(i,0,m) {
		int a,b;cin>>a>>b;a--;b--;
		edges[i]={a,b};
	}


	auto hash=[&](auto self, vector<vi>& g, int a, int p=-1) -> int{
		vi sons;	
		for (auto b : g[a]) if (b != p) {
			sons.pb(self(self, g, b, a));
		}
		sort(all(sons));

		int po=M;
		int ans=1274126;
		for (auto x : sons) {
			ans += po * x;	
			po *= M;
		}
		return ans;
	};

	auto test=[&](vector<vi>& g) {
		vi szs(n,0), ps(n);
		auto dfs1=[&](auto self, int a, int p) -> void{
			ps[a] = p;
			szs[a] = 1;
			for (auto b : g[a]) if (b != p) {
				self(self, b, a);
				szs[a] += szs[b];
			}
		};
		dfs1(dfs1, 0,-1);

		auto ctrd=[&](int a) {
			if (2*(n - szs[a]) > n) return false;			
			for (auto b : g[a]) if (b != ps[a]) {
				if (2*szs[b] > n) return false;
			}
			return true;
		};
		vi ctrds;
		rep(a,0,n) {
			if (ctrd(a)) ctrds.pb(a);	
		}
		assert(1 <= sz(ctrds));
		assert(sz(ctrds) <= 2);

		vi ans;
		for (auto x : ctrds) ans.pb(hash(hash, g, x));
		return ans;
	};

	auto is_eq = [&](vi a, vi b) {
		if (sz(a) != sz(b)) return false;
		sort(all(a));
		sort(all(b));
		return a == b;
	};

	auto dfstree=[&](int a, vector<vi>& tree) {
		DSU dsu(n);
		tree.assign(n, vi());
		shuffle(all(edges), rng);

		for (auto [a,b] : edges) {
			if (!dsu.merge(a,b)) continue;
			tree[a].pb(b);
			tree[b].pb(a);
		}
	};
	
	vector<vi> tree(n);
	dfstree(0, tree);

//	debug("tree");
//	rep(a,0,n) {
//		for (auto b : g[a]) {
//			cout << a << ' ' << b << endl;
//		}
//	}
	
	auto hashs = test(tree);
	rep(_, 1, mxI) {
		//debug(_);
		dfstree(rnum(rng), tree);
		auto hashc = test(tree);
		if (!is_eq(hashc, hashs)) {
			cout << "NO\n";
			return;
		}
	}
	cout << "YES\n";
}

signed main() {
	ios_base::sync_with_stdio(0);cin.tie(0);

	int t=1;
	cin>>t;
	while(t--) solve();
}

詳細信息

Test #1:

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

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: 211ms
memory: 3676kb

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
YES
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
YES
YES
YES
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 [12th token]