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

为什么贪心过不了,所有的测试数据都过。(附code)帮忙看一下为什么?

Posted by RUNSLOWLY at 2009-08-04 11:57:37 on Problem 3715
#include<iostream>
using namespace std;
#define NUM 210
int color[NUM],fr[NUM][NUM],str[NUM],n;//用链表来表示其不同颜色的之间朋友str用来表示其朋友数。
int out[NUM];
int main()
{
	int i,a,b,m,sum,j;
	int cases;
	int max;
	cin>>cases;
	while(cases--)
	{
		cin>>n>>m;
		for(i=0;i<n;i++)
		{
			str[i]=0;  //不同颜色朋友个数初始化为0.
			out[i]=0;  //初始化为0表示没有被删除。
		}
	
		for(i=0;i<n;i++)
		{
			cin>>color[i];
		}
		memset(fr,0,sizeof(fr));
		for(i=0;i<m;i++)
		{
			cin>>a>>b;
			if(color[a]!=color[b])
			{  
				if(fr[a][b]==0)
				{
			        str[a]++;
		        	str[b]++;
			    	fr[a][b]=1;
			    	fr[b][a]=1;
				}
			}
		}	
		sum=0;
		for(i=0;i<n-1;i++)
		{
			max=0;
			a=0;
			for(j=0;j<n;j++)
			{
				if(max<str[j] || max>=2 && max==str[j])
				{
					max=str[j];
					a=j;
				}
			}
			if(max>0)
			{
				out[a]=1;
				sum++;	
				for(b=0;b<n;b++)
				{
					if(fr[a][b]==1)
					{
						fr[b][a]=0;
						str[b]--;
					}
				}
				str[a]=0;
			}

		}
		cout<<sum;
		for(i=0;i<n;i++)
		{
			if(out[i]==1)
				cout<<' '<<i;
		}
		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