0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-09-19 22:25:27 +00:00

#1850. 【Gym104090G】Subgraph Isomorphism

https://sjzezoj.com/submission/67557
This commit is contained in:
Baoshuo Ren 2023-01-29 10:24:53 +08:00
parent af0b89f5c8
commit 7bb1be51b5
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
2 changed files with 234 additions and 0 deletions

106
S2OJ/1818/1818.cpp Normal file
View File

@ -0,0 +1,106 @@
#include <iostream>
#include <queue>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 2e6 + 5;
const int INF = 0x3f3f3f3f;
int n, q, t, cnt = 1, pos = 1, lst,
a[N], dep[N], fa[N][std::__lg(N) + 1];
std::vector<int> g[N];
void bfs(int s) {
std::fill_n(dep, N, INF);
std::queue<int> q;
q.emplace(s);
dep[0] = 0;
dep[s] = 1;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v : g[u]) {
if (dep[v] > dep[u] + 1) {
dep[v] = dep[u] + 1;
q.emplace(v);
fa[v][0] = u;
for (int k = 1; k <= std::__lg(n); k++) {
fa[v][k] = fa[fa[v][k - 1]][k - 1];
}
}
}
}
}
int lca(int x, int y) {
if (dep[x] < dep[y]) std::swap(x, y);
for (int k = std::__lg(n); k >= 0; k--) {
if (dep[fa[x][k]] >= dep[y]) {
x = fa[x][k];
}
}
if (x == y) return x;
for (int k = std::__lg(n); k >= 0; k--) {
if (fa[x][k] != fa[y][k]) {
x = fa[x][k];
y = fa[y][k];
}
}
return fa[x][0];
}
constexpr int get(int x) {
return x * (x - 1) / 2;
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> q >> t;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
for (int j = i * (i - 1) / 2 + 1; j <= i * (i - 1) / 2 + i; j++) {
if (a[pos] == j) {
g[j].emplace_back(++cnt);
g[j].emplace_back(++cnt);
pos++;
} else {
g[j].emplace_back(++cnt);
}
}
}
bfs(1);
while (q--) {
int x, y;
cin >> x >> y;
x = (x - 1 + t * lst) % ((n + 1) * (n + 2) / 2) + 1;
y = (y - 1 + t * lst) % ((n + 1) * (n + 2) / 2) + 1;
cout << (lst = lca(x, y)) << endl;
}
return 0;
}

128
S2OJ/1850/1850.cpp Normal file
View File

@ -0,0 +1,128 @@
#include <iostream>
#include <functional>
#include <stack>
#include <unordered_set>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
// 树哈希
// https://peehs-moorhsum.blog.uoj.ac/blog/7891
unsigned long long h(unsigned long long x) {
return x * x * x * 1237123 + 19260817;
}
unsigned long long f(unsigned long long x) {
return h(x & ((1ull << 31) - 1)) + h(x >> 31);
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
std::vector<std::vector<int>> g(n + 1);
for (int i = 1, x, y; i <= m; i++) {
cin >> x >> y;
g[x].emplace_back(y);
g[y].emplace_back(x);
}
if (m > n) {
cout << "NO" << endl;
continue;
}
if (m < n) {
cout << "YES" << endl;
continue;
}
// assert(n == m);
std::stack<int> st;
std::vector<int> circle;
std::vector<bool> vis1(n + 1);
std::function<void(int, int)> dfs1 = [&](int u, int f) -> void {
if (vis1[u]) {
if (circle.empty()) {
while (!st.empty() && st.top() != u) {
circle.emplace_back(st.top());
st.pop();
}
circle.emplace_back(u);
}
return;
}
vis1[u] = true;
st.emplace(u);
for (int v : g[u]) {
if (v == f) continue;
dfs1(v, u);
}
if (!st.empty()) st.pop();
};
dfs1(1, 0);
std::vector<bool> vis2(n + 1);
std::vector<unsigned long long> hash(n + 1);
std::unordered_set<unsigned long long> set;
for (int x : circle) vis2[x] = true;
std::function<void(int)> dfs2 = [&](int u) -> void {
vis2[u] = true;
hash[u] = 1;
for (int v : g[u]) {
if (vis2[v]) continue;
dfs2(v);
hash[u] += f(hash[v]);
}
};
for (int x : circle) {
dfs2(x);
set.emplace(hash[x]);
}
bool flag = circle.size() % 2 == 0;
for (int i = 0; i + 2 < circle.size(); i++) {
if (hash[circle[i]] != hash[circle[i + 2]]) {
flag = false;
break;
}
}
cout << (set.size() == 1 || flag ? "YES" : "NO") << endl;
}
return 0;
}