Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

这是我的代码,请帮我找出不合理的地方吧

Posted by answeran at 2009-06-10 20:33:57 on Problem 2337
> 这个题对结果的输出是不是有什么特殊要求,感觉自己的程序可以找到图的欧拉路(如果存在),可是总是不能AC
#include<iostream>
#include<string>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
class eage
{
public:
	bool visited;
	string a;
	eage(bool v=false):visited(v){}
	bool operator <(const eage &b)
	{
		return a>b.a;
	}
};
class Node
{
public:
	char value;
	vector<eage> Q;
	int E;
	int O;
	Node(int N=0,int m=0):E(N),O(m){Q.clear();}
	bool operator <(const Node& d)
	{return value<d.value;}
};
vector<Node> v;
int Num=0,size;
void input()
{
	int n,i,j,k,fc,sc;
	cin>>size;
	eage c;
	Node d;
	string temp;
	char a,b;
	n=size;
	v.clear();
	Num=0;
	i=0;
	while(n--)
	{
		fc=sc=0;
		cin>>temp;
		c.a=temp;
		a=temp[0];
		b=temp[temp.size()-1];
		for(j=0;j<i;j++)
		{
			if(a==v[j].value)
			{
				fc=1;
				v[j].O++;
				v[j].Q.push_back(c);
			}
			if(b==v[j].value)
			{
				sc=1;
				v[j].E++;
			}
		}
		if(fc==0||sc==0)
		{
			if(a==b)
			{
				d.value=a;
				d.E=1;
				d.O=1;
				d.Q.push_back(c);
				v.push_back(d);
				i++;
			}
			else
			{
				if(fc==0)
				{
					d.value=a;
					d.Q.push_back(c);
					d.O=1;
					v.push_back(d);
					d.Q.clear();
				}
				if(sc==0)
				{
					d.value=b;
					d.O=0;
					d.E=1;
					v.push_back(d);
				}
				i+=2-fc+sc;
			}
		}
		d.O=0;
		d.E=0;
		d.Q.clear();
	}
	sort(v.begin(),v.end());
	for(j=0;j<i;j++)
		sort(v[j].Q.begin(),v[j].Q.end());
};
bool connected(int index)
{
	bool used[26];
	fill_n(used,26,false);
	queue<int> T;
	int i=0,j;
	used[index]=true;
	T.push(index);
	while(!T.empty())
	{
		int x=T.front();
		for(i=0;i<v[x].Q.size();i++)
		{
			
				char str=v[x].Q[i].a[v[x].Q[i].a.size()-1];
				for(j=0;j<v.size();j++)
					if(v[j].value==str&&used[j]==false)
					{	
						T.push(j);
						used[j]=true;
					}
		
		}
		T.pop();
	}
	for(i=0;i<v.size();i++)
		if(used[i]!=true&&v[i].E+v[i].O!=0)
			return false;
	return true;
};
void dfs(int index)
{
	int i,j,e=-1;
	for(i=v[index].O-1;i>=0;i--)
	{
		if(v[index].Q[i].visited==false)
		{
			v[index].O--;
			char str=v[index].Q[i].a[v[index].Q[i].a.size()-1];
			for(j=0;j<v.size();j++)
			if(v[j].value==str&&connected(j))
				e=j;
			if(e!=-1)
			{
				cout<<v[index].Q[i].a;
				v[index].Q[i].visited=true;
				break;
			}
			else{
				v[index].O++;}
		}
	}
	if(e==-1) 
	{
		return;
	}
	v[e].E--;
	Num++;
	if(Num<size)
		cout<<".";
	dfs(e);
}
void solve()
{
	int i,j,sum=0,b=0;
	for(i=0;i<v.size();i++)
	{
		if(v[i].E<v[i].O)
			b=i;
		if(v[i].E!=v[i].O)
		{
			sum++;
		}
		if(v[i].E-v[i].O>1||v[i].E-v[i].O<-1)
		{
			sum=10;
			break;
		}
	}
	if(sum>2)
	{
		cout<<"***";
		return;
	}
	if(!connected(b))
	{
		cout<<"***";
		return;
	}
	dfs(b);
}
int main()
{
int c;
cin>>c;
while(c--)
{
   input();
   solve(); 
   cout<<endl;
}
return 0;

}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator