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 0huzi0 at 2009-05-04 08:34:09 on Problem 1637
#include<iostream>
using namespace std;
struct WAY
{
	int did,biao,id;
	WAY*next;
}way[2001];
struct NODE
{
	int ru,chu,rdc;
	bool visit;
	WAY*first;
}node[201];
int m,c;
bool adjust(int s)
{
	if(node[s].rdc>0)
	{
		return true;
	}
	WAY *temp;
	for(temp=node[s].first;temp!=NULL && node[s].rdc!=0;temp=temp->next)
	{
		if(temp->biao==1 && way[temp->did].biao==2 && node[temp->id].visit==false)
		{
			node[temp->id].visit=true;
			if(adjust(temp->id))
			{
				temp->biao=2;
				way[temp->did].biao=1;
				node[s].rdc+=2;
				node[temp->id].rdc-=2;
				node[temp->id].visit=false;
				return true;
			}
			node[temp->id].visit=false;
		}
	}
	return false;
}
int main()
{
	int n,s,i,k,u,v,p;
	scanf("%d",&n);
	while(n--)
	{
		memset(node,0,sizeof(node));
		memset(way,0,sizeof(way));
		c=0;
		p=1;
		scanf("%d%d",&m,&s);
		for(i=0;i<=m;i++)
		{
			node[i].first=NULL;
		}
		for(i=0;i<s;i++)
		{
			scanf("%d%d%d",&u,&v,&k);
			if(k==0)
			{
				way[p].did=p+1;
				way[p].id=v;
				way[p+1].did=p;
				way[p+1].id=u;
				way[p].next=node[u].first;
				way[p+1].next=node[v].first;
				way[p].biao=1;
				way[p+1].biao=2;
				node[u].chu++;
				node[u].first=&way[p];
				node[v].ru++;
				node[v].first=&way[p+1];
				p+=2;
			}
			else
			{
				way[p].did=0;
				way[p].id=v;
				way[p].biao=1;
				way[p].next=node[u].first;
				node[u].chu++;
				node[u].first=&way[p];
				node[v].ru++;
				p++;
			}
		}
		for(i=1;i<=m;i++)
		{
			node[i].rdc=node[i].ru-node[i].chu;
			if(node[i].rdc%2)
			{
				break;
			}
		}
		if(i>m)
		{
			for(i=1;i<=m;i++)
			{
				if(node[i].rdc<0)
				{
					while(node[i].rdc<0)
					{
						if(!adjust(i))break;
					}
					if(node[i].rdc!=0)
					{
						printf("impossible\n");
						break;
					}
				}
			}
			if(i>m)
			{
				printf("possible\n");
			}
		}
		else
		{
			printf("impossible\n");
		}
	}
	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