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 BEhind at 2009-02-17 11:28:06 on Problem 1129
#include <iostream>
#include <string.h>
using namespace std;

int color[26][26];
int numcol[26];
int used[26];
int n;
int acc;

int max()
{
	int max = 0;
	int tmp = 0;
	for (int i = 1; i <= n; i++)
	{
		if(numcol[i] > max && !used[i])
		{
			max = numcol[i];
			tmp = i;
		}
	}
	return tmp;
}
void bfs(int k)
{
	if(!k)
 		return ;
	used[k] = 1;
	for (int i = 1; i <= n; i++)
	{
		if(!used[i] && color[k][i] == 0)
		{
			used[i] = 1;
		}
 	}
 	acc++;
 	bfs(max());
}

int main()
{
	bool conn;
	char ch;
	char str[26];
	while(scanf("%d", &n) == 1 && n)
	{
		acc = 0;
		memset(color, 0, sizeof(color));
		memset(numcol, 0, sizeof(numcol));
		memset(used, 0, sizeof(used));
		for (int i = 1; i <= n; i++)
		{
			scanf("%s", str);
			numcol[i] = strlen(str) - 2;
            for (int j = 2; str[j]; j++)
            {
                int t = str[j] - 'A' + 1;
                color[i][t] = 1;
                color[t][i] = 1;
            }
		}
		bfs(max());
		if(!acc)
		{
			cout << "1 channel needed." << endl;
		}
		else
			cout << acc << " channels needed." << 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