QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#286577 | #5160. Kebab Pizza | maslory21 | WA | 0ms | 3644kb | C++20 | 2.4kb | 2023-12-18 03:36:25 | 2023-12-18 03:36:26 |
Judging History
answer
#include <bits/stdc++.h>
#define all(x) begin(x), end(x)
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define sz(x) (int)(x).size()
#define endl '\n'
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define fi first
#define se second
#define pb push_back
#define vi vector<int>
#define vll vector<long long>
#define vec vector
#define pii pair<int, int>
#define pll pair<long long, long long>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
int k, n;
string ok = "possible";
string bad = "impossible";
bool cycle = false;
int components = 0;
void dfs(int cur, vector<vector<int>>& G, vector<int>& vis, int p){
vis[cur] = 1;
for(auto it : G[cur]){
if(!vis[it]){
dfs(it, G, vis, cur);
}
else if(vis[it] == 1 && it != p){
cycle = true;
}
}
vis[cur] = 2;
}
void solve(){
cin >> n >> k;
set<int> s[k + 1];
vector<pii> v(n + 1);
vector<int> counter(k + 1);
vector<vector<int>> G(k + 1);
for(int i =1; i<=n; ++i){
cin >> v[i].fi >> v[i].se;
s[v[i].fi].insert(v[i].se);
s[v[i].se].insert(v[i].fi);
}
for(int i = 1; i<=k; ++i){
if(s[i].size() > 1){
counter[i] = 1;
}
}
int a;
for(int i = 1; i<=k; ++i){
a = 0;
for(auto it : s[i]){
if(it == i){
continue;
}
G[i].push_back(it);
a += counter[it];
}
if(a > 2){
cout << bad << endl;
return;
}
}
vector<int> vis(k + 1);
for(int i = 1; i<=k; ++i){
if(!counter[i]){
if(!s[i].empty() && *(s[i].begin()) == i){
++components;
}
continue;
}
if(!vis[i]){
++components;
dfs(i, G, vis, -1);
}
}
if(cycle){
if(components==1){
cout << ok << endl;
}
else{
cout << bad << endl;
}
}
else{
cout << ok << endl;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
// std::random_device rd;
// std::mt19937 gen(rd());
// std::uniform_int_distribution<int> distribution(1, 4);
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3608kb
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: 3548kb
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: 0ms
memory: 3644kb
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: 0ms
memory: 3544kb
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: 3600kb
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: 3600kb
input:
5 4 1 1 1 4 2 2 2 4 3 4
output:
possible
result:
ok single line: 'possible'
Test #7:
score: 0
Accepted
time: 0ms
memory: 3604kb
input:
6 4 1 1 1 4 2 2 2 4 3 3 3 4
output:
impossible
result:
ok single line: 'impossible'
Test #8:
score: -100
Wrong Answer
time: 0ms
memory: 3608kb
input:
4 5 1 2 3 4 4 5 5 3
output:
possible
result:
wrong answer 1st lines differ - expected: 'impossible', found: 'possible'