QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#201646 | #5160. Kebab Pizza | PhantomThreshold# | WA | 1ms | 3548kb | C++20 | 2.6kb | 2023-10-05 15:48:13 | 2023-10-05 15:48:13 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
void yes(){cout<<"possible"<<endl;exit(0);};
void no(){cout<<"impossible"<<endl;exit(0);};
int main()
{
ios_base::sync_with_stdio(false);
int n,k;
cin>>n>>k;
set<pair<int,int>> st;
vector<vector<int>> G(k+5);
vector<int> sp(k+5);
for(int i=1;i<=n;i++)
{
int u,v;
cin>>u>>v;
if(u>v)swap(u,v);
if(st.contains({u,v}))continue;
st.emplace(u,v);
if(u==v)sp[u]=1;
else
{
G[u].push_back(v);
G[v].push_back(u);
}
}
vector<int> conn;
vector<int> cyc;
vector<int> vis(k+5),pa(k+5);
function<void(int)> dfs1=[&](int u)
{
vis[u]=1;
for(auto v:G[u])
{
if(pa[u]==v)continue;
if(not vis[v])
{
pa[v]=u;
dfs1(v);
}
else if(cyc.empty())
{
int t=u;
cyc.push_back(u);
while(t!=v)
{
t=pa[t];
cyc.push_back(t);
};
}
}
vis[u]=2;
};
for(int i=1;i<=k;i++)
{
if(not vis[i])
{
if(G[i].size() or sp[i])
{
conn.push_back(i);
dfs1(i);
}
else no();
}
}
if(conn.size()>1u and cyc.size())no();
fill(vis.begin(),vis.end(),0);
if(cyc.size())
{
for(auto z:cyc)vis[z]=1;
for(int i=1;i<=k;i++)
{
if(vis[i])continue;
if(sp[i])no();
if(G[i].size())
{
int ok=0;
for(auto v:G[i])
{
if(vis[v])
ok=1;
}
if(not ok)no();
}
}
yes();
}
else
{
vector<int> dep(k+5);
vector<int> z(k+5);
for(auto r:conn)
{
vector<int> inb;
function<void(int)> dfs2=[&](int u)
{
inb.push_back(u);
vis[u]=1;
for(auto v:G[u])
{
if(not vis[v])
{
dep[v]=dep[u]+1;
dfs2(v);
}
}
};
dep[r]=1;
dfs2(r);
int nr=0;
for(auto u:inb)
{
if(dep[u]>dep[nr] or (dep[u]==dep[nr] and sp[u]==1))
nr=u;
}
r=nr;
for(auto u:inb)dep[u]=vis[u]=pa[u]=0;
function<void(int)> dfs3=[&](int u)
{
inb.push_back(u);
vis[u]=1;
for(auto v:G[u])
{
if(not vis[v])
{
dep[v]=dep[u]+1;
pa[v]=u;
dfs3(v);
}
}
};
dep[r]=1;
dfs3(r);
nr=0;
for(auto u:inb)
{
if(dep[u]>dep[nr] or (dep[u]==dep[nr] and sp[u]==1))
nr=u;
}
for(auto u:inb)vis[u]=0;
int t=nr;
vis[t]=1;
while(t!=r)
{
t=pa[t];
vis[t]=1;
}
for(auto u:inb)
{
if(vis[u])continue;
if(sp[u])no();
int ok=0;
for(auto v:G[u])
{
if(vis[v])
{
ok=1;
}
}
if(not ok)no();
}
}
yes();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3540kb
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: 1ms
memory: 3480kb
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: 3440kb
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: 3448kb
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: 3540kb
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: 3484kb
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: 3512kb
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: 0
Accepted
time: 0ms
memory: 3420kb
input:
4 5 1 2 3 4 4 5 5 3
output:
impossible
result:
ok single line: 'impossible'
Test #9:
score: 0
Accepted
time: 0ms
memory: 3548kb
input:
4 4 1 1 2 3 3 4 4 2
output:
impossible
result:
ok single line: 'impossible'
Test #10:
score: -100
Wrong Answer
time: 0ms
memory: 3508kb
input:
3 4 1 2 2 3 3 1
output:
impossible
result:
wrong answer 1st lines differ - expected: 'possible', found: 'impossible'