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 yogafrank at 2008-10-09 10:09:05 on Problem 3697
In Reply To:什么思路呀???我怎么Memory Limit Exceeded Posted by:dhx123 at 2008-10-04 20:26:11
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

const int N = 10001;
bool flag[N];
int n, m;
queue <int> q;
vector <int> hash;

void swap ( int &s, int &t )
{
	int a = s;
	s = t;
	t = a;
}

void init()
{
	while ( !q.empty() )
		q.pop();
}

void bfs()
{
	int i;

	while ( !q.empty() )
	{
		int v = q.front();
		q.pop();

		if ( flag[v] )
			continue;

		flag[v] = true;
		for ( i = 2; i <= n; i++ )
		{
			if ( flag[i] )
				continue;

			int from = v, to = i;

			if ( from > to )
				swap ( from, to );
			if ( binary_search ( hash.begin(), hash.end(), ( from << 15 ) + to ) == false )
				q.push( to );
		}
	}
}

int main()
{
	int test = 0;
//	freopen ( "3697.txt", "r", stdin );
	while ( scanf ( "%d%d", &n, &m ) != -1 )
	{
		if ( n == 0 )
			break;
		int s, t, i;
		
		init ();
		memset ( flag, false, sizeof ( flag ) );
		for ( i = 0; i < m; i++ )
		{
			scanf ( "%d%d", &s, &t );
			if ( s > t )
				swap ( s, t );

			hash.push_back( ( s << 15 ) + t );
		}

		q.push( 1 );
		bfs();

		int cnt = 0;
		for ( i = 2; i <= n; i++ )
			if ( flag[i] )
				cnt++;
		printf ( "Case %d: %d\n", ++test, cnt );
	}
	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