QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#621803 | #5307. Subgraph Isomorphism | shift | RE | 0ms | 0kb | C++23 | 7.1kb | 2024-10-08 17:05:27 | 2024-10-08 17:05:28 |
answer
// #pragma once
// C
#include <cassert>
#include <cctype>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <csetjmp>
#include <cstdarg>
#include <cstddef>
#include <cstdlib>
#include <cstdint>
// C++
#include <bitset>
#include <complex>
#include <algorithm>
#include <bitset>
#include <functional>
#include <iterator>
#include <limits>
#include <memory>
#include <new>
#include <numeric>
#include <typeinfo>
#include <utility>
#include <array>
#include <atomic>
#include <initializer_list>
#include <ratio>
#include <scoped_allocator>
#include <tuple>
#include <typeindex>
#include <type_traits>
#if _HAS_CXX17
#include <any>
// #include <execution>
#include <optional>
#include <variant>
#include <string_view>
#endif
#if _HAS_CXX20
#include <bit>
#include <compare>
#include <concepts>
#include <numbers>
#include <ranges>
#include <span>
#include <source_location>
#include <version>
#endif
#if _HAS_CXX23
#include <expected>
#include <stdatomic.h>
#if __cpp_impl_coroutine
#include <coroutine>
#endif
#endif
// C
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cwchar>
#include <cwctype>
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
// #include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cuchar>
// C++
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <codecvt>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <shared_mutex>
#if _HAS_CXX17
#include <any>
#include <charconv>
// #include <execution>
#include <filesystem>
#include <optional>
#include <memory_resource>
#include <variant>
#endif
#if _HAS_CXX20
#include <barrier>
#include <bit>
#include <compare>
#include <concepts>
#include <format>
#include <latch>
#include <numbers>
#include <ranges>
#include <span>
#include <stop_token>
#include <semaphore>
#include <source_location>
#include <syncstream>
#include <version>
#endif
#if _HAS_CXX23
#include <expected>
#include <spanstream>
#if __has_include(<stacktrace>)
#include <stacktrace>
#endif
#include <stdatomic.h>
#include <stdfloat>
#endif
using i64 = long long;
using u64 = unsigned long long;
// https://github.com/Heltion/debug.h/blob/main/debug.h
template <class T, size_t size = std::tuple_size<T>::value> std::string to_debug(T, std::string s = "") requires(not std::ranges::range<T>);
std::string to_debug(auto x) requires requires(std::ostream& os) { os << x; } { return static_cast<std::ostringstream>(std::ostringstream() << x).str(); }
std::string to_debug(std::ranges::range auto x, std::string s = "") requires(not std::is_same_v<decltype(x), std::string>) {
for (auto xi : x) { s += ", " + to_debug(xi); }
return "[" + s.substr(s.empty() ? 0 : 2) + "]";
}
template <class T, size_t size> std::string to_debug(T x, std::string s) requires(not std::ranges::range<T>) {
[&]<size_t... I>(std::index_sequence<I...>) { ((s += ", " + to_debug(get<I>(x))), ...); }(std::make_index_sequence<size>());
return "(" + s.substr(s.empty() ? 0 : 2) + ")";
}
#define debug(...) std::cerr << __FILE__ ":" << __LINE__ << ": (" #__VA_ARGS__ ") = " << to_debug(std::tuple(__VA_ARGS__)) << "\n"
const u64 mask = std::mt19937_64(std::random_device{}())();
u64 shift(u64 x) {
x ^= mask;
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
x ^= mask;
return x;
}
struct DSU {
std::vector<int> p, siz;
DSU(int n) : p(n), siz(n, 1) { std::iota(p.begin(), p.end(), 0); }
int find(int x) {
return p[x] == x ? x : p[x] = find(p[x]);
}
bool same(int x, int y) { return find(x) == find(y); }
bool merge(int x, int y) {
x = find(x);
y = find(y);
if (x == y) return false;
siz[x] += siz[y];
p[y] = x;
return true;
}
int size(int x) { return siz[find(x)]; }
};
void solve() {
int n, m;
std::cin >> n >> m;
int cnt = 0;
DSU dsu(n);
std::vector<int> deg(n);
std::set<std::pair<int, int>> s;
for(int i = 0; i < m; i ++ ) {
int u, v;
std::cin >> u >> v;
u --, v --;
if(u > v) std::swap(u, v);
s.insert({u, v});
if(not s.count({u, v})) cnt += dsu.merge(u, v);
}
if(m == n - 1 and dsu.size(0) == n) {
std::cout << "YES" << '\n';
return;
}
if(cnt > 1) {
std::cout << "NO\n";
return;
}
std::vector<std::vector<int>> adj(n);
for(auto [x, y] : s) {
adj[x].push_back(y);
adj[y].push_back(x);
deg[x] += 1;
deg[y] += 1;
}
std::queue<int> q;
std::vector<bool> at(n, true);
std::vector<u64> hash(n);
for(int i = 0; i < n; i ++ ) {
if(deg[i] == 1) {
q.push(i);
hash[i] = 1;
}
}
while(not q.empty()) {
auto u = q.front(); q.pop();
at[u] = false;
for(auto v : adj[u]) {
deg[v] -= 1;
hash[v] += shift(hash[u]);
if(deg[v] == 1) q.push(v);
}
}
int place = std::find(at.begin(), at.end(), true) - at.begin();
std::vector<u64> R;
auto dfs = [&](auto &self, int u) -> void {
at[u] = false;
R.push_back(hash[u]);
for(auto v : adj[u]) {
if(at[v]) {
self(self, v);
}
}
};
dfs(dfs, place);
if(std::count(R.begin(), R.end(), *R.begin()) == R.size()) {
std::cout << "YES" << '\n';
return;
}
bool ok = (int(R.size()) % 2) == 0;
for(int i = 2; i < R.size(); i += 2) {
ok &= R[i] == R[i - 2];
}
for(int i = 3; i < R.size(); i += 2) {
ok &= R[i] == R[i - 2];
}
std::cout << (ok ? "YES" : "NO") << '\n';
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int T = 1;
std::cin >> T;
while(T -- ) {
solve();
}
return 0;
}
詳細信息
Test #1:
score: 0
Runtime Error
input:
4 7 6 1 2 2 3 3 4 4 5 5 6 3 7 3 3 1 2 2 3 3 1 5 5 1 2 2 3 3 4 4 1 1 5 1 0