HDU 1937 J - Justice League

此算法解决如何从已知关系的超级英雄中选出合适的正义联盟成员。确保被选中的英雄间存在合作关系,未入选的英雄间不存在合作,防止形成非官方联盟。
J - Justice League
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  HDU 1941

Description

Thirty five years ago, a group of super heroes was chosen to form the Justice League, whose purpose was to protect the planet Earth from the villains. After all those years helping mankind, its members are retiring and now it is time to choose the new members of the Justice League. In order to keep their secret identity, let’s say, secret, super heroes usually use an integer number to identify themselves. There are H super heroes on Earth, identified with the integers from 1 to H. With a brief look at the newspapers anyone can find out if two super heroes have already worked together in a mission. If this happened, we say that the two heroes have a relationship. 

There must be only one Justice League in the world, which could be formed by any number of super heroes (even only one). Moreover, for any two heroes in the new league, they must have a relationship. 

Besides, consider the set of the heroes not chosen to take part in the Justice League. For any two heroes on that set, they must not have a relationship. This prevents the formation of unofficial justice leagues. 

You work for an agency in charge of creating the new Justice League. The agency doesn’t know if it is possible to create the League with the restrictions given, and asked for your programming skills. Given a set of super heroes and their relationships, determine if it is possible to select any subset to form the Justice League, according to the given restrictions. 
 

Input

The input is composed of several test cases. The first line of each test case contains two integers separated by a single space, H (2 <= H <= 5×10  4) and R (1 <= R <= 10  5), indicating, respectively, the number of heroes and the number of relationships. Each of the following R lines contains two integers separated by a single space, A and B (1 <= A < B <= H), indicating that super hero A has a relationship with super hero B. Note that if A has a relationship with B, B also has a relationship with A. A relationship is never informed twice on a test case. 
The end of input is indicated by H = R = 0. 
 

Output

For each test case in the input print a single line, containing the uppercase letter “Y” if it is possible to select a subset of heroes to form the Justice League according to the given restrictions, or the uppercase letter “N” otherwise.
 

Sample Input

5 5
1 2
2 3
1 3
1 4
3 5
9 8
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
4 3
1 2
2 3
3 4
0 0
 

Sample Output

Y N Y
 
 
 
 
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int ind[60000];
int vis[60000];
int t[60000];
bool cmp(int a,int b){
   return ind[a]<ind[b];
}
int main(){
   int n,m;
   while(scanf("%d%d",&n,&m)!=EOF){
        if(n==0&&m==0)
        break;
       memset(ind,0,sizeof(ind));
       memset(t,0,sizeof(t));
       memset(vis,0,sizeof(vis));
       for(int i=0;i<=n;i++){
        t[i]=i;
       }
       int u,v;
       vector<int>q[60000];
       for(int i=1;i<=m;i++){
          scanf("%d%d",&u,&v);
          ind[u]++;
          ind[v]++;
          q[u].push_back(v);
          q[v].push_back(u);
       }
      sort(t+1,t+n+1,cmp);

      for(int i=1;i<=n;i++){
            int temp=t[i];
        if(vis[temp]==0){
                for(int j=0;j<q[temp].size();j++){
                    vis[q[temp][j]]=1;
                    ind[q[temp][j]]--;
                }
        }
      }
     int ans=0;
     int tmin=60000;
     for(int i=1;i<=n;i++){
        if(vis[i]){
            ans++;
            tmin=min(tmin,ind[i]);
        }
     }

     if(ans==tmin+1)
        printf("Y\n");
     else
        printf("N\n");

   }
   return 0;
}

 

 
 
 
 
 

 

 

转载于:https://www.cnblogs.com/13224ACMer/p/4678482.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值