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

Re:how to solve it?

Posted by anne2277 at 2005-07-07 16:09:14 on Problem 1463
In Reply To:how to solve it? Posted by:anne2277 at 2005-07-06 21:32:11
使用邻接表也不行呀。
#include<iostream>
#include<stdio.h>
using namespace std;

int n;
int visit[1502];
struct type
{
    int num;
    int q[2202];    
}a[1502];

void greedy()
{
    memset( visit, 0, sizeof(visit) );
    int cnt = 0;
    while( 1 )
    {
        int max = 0;
        int index;
        for( int i = 0; i < n; i++ )
        {
                
                if( visit[i] == 1 )continue;
                
                if( a[i].num <= max )continue;
                int sum = 1;
                
                //寻找最大连接的点 
                for( int j = 0; j < a[i].num; j++ )
                {
                    if( visit[j] == 0 )
                    {
                           sum++;    
                    }
                }    
                if( sum > max )
                {
                                max = sum;
                                index = i;    
                }
        }
        if( max == 0 )break;
        
        
        //更新,调整过程 
        cnt++;
        visit[index] = 1;
        for( int i = 0; i < a[index].num; i++ )
        {
                visit[a[index].q[i]] = 1;    
        }
    }    
    printf( "%d\n", cnt );
}
/*
void output()
{
    for( int i = 0; i < n; i++ )
    {
        cout << i << ": ";
        for( int j = 0; j < a[i].num; j++ )
        {
                cout <<   a[i].q[j] << " ";  
        }    
        cout << endl;
    }  
    cout << endl;  
}
*/

int main()
{
    while( scanf( "%d", &n ) != EOF )
    {
        int v;
        memset( a, 0, sizeof(a) );
        
        for( int i = 0; i < n; i++ )
        {
                scanf( "%d", &v );
                getchar();
                getchar();
                int num;
                scanf( "%d", &num );
                getchar();
                
                int tmp;
                for( int j = a[v].num; j < a[v].num+num; j++ )
                {
                      scanf( "%d", &tmp );
                      a[v].q[j] = tmp; 
                      a[tmp].q[a[tmp].num++] = v;  
                }   
                a[v].num += num;
        }    
        //output();
        greedy();
    }    
}

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