| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
好奇怪,我也超内存了!!!!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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator