QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#102661 | #5160. Kebab Pizza | w4p3r# | WA | 2ms | 3408kb | C++20 | 1.7kb | 2023-05-03 15:39:18 | 2023-05-03 15:39:20 |
Judging History
answer
#include<bits/stdc++.h>
#define N 100005
using namespace std;
int n,m;
int in[N];
struct node{
int x,y;
}Q[N],E[N];
inline bool cmp(node a,node b){
return a.x==b.x?a.y<b.y:a.x<b.x;
}
inline void Rd(int &res){
char c;res=0;
while(c=getchar(),c<48);
do res=(res<<3)+(res<<1)+(c^48);
while(c=getchar(),c>47);
return;
}
bool mark[N],vis[N];
inline bool add_edge(int a,int b){
if(E[a].x==-1)E[a].x=b;
else if(E[a].y==-1)E[a].y=b;
else return false;
return true;
}
int dfs(int x,int f){
if(x==-1)return 2;
if(vis[x])return 1;
vis[x]=1;
int nxt=Q[x].x;
if(Q[x].x==f)nxt=Q[x].y;
return dfs(nxt,x);
}
inline bool check(){
for(int i=1;i<=m;i++)E[i].x=-1,E[i].y=-1;
for(int i=1;i<=n;i++)if(Q[i].x==Q[i].y)mark[Q[i].x]=1;
for(int i=1;i<=n;i++){
if(Q[i].x==Q[i].y)continue;
if(!add_edge(Q[i].x,Q[i].y))return false;
if(!add_edge(Q[i].y,Q[i].x))return false;
}
int key=0;
for(int i=1;i<=n;i++)if(!vis[i]){
if(E[i].x==-1)continue;
if(key==1)return false;
int o=dfs(E[i].x,-2);
if(o==1){
if(key)return false;
key=1;
}
else{
key=2;
dfs(E[i].y,-2);
}
}
if(key==2)return true;
for(int i=1;i<=n;i++)if(mark[i]&&!vis[i])return false;
return true;
}
int main(){
Rd(n);Rd(m);
for(int i=1;i<=n;i++){
Rd(Q[i].x);Rd(Q[i].y);
if(Q[i].x>Q[i].y)swap(Q[i].x,Q[i].y);
}
sort(Q+1,Q+n+1,cmp);
int h=1;
for(int i=2;i<=n;i++)if(Q[i].x!=Q[h].x||Q[i].y!=Q[h].y)Q[++h]=Q[i];
n=h;
for(int i=1;i<=n;i++)in[Q[i].x]++,in[Q[i].y]++;
h=0;
for(int i=1;i<=n;i++)if(in[Q[i].x]>1&&in[Q[i].y]>1&&Q[i].x!=Q[i].y)Q[++h]=Q[i];
n=h;
if(check())puts("possible");
else puts("impossible");
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3408kb
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: 3280kb
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: 3288kb
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: 3340kb
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: 2ms
memory: 3288kb
input:
4 4 1 2 2 1 3 4 4 3
output:
possible
result:
ok single line: 'possible'
Test #6:
score: 0
Accepted
time: 2ms
memory: 3284kb
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: 2ms
memory: 3344kb
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: 3392kb
input:
4 5 1 2 3 4 4 5 5 3
output:
possible
result:
wrong answer 1st lines differ - expected: 'impossible', found: 'possible'