QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#719289#2515. Graph Cardsyuto1115AC ✓15990ms121500kbC++202.8kb2024-11-07 00:01:572024-11-07 00:02:01

Judging History

This is the latest submission verdict.

  • [2024-11-07 00:02:01]
  • Judged
  • Verdict: AC
  • Time: 15990ms
  • Memory: 121500kb
  • [2024-11-07 00:01:57]
  • Submitted

answer

#include<bits/stdc++.h>
using namespace std;
#define rep2(i,j,k) for(ll i = ll(j); i < ll(k); i++)
#define rep(i,k) rep2(i,0,k)
#define rrep2(i,j,k) for(ll i = ll(j)-1; i >= ll(k); i--)
#define rrep(i,k) rrep2(i,k,0)
#define all(a) a.begin(), a.end()
#define SZ(a) ll(a.size())
#define eb emplace_back
using ll = long long;
using P = pair<ll,ll>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vp = vector<P>;
using vvp = vector<vp>;
const ll inf = LLONG_MAX/4;
bool chmin(auto& a, auto& b) {return a > b ? a = b, 1 : 0; }
bool chmax(auto& a, auto& b) {return a < b ? a = b, 1 : 0; }

using u64 = uint64_t;
const u64 mod = inf;
u64 add(u64 a, u64 b) {
	a += b;
	if(a >= mod) a -= mod;
	return a;
}
u64 mul(u64 a, u64 b) {
	auto c = (__uint128_t) a * b;
	return add(c >> 61, c & mod);
}

mt19937_64 rng;
uniform_int_distribution<ll> dist(0, mod-1);

u64 hs[1000010];
u64 R;
u64 pw[1000010];

void solve() {
	int n;
	cin >> n;
	set<tuple<int, int, u64>> st;
	rep(_, n) {
		int k;
		cin >> k;
		vvp G(k);
		rep(i, k) {
			int u, v;
			cin >> u >> v;
			--u, --v;
			G[u].eb(v, i);
			if(v != u) G[v].eb(u, i);
		}
		vector<bool> on_c(k);
		vl cycle;
		{
			vl ls;
			vector<bool> seen(k);
			auto dfs = [&](auto &dfs, int u, int p) -> void {
				ls.eb(u);
				seen[u] = true;
				for(auto [v, id] : G[u]) {
					if(id == p) continue;
					if(seen[v]) {
						rrep(i, SZ(ls)) {
							on_c[ls[i]] = true;
							cycle.eb(ls[i]);
							if(ls[i] == v) return;
						}
					} else {
						dfs(dfs, v, id);
						if(!cycle.empty()) return;
					}
				}
				ls.pop_back();
			};
			dfs(dfs, 0, -1);
		}
		//for(ll u : cycle) cerr << u << ' ';
		//cerr << endl;
		auto dfs = [&](auto &dfs, int u, int p) -> pair<int, u64> {
			u64 res = 1;
			vector<u64> ls;
			int d = 0;
			for(auto [v,_] : G[u]) {
				if(v == p or on_c[v]) continue;
				auto [nd, nv] = dfs(dfs, v, u);
				if(nd+1 > d) d = nd+1;
				ls.eb(nv);
			}
			for(ll l : ls) res = mul(res, add(hs[d], l));
			return {d, res};
		};
		vl v;
		for(ll u : cycle) {
			u64 now = dfs(dfs, u, -1).second;
			//cerr << now << ' ';
			v.eb(now);
		}
		//cerr << endl;
		int sz = SZ(v);
		u64 mn = mod;
		rep(_, 2) {
			vector<u64> sl(sz+1), sr(sz+1);
			rrep(i, sz) sr[i] = add(mul(sr[i+1], R), v[i]);
			rep(i, sz) sl[i+1] = add(sl[i], mul(v[i], pw[i]));
			rep(i, sz) {
				u64 now = sr[i];
				now = add(now, mul(sl[i], pw[sz-i]));
				chmin(mn, now);
				//cerr << now << endl;
			}
			reverse(all(v));
		}
		st.insert({k, sz, mn});
	}
	cout << st.size() << endl;
}

