QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#201653#5160. Kebab PizzaPhantomThreshold#WA 0ms3556kbC++202.6kb2023-10-05 15:51:462023-10-05 15:51:46

Judging History

你现在查看的是最新测评结果

  • [2023-10-05 15:51:46]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3556kb
  • [2023-10-05 15:51:46]
  • 提交

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);
				};
			}
			else no();
		}
		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);
			}
		}
	}
	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: 0ms
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: -100
Wrong Answer
time: 0ms
memory: 3556kb

input:

5 5
1 3
1 5
2 3
2 5
3 4

output:

impossible

result:

wrong answer 1st lines differ - expected: 'possible', found: 'impossible'