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

贪心算法居然0ms通过了

Posted by haining at 2012-09-07 20:15:23 on Problem 1129
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<string>
using namespace std;

int map[30][30];
int visit[30];
int q[30];
int res,next;
char a[30];

int main()
{
    int n,i,j,k;
    while(scanf("%d",&n)&&n)
    {
     memset(map,0,sizeof(map));
     memset(visit,0,sizeof(visit));
       getchar();
     for(i=0;i<n;i++)
     {
      gets(a);      
      for(j=2;j<strlen(a);j++)
      {
       map[i][a[j]-'A']=1;
       map[a[j]-'A'][i]=1;
      }
     }    
     
    res=1;
    next=0;
    int flag,f1;
    while(res<4)
    {
      flag=1;
      visit[next]=1;
      j=1;q[0]=next;
      
      for(i=0;i<n;i++){
        if(visit[i])continue;
        
        f1=1;
        for(k=0;k<j;k++)
        if(map[i][q[k]]==1)
        f1=0;

        if(f1==1){
               visit[i]=1;
              q[j]=i;
               j++;           
               }
      }
     
       
      for(i=0;i<n;i++)
      if(!visit[i]){
                    next=i;
                    res++;
                    flag=0;
                    break;
                    }
     
     if(flag==1)break;
     }      
     if(flag==1){
                 if(res==1)printf("1 channel needed.\n");
                 else printf("%d channels needed.\n",res);
                 }
     else {
          printf("4 channels needed.\n");
          }
    }
    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