提交记录 7151


用户 题目 状态 得分 用时 内存 语言 代码长度
Snakes noip18d. 【NOIP2018】旅行 Accepted 100 1.876 ms 692 KB C++11 3.24 KB
提交时间 评测时间
2018-12-29 10:27:13 2020-08-01 01:00:46
#include <cstdio>
#include <algorithm>
using namespace std;
const int N=5005;
const int M=10005;
int n,m;
int u,v;
int h[N],nx[M],to[M],cnt;
int dfn[N],low[N],idx;
int s[N],t;
bool vis[N];
int loop[N],lcnt;
bool inloop[N],pass[N];
int f[N],scnt,fir,lim,cut;
int mv[N];
bool forbid[M];
int matu[M],matv[M],matp[M],matc;
int aeh[N],aenx[M],aeto[M],aecnt;
bool cmp(int u,int v)
{
	return matv[u]>matv[v];
}
void aeforl(int u,int v)
{
	aecnt++;
	aeto[aecnt]=v;
	aenx[aecnt]=aeh[u];
	aeh[u]=aecnt;
}
void addedge(int u,int v)
{
	cnt++;
	to[cnt]=v;
	nx[cnt]=h[u];
	h[u]=cnt;
}
void greedy(int u,int p)
{
	printf("%d ",u);
	for (int i=h[u];i;i=nx[i])
		if (to[i]!=p)
			greedy(to[i],u);
}
void findloop(int u,int las)
{
	idx++;
	dfn[u]=low[u]=idx;
	t++;
	s[t]=u;
	vis[u]=1;
	for (int i=aeh[u];i;i=aenx[i])
		if (i!=(las^1))
			if (!vis[aeto[i]])
			{
				findloop(aeto[i],i);
				if (low[aeto[i]]<low[u])
					low[u]=low[aeto[i]];
			}
			else
				if (dfn[aeto[i]]<low[u])
					low[u]=dfn[aeto[i]];
	if (dfn[u]==low[u])
	{
		if (s[t]==u)
			t--;
		else
		{
			while (s[t]!=u)
			{
				lcnt++;
				loop[lcnt]=s[t];
				t--;
			}
			lcnt++;
			loop[lcnt]=u;
			t--;
		}
	}
}
void flag(int u,int p)
{
	if (u==1)
		pass[u]=1;
	for (int i=h[u];i;i=nx[i])
		if (to[i]!=p&&inloop[to[i]]==0)
		{
			flag(to[i],u);
			pass[u]|=pass[to[i]];
		}
}
void dfs(int u,int p)
{
	printf("%d ",u);
	for (int i=h[u];i;i=nx[i])
		if (to[i]!=p&&forbid[i]==0)
			dfs(to[i],u);
}
int main()
{
	scanf("%d%d",&n,&m);
	aecnt++;
	for (int i=1;i<=m;i++)
	{
		scanf("%d%d",&u,&v);
		aeforl(u,v);
		aeforl(v,u);
		matc++;
		matu[matc]=u;
		matv[matc]=v;
		matp[matc]=matc;
		matc++;
		matu[matc]=v;
		matv[matc]=u;
		matp[matc]=matc;
	}
	sort(matp+1,matp+matc+1,cmp);
	for (int i=1;i<=matc;i++)
		addedge(matu[matp[i]],matv[matp[i]]);
	if (n>m)
	{
		greedy(1,0);
		printf("\n");
	}
	else
	{
		findloop(1,0);
		for (int i=1;i<=lcnt;i++)
			inloop[loop[i]]=1;
		flag(loop[lcnt],0);
		for (int i=1;i<=lcnt;i++)
			for (int j=h[loop[i]];j;j=nx[j])
				if (pass[to[j]]==0&&inloop[to[j]]==0)
					if (to[j]>mv[loop[i]])
						mv[loop[i]]=to[j];
		scnt=0;
		for (int i=h[loop[lcnt]];i;i=nx[i])
			if (!pass[to[i]])
			{
				scnt++;
				f[scnt]=to[i];
			}
		sort(f+1,f+scnt+1);
		for (fir=1;fir<=scnt;fir++)
			if (inloop[f[fir]])
				break;
		lim=f[fir+1];
		if (f[fir]==loop[1])
		{
			for (cut=1;cut<lcnt;cut++)
			{
				if (mv[loop[cut]]>loop[cut+1])
				{
					lim=n+1;
					for (int i=h[loop[cut]];i;i=nx[i])
						if (to[i]>loop[cut+1]&&inloop[to[i]]==0)
							if (to[i]<lim)
								lim=to[i];
				}
				if (loop[cut+1]>lim||cut+1==lcnt)
					break;
			}
			for (int i=h[loop[cut]];i;i=nx[i])
				if (to[i]==loop[cut+1])
					forbid[i]=1;
			for (int i=h[loop[cut+1]];i;i=nx[i])
				if (to[i]==loop[cut])
					forbid[i]=1;
		}
		else
		{
			loop[0]=loop[lcnt];
			for (cut=lcnt-1;cut>0;cut--)
			{
				if (mv[loop[cut]]>loop[cut-1])
				{
					lim=n+1;
					for (int i=h[loop[cut]];i;i=nx[i])
						if (to[i]>loop[cut-1]&&inloop[to[i]]==0)
							if (to[i]<lim)
								lim=to[i];
				}
				if (loop[cut-1]>lim||cut-1==0)
					break;
			}
			for (int i=h[loop[cut]];i;i=nx[i])
				if (to[i]==loop[cut-1])
					forbid[i]=1;
			for (int i=h[loop[cut-1]];i;i=nx[i])
				if (to[i]==loop[cut])
					forbid[i]=1;
		}
		dfs(1,0);
		printf("\n");
	}
	return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #114.01 us52 KBAcceptedScore: 4

Testcase #218.18 us52 KBAcceptedScore: 4

Testcase #314.71 us52 KBAcceptedScore: 4

Testcase #442.66 us52 KBAcceptedScore: 4

Testcase #538.12 us52 KBAcceptedScore: 4

Testcase #6313.2 us144 KBAcceptedScore: 4

Testcase #7313.93 us144 KBAcceptedScore: 4

Testcase #8314.49 us148 KBAcceptedScore: 4

Testcase #9315.86 us132 KBAcceptedScore: 4

Testcase #10314.69 us128 KBAcceptedScore: 4

Testcase #111.704 ms456 KBAcceptedScore: 4

Testcase #121.715 ms492 KBAcceptedScore: 4

Testcase #131.693 ms504 KBAcceptedScore: 4

Testcase #141.708 ms452 KBAcceptedScore: 4

Testcase #151.702 ms532 KBAcceptedScore: 4

Testcase #1618.61 us92 KBAcceptedScore: 4

Testcase #1719.02 us92 KBAcceptedScore: 4

Testcase #1845.09 us100 KBAcceptedScore: 4

Testcase #1945.61 us96 KBAcceptedScore: 4

Testcase #20341.94 us220 KBAcceptedScore: 4

Testcase #21341 us220 KBAcceptedScore: 4

Testcase #22337.8 us224 KBAcceptedScore: 4

Testcase #231.853 ms692 KBAcceptedScore: 4

Testcase #241.859 ms672 KBAcceptedScore: 4

Testcase #251.876 ms656 KBAcceptedScore: 4


Judge Duck Online | 评测鸭在线
Server Time: 2026-04-08 22:13:03 | Loaded in 2 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