int main(){
	cin.tie(0)->sync_with_stdio(0);
	rep(i, 1000010) hs[i] = dist(rng);
	R = dist(rng);
	pw[0] = 1;
	rep2(i, 1, 1000010) pw[i] = mul(pw[i-1], R);
	int t;
	cin >> t;
	rep(_, t) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 11ms
memory: 19416kb

input:

1
2
4 1 2 2 3 3 1 1 4
4 1 2 2 3 3 1 2 4

output:

1

result:

ok single line: '1'

Test #2:

score: 0
Accepted
time: 5ms
memory: 19164kb

input:

2
2
4 1 2 2 3 3 1 1 4
5 1 2 2 3 3 1 2 4 2 5
3
9 1 2 2 5 5 7 7 6 6 3 3 1 2 4 7 9 9 8
9 1 4 4 2 2 3 3 5 5 7 7 6 6 4 7 8 8 9
9 1 2 2 5 5 4 4 1 4 7 7 8 8 6 8 9 5 3

output:

2
2

result:

ok 2 lines

Test #3:

score: 0
Accepted
time: 6748ms
memory: 36244kb

input:

30
332526
3 3 2 3 1 2 1
3 2 3 3 1 1 2
3 1 3 1 2 3 2
3 3 2 2 1 1 3
3 2 1 2 3 1 3
3 1 3 1 2 3 2
3 1 2 2 3 3 1
3 2 1 2 3 1 3
3 3 1 3 2 1 2
3 1 3 1 2 3 2
3 1 3 3 2 2 1
3 1 2 2 3 3 1
3 3 1 3 2 1 2
3 2 1 2 3 1 3
3 3 2 2 1 1 3
3 1 2 1 3 2 3
3 1 2 1 3 2 3
3 2 3 2 1 3 1
3 1 2 1 3 2 3
3 3 1 1 2 2 3
3 1 3 1 2 ...

output:

1
4
5
89
93
242
628
1575
3401
5703
7718
8888
9258
9355
9356
9203
9211
89
239
648
1739
4541
10352
19037
27089
31567
33030
33068
32480
31503

result:

ok 30 lines

Test #4:

score: 0
Accepted
time: 6068ms
memory: 22904kb

input:

30
333333
3 2 1 2 3 3 1
3 1 2 3 2 3 1
3 3 2 1 3 1 2
3 2 3 2 1 3 1
3 2 3 2 1 1 3
3 1 2 3 2 3 1
3 3 1 2 1 3 2
3 1 3 1 2 3 2
3 1 3 2 3 1 2
3 2 1 3 2 3 1
3 2 3 1 2 1 3
3 3 1 1 2 3 2
3 3 2 3 1 1 2
3 2 1 3 1 3 2
3 3 2 1 2 3 1
3 3 2 1 2 1 3
3 2 3 2 1 1 3
3 1 2 3 2 3 1
3 3 1 1 2 3 2
3 1 3 1 2 2 3
3 1 2 3 2 ...

output:

1
2
5
13
33
89
240
653
1771
4753
11868
24982
40524
51128
54690
54119
52143
49848
47551
45441
43471
41664
40000
38461
37037
35714
34482
33333
32258
31250

result:

ok 30 lines

Test #5:

score: 0
Accepted
time: 15990ms
memory: 121500kb

input:

30
2
500000 12446 494050 179161 216388 282442 232792 428247 130742 87062 493860 276791 469755 342468 58973 93535 429405 5662 112197 121541 62083 28611 427877 376918 349780 372239 58010 457751 308686 448844 473714 151350 480692 152372 415617 311966 298916 302690 200753 75481 396263 318635 79619 39930...

output:

1
2
2
1
1
2
2
1
1
2
2
1
1
2
2
1
1
2
2
1
1
2
2
1
1
2
2
1
1
2

result:

ok 30 lines

Test #6:

score: 0
Accepted
time: 201ms
memory: 63448kb

input:

1
4
240000 1 2 2 3 3 4 4 7 1 5 2 6 7 8 8 9 9 10 10 13 7 11 8 12 13 14 14 15 15 16 16 19 13 17 14 18 19 20 20 21 21 22 22 25 19 23 20 24 25 26 26 27 27 28 28 31 25 29 26 30 31 32 32 33 33 34 34 37 31 35 32 36 37 38 38 39 39 40 40 43 37 41 38 42 43 44 44 45 45 46 46 49 43 47 44 48 49 50 50 51 51 52 52...

output:

2

result:

ok single line: '2'