QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#270987 | #5160. Kebab Pizza | ckiseki | WA | 1ms | 3828kb | C++20 | 2.9kb | 2023-12-01 19:38:18 | 2023-12-01 19:38:18 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define all(x) begin(x), end(x)
#ifdef CKISEKI
#define safe cerr << __PRETTY_FUNCTION__ << " line " << __LINE__ << " safe\n"
#define debug(a...) debug_(#a, a)
#define orange(a...) orange_(#a, a)
#include <experimental/iterator>
void debug_(auto s, auto ...a) {
cerr << "\e[1;32m(" << s << ") = (";
int f = 0;
(..., (cerr << (f++ ? ", " : "") << a));
cerr << ")\e[0m\n";
}
void orange_(auto s, auto L, auto R) {
cerr << "\e[1;33m[ " << s << " ] = [ ";
using namespace experimental;
copy(L, R, make_ostream_joiner(cerr, ", "));
cerr << " ]\e[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) safe
#define orange(...) safe
#endif
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n, k;
cin >> n >> k;
vector<set<int>> st(k);
vector<int> self_loop;
vector<pair<int,int>> pairs(n);
for (auto &[a, b] : pairs) {
cin >> a >> b;
--a, --b;
if (a == b) {
self_loop.emplace_back(a);
continue;
}
st[a].emplace(b);
st[b].emplace(a);
}
vector<int> cover, covered(k);
for (int i = 0; i < k; i++) {
if (st[i].empty()) continue;
if (st[i].size() == 1) {
covered[i] = true;
cover.emplace_back(i);
}
}
vector<pair<int,int>> lonely_pair;
for (int i : cover) {
debug(i);
if (st[i].empty()) continue;
int j = *st[i].begin();
if (covered[j]) {
lonely_pair.emplace_back(i, j);
}
st[j].erase(i);
st[i].clear();
}
int tot_edge = 0;
for (int i = 0; i < k; i++) {
debug(st[i].size());
if (st[i].size() > 2) {
cout << "impossible\n";
return 0;
}
debug(i);
orange(all(st[i]));
tot_edge += st[i].size();
}
assert (tot_edge % 2 == 0);
tot_edge /= 2;
debug(tot_edge);
bool cycle = false;
int cycle_size = 0;
vector<int> vis(k), stk;
const auto dfs = [&](auto self, int i, int f) -> void {
stk.emplace_back(i);
++cycle_size;
vis[i] = true;
for (int j : st[i]) {
if (j == f) continue;
if (!vis[j]) {
self(self, j, i);
} else {
cycle = true;
}
}
return;
};
for (int i = 0; i < k; i++) {
if (!vis[i]) {
cycle = false;
cycle_size = 0;
stk.clear();
dfs(dfs, i, -1);
if (cycle) {
sort(all(stk));
for (int x : self_loop) {
if (!binary_search(all(stk), x)) {
safe;
cout << "impossible\n";
return 0;
}
}
if (lonely_pair.size() > 0) {
cout << "impossible\n";
return 0;
}
for (auto [x, y] : pairs) {
if (!binary_search(all(stk), x) && !binary_search(all(stk), y)) {
cout << "impossible\n";
return 0;
}
}
debug(cycle_size);
if (cycle_size != tot_edge) {
cout << "impossible\n";
return 0;
}
}
}
}
cout << "possible\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3592kb
input:
7 6 2 2 3 6 1 1 1 5 4 5 6 6 6 5
output:
possible
result:
ok single line: 'possible'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3588kb
input:
5 5 1 3 1 5 2 3 2 5 3 4
output:
possible
result:
ok single line: 'possible'
Test #3:
score: 0
Accepted
time: 1ms
memory: 3828kb
input:
6 7 1 2 2 3 3 4 4 5 3 6 6 7
output:
impossible
result:
ok single line: 'impossible'
Test #4:
score: 0
Accepted
time: 1ms
memory: 3540kb
input:
8 4 1 1 1 2 2 1 2 2 3 3 3 4 4 3 4 4
output:
possible
result:
ok single line: 'possible'
Test #5:
score: 0
Accepted
time: 0ms
memory: 3688kb
input:
4 4 1 2 2 1 3 4 4 3
output:
possible
result:
ok single line: 'possible'
Test #6:
score: 0
Accepted
time: 0ms
memory: 3648kb
input:
5 4 1 1 1 4 2 2 2 4 3 4
output:
possible
result:
ok single line: 'possible'
Test #7:
score: -100
Wrong Answer
time: 0ms
memory: 3652kb
input:
6 4 1 1 1 4 2 2 2 4 3 3 3 4
output:
possible
result:
wrong answer 1st lines differ - expected: 'impossible', found: 'possible'