QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#874590 | #2515. Graph Cards | ucup-team5234 | AC ✓ | 16360ms | 127672kb | C++23 | 4.6kb | 2025-01-28 11:34:48 | 2025-01-28 11:34:48 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define pb(a) push_back(a)
#define rep(i, n) for(int i=0;i<n;i++)
#define foa(e, v) for(auto&& e : v)
using ll = long long;
const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);
#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;
struct RollingHash {
static const int base1 = 1007, base2 = 2009;
static const int mod1 = 1000000007, mod2 = 1000000009;
std::vector<long long> hash1, hash2, power1, power2;
// construct
RollingHash(const vector<int> &S) {
int n = (int)S.size();
hash1.assign(n+1, 0);
hash2.assign(n+1, 0);
power1.assign(n+1, 1);
power2.assign(n+1, 1);
for (int i = 0; i < n; ++i) {
hash1[i+1] = (hash1[i] * base1 + S[i]) % mod1;
hash2[i+1] = (hash2[i] * base2 + S[i]) % mod2;
power1[i+1] = (power1[i] * base1) % mod1;
power2[i+1] = (power2[i] * base2) % mod2;
}
}
// get hash of S[left:right)
inline std::pair<long long, long long> get(int l, int r) const {
long long res1 = hash1[r] - hash1[l] * power1[r-l] % mod1;
if (res1 < 0) res1 += mod1;
long long res2 = hash2[r] - hash2[l] * power2[r-l] % mod2;
if (res2 < 0) res2 += mod2;
return {res1, res2};
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int t; cin >> t;
map<vector<int>, int> mp;
int rt_id = 0;
rep(_, t) {
unordered_set<ll> st;
int cnt = 0;
int m; cin >> m;
rep(k, m) {
int n; cin >> n;
vector<vector<int>> v(n);
rep(i, n) {
int a, b;
cin >> a >> b;
a --; b --;
v[a].pb(b);
v[b].pb(a);
}
vector<int> heiro;
vector<bool> seen(n, 0);
int sta = -1;
auto dfs = [&](auto dfs, int now, int p) -> bool {
if(seen[now]) {
heiro.pb(now);
sta = now;
return true;
}
seen[now] = 1;
foa(e, v[now]) {
if(e == p) continue;
bool ret = dfs(dfs, e, now);
if(ret) {
if(sta == now) {
return false;
}
heiro.pb(now);
return true;
}
}
return false;
};
dfs(dfs, 0, -1);
int sz = heiro.size();
auto dfs1 = [&](auto dfs1, int now, int p) -> int {
vector<int> vec;
foa(e, v[now]) {
if(e == p) continue;
vec.pb(dfs1(dfs1, e, now));
}
sort(all(vec));
if(mp.count(vec)) {
return mp[vec];
}
mp[vec] = rt_id ++;
return mp[vec];
};
vector<int> nodes;
rep(i, sz) {
int v1 = heiro[(i + sz - 1) % sz];
int v2 = heiro[(i + 1) % sz];
vector<int> vec;
foa(e, v[heiro[i]]) {
if(v1 == e or v2 == e) continue;
vec.pb(dfs1(dfs1, e, heiro[i]));
}
sort(all(vec));
if(mp.count(vec)) {
nodes.pb(mp[vec]);
} else {
mp[vec] = rt_id ++;
nodes.pb(mp[vec]);
}
}
rep(i, sz) nodes.pb(nodes[i]);
RollingHash r(nodes);
bool flag = 0;
rep(i, sz) {
auto [r1, r2] = r.get(i, i + sz);
if(st.count((r1 << 30) | r2)) {
flag = 1;
break;
}
}
if(flag) continue;
reverse(all(nodes));
RollingHash re(nodes);
rep(i, sz) {
auto [r1, r2] = re.get(i, i + sz);
if(st.count((r1 << 30) | r2)) {
flag = 1;
break;
}
}
if(flag) continue;
auto [r1, r2] = re.get(0, sz);
st.insert((r1 << 30) | r2);
cnt ++;
}
cout << cnt << '\n';
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3712kb
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: 1ms
memory: 3584kb
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: 12452ms
memory: 125776kb
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: 9685ms
memory: 46732kb
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: 16360ms
memory: 127672kb
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: 188ms
memory: 58208kb
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'