QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#643101 | #9420. Find Yourself | ucup-team004 | WA | 271ms | 14264kb | C++23 | 5.6kb | 2024-10-15 18:50:41 | 2024-10-15 18:50:41 |
Judging History
answer
#include <bits/stdc++.h>
using i64 = long long;
using u64 = unsigned long long;
using u32 = unsigned;
int T;
int cs;
void solve() {
int n, m;
std::cin >> n >> m;
if (T == 415 && cs == 13) {
std::cout << n << " " << m << "\n";
}
std::vector<std::vector<int>> adj(n);
for (int i = 0; i < m; i++) {
int u, v;
std::cin >> u >> v;
u--;
v--;
if (T == 415 && cs == 13) {
std::cout << u << " " << v << "\n";
}
adj[u].push_back(v);
adj[v].push_back(u);
}
std::vector<bool> del(n);
std::vector<int> col(n, -1);
std::queue<int> q;
col[0] = 0;
q.push(0);
while (!q.empty()) {
int x = q.front();
q.pop();
for (auto y : adj[x]) {
if (col[x] == col[y]) {
std::cout << "NO\n";
return;
}
if (col[y] == -1) {
col[y] = col[x] ^ 1;
q.push(y);
}
}
}
std::vector<bool> f(n);
for (int c = 0; c < 2; c++) {
for (int x = 0; x < n; x++) {
if (del[x] || col[x] != c) {
continue;
}
for (auto y : adj[x]) {
if (!del[y] && adj[y].size() == 2) {
int z = adj[y][0] ^ adj[y][1] ^ x;
if (f[z]) {
del[y] = true;
adj[y].clear();
} else {
f[z] = true;
}
}
}
for (auto y : adj[x]) {
if (!del[y] && adj[y].size() == 2) {
int z = adj[y][0] ^ adj[y][1] ^ x;
f[z] = false;
}
}
}
for (int x = 0; x < n; x++) {
if (del[x] || col[x] != c) {
continue;
}
adj[x].erase(std::remove_if(adj[x].begin(), adj[x].end(),
[&](int y) {
return del[y];
}), adj[x].end());
}
}
int rt = 0;
while (del[rt]) {
rt++;
}
std::vector<std::vector<int>> cyc;
std::vector<bool> vis(n);
std::vector<int> p(n, -1), dep(n);
auto dfs = [&](auto &&self, int x) -> void {
vis[x] = true;
for (auto y : adj[x]) {
if (!vis[y]) {
dep[y] = dep[x] + 1;
p[y] = x;
self(self, y);
} else if (dep[y] > dep[x]) {
if (cyc.size() <= 2) {
std::vector<int> a;
for (int i = y; i != p[x]; i = p[i]) {
a.push_back(i);
}
cyc.push_back(a);
}
}
}
};
dfs(dfs, rt);
if (cyc.empty()) {
std::cout << "YES\n";
return;
}
for (auto &a : cyc) {
if (a.size() != 4) {
std::cout << "NO\n";
return;
}
}
std::vector<int> h(n);
for (int x = 0; x < n; x++) {
if (del[x]) {
continue;
}
if (adj[x].size() > 2) {
h[x] = 1;
}
int cnt = 0;
for (auto y : adj[x]) {
if (adj[y].size() >= 2) {
cnt++;
}
}
if (cnt > 2) {
h[x] = 2;
}
}
for (auto c : cyc) {
int c1 = 0;
int c2 = 0;
for (auto x : c) {
if (h[x] == 2) {
c2++;
}
if (h[x]) {
c1++;
}
}
if (c1 == 4 || c2 > 1) {
std::cout << "NO\n";
return;
}
}
std::cout << "YES\n";
}
void brute() {
int n, m;
std::cin >> n >> m;
std::vector<int> adj(n);
for (int i = 0; i < m; i++) {
int u, v;
std::cin >> u >> v;
u--;
v--;
adj[u] |= 1 << v;
adj[v] |= 1 << u;
}
std::vector<int> f(1 << n);
std::vector<bool> vis(1 << n);
std::queue<int> q;
for (int s = 1; s < (1 << n); s++) {
int i = __builtin_ctz(s);
f[s] = f[s ^ (1 << i)] | adj[i];
}
std::vector<std::vector<std::array<int, 2>>> e(1 << n);
for (int s = 0; s < (1 << n); s++) {
if (__builtin_popcount(s) <= 2) {
vis[s] = true;
q.push(s);
} else {
for (int t = (s - 1) & s; t; t = (t - 1) & s) {
int u = t;
int v = t ^ s;
if (__builtin_popcount(u) >= 2) {
u = f[u];
}
if (__builtin_popcount(v) >= 2) {
v = f[v];
}
e[u].push_back({v, s});
e[v].push_back({u, s});
}
}
}
while (!q.empty()) {
int x = q.front();
q.pop();
for (auto [y, z] : e[x]) {
if (vis[y] && !vis[z]) {
vis[z] = true;
q.push(z);
}
}
}
if (vis[(1 << n) - 1]) {
std::cout << "YES\n";
} else {
std::cout << "NO\n";
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int T;
std::cin >> T;
::T = T;
while (T--) {
cs++;
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3740kb
input:
3 3 3 1 2 2 3 3 1 4 4 1 2 2 3 3 4 4 1 6 6 1 2 2 3 3 4 4 5 5 6 6 1
output:
NO YES NO
result:
ok 3 token(s): yes count is 1, no count is 2
Test #2:
score: 0
Accepted
time: 0ms
memory: 3808kb
input:
10 8 8 1 2 2 3 3 4 4 1 1 5 5 6 2 7 7 8 8 8 1 2 2 3 3 4 4 1 1 5 2 6 3 7 4 8 9 9 1 2 2 3 3 4 4 1 1 5 5 6 3 7 7 8 2 9 7 8 1 3 1 4 1 5 2 3 2 4 2 5 3 6 4 7 8 8 1 2 2 3 3 4 4 1 1 5 5 6 3 7 7 8 8 8 1 2 2 3 3 4 4 1 1 5 5 6 2 7 3 8 8 8 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 1 9 10 1 3 1 4 1 5 2 3 2 4 2 5 1 6 2 7 3 8 ...
output:
NO NO NO NO YES YES NO YES YES YES
result:
ok 10 token(s): yes count is 5, no count is 5
Test #3:
score: 0
Accepted
time: 0ms
memory: 3560kb
input:
10 11 12 1 3 1 4 1 5 2 3 2 4 2 5 1 6 6 7 2 8 2 9 3 10 3 11 11 12 1 2 2 3 3 4 3 5 3 6 3 7 5 8 6 8 7 8 7 9 8 10 10 11 11 12 1 2 2 3 3 4 3 5 3 6 3 7 5 8 6 8 7 8 8 9 8 10 10 11 8 12 1 3 2 3 1 4 2 4 1 5 2 5 1 6 2 6 1 7 2 7 1 8 2 8 6 7 1 2 1 3 3 4 4 2 1 5 5 6 6 2 12 12 1 2 2 3 3 4 4 1 1 5 1 6 2 7 2 8 3 9 ...
output:
YES NO YES YES NO YES NO YES YES YES
result:
ok 10 token(s): yes count is 7, no count is 3
Test #4:
score: 0
Accepted
time: 236ms
memory: 7840kb
input:
5518 8 8 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 1 9 9 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 1 8 8 1 2 2 3 3 4 4 1 1 5 5 6 3 7 7 8 8 8 1 2 2 3 3 4 4 1 1 5 5 6 2 7 3 8 8 8 1 2 2 3 3 4 4 1 1 5 5 6 2 7 4 8 3 3 1 2 2 3 3 1 6 6 1 2 2 3 3 4 4 5 5 6 6 1 8 8 1 2 2 3 3 4 4 1 1 5 5 6 2 7 7 8 8 8 1 2 2 3 3 4 4 1 1 5 2 6 3 7...
output:
NO NO YES YES YES NO NO NO NO NO NO NO YES YES YES NO NO NO NO NO YES NO YES YES YES YES NO NO NO NO YES NO YES NO NO YES YES YES YES NO NO YES YES YES YES NO YES NO YES YES YES NO NO NO YES YES NO YES NO YES NO NO YES YES YES YES YES YES NO YES NO NO NO NO NO YES YES NO YES NO YES NO NO NO NO YES Y...
result:
ok 5518 token(s): yes count is 3676, no count is 1842
Test #5:
score: 0
Accepted
time: 243ms
memory: 7520kb
input:
5518 8 8 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 1 9 9 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 1 8 8 1 2 2 3 3 4 4 1 1 5 5 6 3 7 7 8 8 8 1 2 2 3 3 4 4 1 1 5 5 6 2 7 3 8 8 8 1 2 2 3 3 4 4 1 1 5 5 6 2 7 4 8 3 3 1 2 2 3 3 1 6 6 1 2 2 3 3 4 4 5 5 6 6 1 8 8 1 2 2 3 3 4 4 1 1 5 5 6 2 7 7 8 8 8 1 2 2 3 3 4 4 1 1 5 2 6 3 7...
output:
NO NO YES YES YES NO NO NO NO NO YES YES NO YES YES YES YES YES YES NO YES NO YES YES NO NO YES YES YES NO NO YES YES NO NO YES YES NO YES YES NO YES YES YES NO NO NO YES NO NO NO NO YES NO NO YES YES NO YES YES YES NO NO NO NO YES YES NO YES YES YES YES YES YES YES NO YES YES NO YES NO YES NO NO NO...
result:
ok 5518 token(s): yes count is 3671, no count is 1847
Test #6:
score: -100
Wrong Answer
time: 271ms
memory: 14264kb
input:
415 8 8 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 1 9 9 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 1 8 8 1 2 2 3 3 4 4 1 1 5 5 6 3 7 7 8 8 8 1 2 2 3 3 4 4 1 1 5 5 6 2 7 3 8 8 8 1 2 2 3 3 4 4 1 1 5 5 6 2 7 4 8 3 3 1 2 2 3 3 1 6 6 1 2 2 3 3 4 4 5 5 6 6 1 8 8 1 2 2 3 3 4 4 1 1 5 5 6 2 7 7 8 8 8 1 2 2 3 3 4 4 1 1 5 2 6 3 7 ...
output:
NO NO YES YES YES NO NO NO NO NO YES YES 86811 99998 17291 86274 85379 75018 69708 18904 22852 3484 16422 42410 61855 83186 80447 59394 60526 64978 62272 21812 61878 82232 85455 69940 52954 14285 1228 43087 32734 66988 79893 35982 68829 30205 52518 12607 56190 48640 29532 33685 27619 64877 53286 356...
result:
wrong output format YES or NO expected, but 86811 found [13th token]