QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#622812#5307. Subgraph Isomorphismarthur_9548Compile Error//C++203.0kb2024-10-09 06:21:352024-10-09 06:21:36

Judging History

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

  • [2024-10-09 06:21:36]
  • 评测
  • [2024-10-09 06:21:35]
  • 提交

answer

#include <bits/stdc++.h>

#define ll 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 = 60;
constexpr ll MOD1 = 21412641264812894;
constexpr ll MOD2 = 17841234164112412;

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;
	}
};

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

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

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


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

		ll po=1;
		ll ans=72412687704886ll % MOD1;
		for (ll x : sons) {
			ans += (po * x) % MOD1;	
			ans %= MOD1;
			po *= M;
			po %= MOD1;
		}
		return ans;
	};
	auto hash2=[&](auto self, vector<vi>& g, int a, int p=-1) -> ll{
		vector<ll> sons;	
		for (auto b : g[a]) if (b != p) {
			sons.pb(self(self, g, b, a));
		}
		sort(all(sons));

		ll po=1;
		ll ans=1241917931328ll % MOD2;
		for (ll x : sons) {
			ans += (po * x) % MOD2;	
			ans %= MOD2;
			po *= M;
			po %= MOD2;
		}
		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);	
		}

		vector<ll> ans;
		for (auto x : ctrds) ans.eb(hash(hash, g, x)^(hash2(hash2,g,x)));
		return ans;
	};

	auto is_eq = [&](vector<ll> a, vector<ll> 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);

	auto hashs = test(tree);
	rep(_, 1, mxI) {
		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();
}

Details

answer.code:17:20: warning: overflow in conversion from ‘long int’ to ‘int’ changes value from ‘4557430888798830399’ to ‘1061109567’ [-Woverflow]
   17 | constexpr int oo = 0x3f3f3f3f3f3f3f3f;
      |                    ^~~~~~~~~~~~~~~~~~
answer.code: In function ‘void solve()’:
answer.code:149:25: error: ‘rnum’ was not declared in this scope; did you mean ‘enum’?
  149 |                 dfstree(rnum(rng), tree);
      |                         ^~~~
      |                         enum