`
java-mans
  • 浏览: 11426919 次
文章分类
社区版块
存档分类
最新评论

hdu 4337

 
阅读更多

hdu你敢把数据搞对吗?水论文题

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<cassert>
#include<cstring>
#include<iomanip>
using namespace std;

#ifdef _WIN32
#define i64 __int64
#define out64 "%I64d\n"
#define in64 "%I64d"
#else
#define i64 long long
#define out64 "%lld\n"
#define in64 "%lld"
#endif
/************ for topcoder by zz1215 *******************/
#define FOR(i,a,b)      for( int i = (a) ; i <= (b) ; i ++)
#define FFF(i,a)        for( int i = 0 ; i < (a) ; i ++)
#define FFD(i,a,b)      for( int i = (a) ; i >= (b) ; i --)
#define S64(a)          scanf(in64,&a)
#define SS(a)           scanf("%d",&a)
#define LL(a)           ((a)<<1)
#define RR(a)           (((a)<<1)+1)
#define pb              push_back
#define MAX(a,b)        ((a)>(b)?(a):(b))
#define MIN(a,b)        ((a)<(b)?(a):(b))
#define CL(Q)           while(!Q.empty())Q.pop()
#define MM(name,what)   memset(name,what,sizeof(name))
#define read            freopen("in.txt","r",stdin)
#define write           freopen("out.txt","w",stdout)

const int inf = 0x3f3f3f3f;
const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;
const double oo = 10e9;
const double eps = 10e-9;
const double pi = acos(-1.0);
const int maxn = 155;

vector<int>g[maxn];
bool a[maxn][maxn];
int n,m;
deque<int>d;
bool vis[maxn];
bool ring;
int tot;


int find(int now)
{
	int to;
	for(int i=0;i<g[now].size();i++)
	{
		to = g[now][i];
		if(!vis[to])
		{
			return to;
		}
	}
	return 0;
}

void add()
{
	if(!ring) return ;
	int now,to,ti;
	for(int i=0;i<d.size();i++)
	{
		now=d[i];
		to = find(d[i]);
		if(to)
		{
			ti = i;
			break;
		}
	}
	deque<int>tmp;
	tmp.clear();
	tmp.pb(to);
	vis[to]=true;
	tot++;
	for(int i=ti;i<d.size();i++)
	{
		tmp.pb(d[i]);
	}
	for(int i=0;i<ti;i++)
	{
		tmp.pb(d[i]);
	}
	d = tmp;
	if(a[d[0]][d.back()])
	{
		ring = true;
	}
	else
	{
		ring = false;
	}
	return ;
}

void make_ring()
{
	if(ring) return ;
	int now,to;
	while(true)
	{
		now = d[0];
		to = find(now);
		if(to)
		{
			tot++;vis[to]=true;
			d.push_front(to);
		}
		else break;
	}
	while(true)
	{
		now = d.back();
		to = find(now);
		if(to)
		{
			d.pb(to);
			vis[to]=true;
			tot++;
		}
		else
		{
			break;
		}
	}
	int s,t;
	s = d[0];
	t = d.back();
	if(a[s][t])
	{
		ring = true;
		return ;
	}
	for(int i=1;i<d.size()-2;i++)
	{
		now = d[i];
		to = d[i+1];
		if(a[now][t] && a[s][to])
		{
			deque<int>tmp;
			tmp.clear();
			for(int u=i+1;u<d.size();u++)
			{
				tmp.pb(d[u]);
			}
			for(int u=i;u>=0;u--)
			{
				tmp.pb(d[u]);
			}
			d = tmp;
			ring=true;
			return ;
		}
	}
	if(a[d[0]][d.back()])
	{
		ring = true;
	}
	else
	{
		ring = false;
	}
	return ;
}


void start()
{
	MM(vis,false);
	d.clear();
	d.pb(1);
	vis[1]=true;
	ring = false;
	tot = 1;
	while(tot<n)
	{
		make_ring();
		if(tot==n)
		{
			break;
		}
		add();
	}
	make_ring();
	return ;
}

int main()
{
	int now,to;
	while(cin>>n>>m)
	{
		for(int i=1;i<=n;i++)
		{
			g[i].clear();
		}
		MM(a,false);
		for(int i=1;i<=m;i++)
		{
			cin>>now>>to;
			if(now!=to)
			{
				g[now].pb(to);
				g[to].pb(now);
				a[now][to]=true;
				a[to][now]=true;
			}
		}
		start();
		if(ring)
		{
			for(int i=0;i<d.size()-1;i++)
			{
				cout<<d[i]<<" ";
			}
			cout<<d.back()<<endl;
		}
		else
		{
			cout<<"no solution"<<endl;
		}
	}
    return 0;
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics