QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#270945 | #5160. Kebab Pizza | rgnerdplayer | WA | 1ms | 3432kb | C++20 | 2.4kb | 2023-12-01 18:33:20 | 2023-12-01 18:33:21 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
struct DSU {
int n, cnt;
vector<int> fa, sz;
DSU(int n = 0) : n(n), cnt(n), fa(n), sz(n, 1) { iota(fa.begin(), fa.end(), 0); }
int find(int u) { return u == fa[u] ? u : fa[u] = find(fa[u]); }
bool join(int u, int v) {
u = find(u), v = find(v);
if (u != v) {
// if (sz[u] < sz[v]) { swap(u, v); }
sz[u] += sz[v];
fa[v] = u;
cnt--;
return true;
}
return false;
}
bool same(int u, int v) { return find(u) == find(v); }
int size(int u) { return sz[find(u)]; }
};
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
auto solve = [&]() {
int m, n;
cin >> m >> n;
vector<pair<int, int>> e;
while (m--) {
int u, v;
cin >> u >> v;
u--, v--;
e.push_back(minmax(u, v));
}
sort(e.begin(), e.end());
e.erase(unique(e.begin(), e.end()), e.end());
vector<vector<int>> adj(n);
for (auto [u, v] : e) {
adj[u].push_back(v);
if (u != v) {
adj[v].push_back(u);
}
}
for (int i = 0; i < n; i++) {
if (count_if(adj[i].begin(), adj[i].end(), [&](int u) { return u != i && int(adj[u].size()) > 1; }) >= 3) {
cout << "impossible\n";
return;
}
}
vector<int> vis(n);
int comps = 0, hasCycle = 0;
for (int i = 0; i < n; i++) {
if (adj[i].empty()) {
continue;
}
bool cycle = false;
auto dfs = [&](auto dfs, int u, int p) -> void {
vis[u] = true;
for (auto v : adj[u]) {
if (v == p) {
continue;
}
if (!vis[v]) {
dfs(dfs, v, u);
} else {
cycle = true;
}
}
};
dfs(dfs, i, -1);
comps++;
hasCycle += cycle;
}
cout << (comps == 1 || !hasCycle ? "possible" : "impossible") << '\n';
};
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3432kb
input:
7 6 2 2 3 6 1 1 1 5 4 5 6 6 6 5
output:
impossible
result:
wrong answer 1st lines differ - expected: 'possible', found: 'impossible'