From 7bb1be51b5e4c4367eb348cb46098f449c1a3185 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sun, 29 Jan 2023 10:24:53 +0800 Subject: [PATCH] =?UTF-8?q?#1850.=20=E3=80=90Gym104090G=E3=80=91Subgraph?= =?UTF-8?q?=20Isomorphism?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://sjzezoj.com/submission/67557 --- S2OJ/1818/1818.cpp | 106 +++++++++++++++++++++++++++++++++++++ S2OJ/1850/1850.cpp | 128 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 234 insertions(+) create mode 100644 S2OJ/1818/1818.cpp create mode 100644 S2OJ/1850/1850.cpp diff --git a/S2OJ/1818/1818.cpp b/S2OJ/1818/1818.cpp new file mode 100644 index 00000000..20a7fe2d --- /dev/null +++ b/S2OJ/1818/1818.cpp @@ -0,0 +1,106 @@ +#include +#include +#include + +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 g[N]; + +void bfs(int s) { + std::fill_n(dep, N, INF); + + std::queue 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; +} + diff --git a/S2OJ/1850/1850.cpp b/S2OJ/1850/1850.cpp new file mode 100644 index 00000000..c742fbbb --- /dev/null +++ b/S2OJ/1850/1850.cpp @@ -0,0 +1,128 @@ +#include +#include +#include +#include +#include + +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> 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 st; + std::vector circle; + std::vector vis1(n + 1); + + std::function 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 vis2(n + 1); + std::vector hash(n + 1); + std::unordered_set set; + + for (int x : circle) vis2[x] = true; + + std::function 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; +}