QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#71319 | #5160. Kebab Pizza | Greenscreen23 | WA | 2ms | 3420kb | C++17 | 2.8kb | 2023-01-09 17:25:29 | 2023-01-09 17:25:32 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
#define rep(i, a, b) for(ll i = (a); i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (ll)(x).size()
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
ll n, k; cin >> n >> k;
vector<unordered_set<ll>> adj(k);
vector<bool> required(k, false);
rep(i, 0, n) {
ll a, b; cin >> a >> b; a--; b--;
if (a == b) {
required[a] = true;
continue;
}
adj[a].insert(b);
adj[b].insert(a);
}
ll nodeWithEdge = -1;
rep(v, 0, k) {
if (sz(adj[v]) == 1) {
adj[*adj[v].begin()].erase(v);
adj[v].clear();
}
if (sz(adj[v]) > 1) {
nodeWithEdge = v;
}
}
if (nodeWithEdge == -1) {
cout << "possible" << endl;
return 0;
}
bool tryCircle = true;
rep(i, 0, k) {
if (sz(adj[i]) >= 3) {
cout << "impossible" << endl;
return 0;
}
if (required[i] && sz(adj[i]) == 0) {
tryCircle = false;
}
}
vector<bool> used(k, false);
bool hasCircle = true;
if (tryCircle) {
// check connected
queue<ll> todo;
todo.push(nodeWithEdge);
used[nodeWithEdge] = true;
while(!todo.empty()) {
ll v = todo.front();
todo.pop();
for(auto u : adj[v]) {
if (used[u]) continue;
todo.push(u);
used[u] = true;
}
}
rep(i, 0, k) {
if (sz(adj[i]) == 1) hasCircle = false;
if (sz(adj[i]) == 2 && !used[i]) hasCircle = false;
}
if (hasCircle) {
cout << "possible" << endl;
return 0;
}
}
ll countDeg2 = 0;
vector<ll> deg1Nodes;
deg1Nodes.reserve(k);
rep(i, 0, k) {
if (sz(adj[i]) == 1) {
deg1Nodes.push_back(i);
}
if (sz(adj[i]) == 2) countDeg2++;
}
if (countDeg2 == 0) {
cout << "possible" << endl;
return 0;
}
used.assign(k, false);
for(auto deg1Node : deg1Nodes) {
if (used[deg1Node]) continue;
ll currNode = deg1Node;
ll prevNode = -1;
do {
for(auto next : adj[currNode]) {
if (next == prevNode) continue;
prevNode = currNode;
currNode = next;
countDeg2--;
}
} while(sz(adj[currNode]) == 2);
countDeg2++;
if (countDeg2 == 0) {
cout << "possible" << endl;
return 0;
}
used[currNode] = true;
}
cout << "impossible" << endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3348kb
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: 2ms
memory: 3380kb
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: 2ms
memory: 3324kb
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: 2ms
memory: 3344kb
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: 1ms
memory: 3396kb
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: 3328kb
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: 2ms
memory: 3420kb
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'