JZOJ-senior-5964. 【NOIP2018提高组D2T1】旅行

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/HuangXinyue1017/article/details/84200929

Time Limits: 1000 ms Memory Limits: 524288 KB

Description

小Y是一个爱好旅行的OIer。她来到X国,打算将各个城市都玩一遍。
小Y了解到,X国的n个城市之间有m条双向道路。每条双向道路连接两个城市。不存在两条连接同一对城市的道路,也不存在一条连接一个城市和它本身的道路。并且,从任意一个城市出发,通过这些道路都可以到达任意一个其他城市。小Y只能通过这些道路从一个城市前往另一个城市。
小Y的旅行方案是这样的:任意选定一个城市作为起点,然后从起点开始,每次可以选择一条与当前城市相连的道路,走向一个没有去过的城市,或者沿着第一次访问该城市时经过的道路后退到上一个城市。当小Y回到起点时,她可以选择结束这次旅行或继续旅行。需要注意的是,小Y要求在旅行方案中,每个城市都被访问到。
为了让自己的旅行更有意义,小Y决定在每到达一个新的城市(包括起点)时,将它的编号记录下来。她知道这样会形成一个长度为n的序列。她希望这个序列的字典序最小,你能帮帮她吗?
对于两个长度均为n的序列A和B,当且仅当存在一个正整数x,满足以下条件时,我们说序列A的字典序小于B。
①对于任意正整数1<=i<x,序列A的第i 个元素Ai 和序列B的第i 个元素Bi相同。
②序列A的第x个元素的值小于序列B的第x个元素的值。

Input

输入文件名为travel.in
输入文件共m+1行。第一行包含两个整数n,m(m<=n),中间用一个空格分隔。
接下来m行,每行包含两个整数u,v(1<=u,v<=n),表示编号为u和v的城市之间有一条道路,两个整数之间用一个空格分隔。

Output

输出文件名为travel.out。
输出文件包含一行,n个整数,表示字典序最小的序列。相邻两个整数之间用一个空格分隔。

Sample Input

输入1:
6 5
1 3
2 3
2 5
3 4
4 6

输入2:
6 6
1 3
2 3
2 5
3 4
4 5
4 6

Sample Output

输出1:
1 3 2 5 4 6

输出2:
1 3 2 4 5 6

Data Constraint

在这里插入图片描述

Solution

每经过一条新边,就会访问一个新的城市(起始节点除外),那么只有 n 1 n-1 条边是有用的
也就是说,经过的边将形成一棵树
m = n 1 m=n-1 ,哪个儿子编号小就走哪个,贪心一下即可
m = n m=n ,找出那个环,枚举不走的边是环上的哪一条,然后和上面一样贪心即可

Code

#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cctype>

#define fo(i,a,b) for(int i=a;i<=b;++i)
#define fd(i,a,b) for(int i=a;i>=b;--i)
#define P(c) putchar(c)

using namespace std;

const int N=5010;
int n,m,h,tp,no,eq,_1,_2,cnt,time;
int v[N],c[N],A[N],B[N];
int dfn[N],low[N],bz[N],in[N];
int b[N][N],p[N][N];
struct node{int x,number;}z[N];

inline void read(int &n)
{
	int x=0,w=0; char ch=0;
	while(!isdigit(ch)) w|=ch=='-',ch=getchar();
	while(isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
	n=w?-x:x;
}

inline void write(int x)
{
	if(x<0) x=-x,putchar('-');
	if(x>9) write(x/10);
	putchar(x%10+'0');
}

void dfs(int x)
{
	v[x]=1,write(x),P(' ');
	fo(i,1,c[x]) if(!v[b[x][i]])
	{
		v[b[x][i]]=1;
		dfs(b[x][i]);
	}
}

void get(int x)
{
	v[x]=1,B[++cnt]=x;
	if(eq&&A[cnt]!=B[cnt])
	{
		eq=0;
		if(B[cnt]<A[cnt]) _1=1,_2=0;
			else _1=0,_2=1;
		if(_2) return;
	}
	fo(i,1,c[x]) if(!v[b[x][i]]&&!p[x][i])
	{
		v[b[x][i]]=1;
		get(b[x][i]);
		if(_2) return;
	}
}

int make(int x,int y)
{
	return (x-1)*n+y;
}

void tarjan(int x,int fa,int number)
{
	dfn[x]=low[x]=++time;
	z[++tp]=(node){x,number};
	bz[x]=in[x]=1;
	fo(i,1,c[x])
	{
		int y=b[x][i];
		if(!bz[y])
		{
			int z=make(x,y);
			tarjan(y,x,z);
			low[x]=min(low[x],low[y]);
		}
		else if(in[y]&&y!=fa) low[x]=min(low[x],dfn[y]);
	}
	if(dfn[x]==low[x])
	{
		int now=tp;
		while(tp&&z[tp].x!=x) --tp;
		if(tp<now) h=dfn[x]; --tp;
	}
}

int main()
{
	freopen("travel.in","r",stdin);
	freopen("travel.out","w",stdout);
	read(n),read(m);
	fo(i,1,m)
	{
		int x,y;
		read(x),read(y);
		b[x][++c[x]]=y;
		b[y][++c[y]]=x;
	}
	fo(i,1,n) sort(b[i]+1,b[i]+1+c[i]);
	if(m==n-1)
	{
		dfs(1);
		return 0;
	}
	tarjan(1,0,0);
	memset(bz,0,sizeof(bz));
	fo(i,1,n) if(low[i]==h) bz[i]=1;
	memset(A,127,sizeof(A));
	int lx=0,ly=0;
	fo(i,1,n)
	{
		fo(j,1,c[i]) if(bz[i]&&bz[b[i][j]])
		{
			memset(v,0,sizeof(v));
			p[lx][ly]=0,p[i][j]=1;
			cnt=no=0,eq=1,_1=_2=0,get(1);
			if(_1) memcpy(A,B,sizeof(A));
			lx=i,ly=j;
		}
	}
	fo(i,1,n) write(A[i]),P(' ');
}

猜你喜欢

转载自blog.csdn.net/HuangXinyue1017/article/details/84200929