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;
}
时间: 2024-12-28 08:21:20

HDU 1937 J - Justice League的相关文章

HDU 1937 F - Finding Seats

F - Finding Seats Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1937 Description A group of K friends is going to see a movie. However, they are too late to get good tickets, so they are looki

HDU 动态规划 J

Problem J Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 41   Accepted Submission(s) : 30 Problem Description 有一楼梯共M级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第M级,共有多少种走法? Input 输入数据首先包含一个整数N,表示测试实例的个数,然后是N行数据

转载:hdu 题目分类 (侵删)

转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093. 1094.1095.1096.1097.1098.1106.1108.1157.116

【贪心专题】HDU 1257 最少拦截系统&amp;&amp;HDU 2111 Saving HDU (选宝贝)

链接:click here~~ 题意: 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高度.某天,雷达捕捉到敌国的导弹来袭.由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹. 怎么办呢?多搞几套系统呗!你说说倒蛮容易,成本呢?成本是个大问题啊.所以俺就到这里来求救了,请帮助计算一下最少需要多少套拦截系统. [解题思路]: 直接模拟一边就可以,不知道想简单了还是数据

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY

[转] 一些图论、网络流入门题总结、汇总

最短路问题此类问题类型不多,变形较少 POJ 2449 Remmarguts' Date(中等)http://acm.pku.edu.cn/JudgeOnline/problem?id=2449题意:经典问题:K短路解法:dijkstra+A*(rec),方法很多相关:http://acm.pku.edu.cn/JudgeOnline/showcontest?contest_id=1144该题亦放在搜索推荐题中 POJ 3013 - Big Christmas Tree(基础)http://ac

【转】一些图论、网络流入门题总结、汇总

最短路问题 此类问题类型不多,变形较少 POJ 2449 Remmarguts' Date(中等) http://acm.pku.edu.cn/JudgeOnline/problem?id=2449 题意:经典问题:K短路 解法:dijkstra+A*(rec),方法很多 相关:http://acm.pku.edu.cn/JudgeOnline/showcontest?contest_id=1144 该题亦放在搜索推荐题中 POJ 3013 - Big Christmas Tree(基础) ht

2014 北京邀请赛ABDHJ题解

A. A Matrix 点击打开链接 构造,结论是从第一行开始往下产生一条曲线,使得这条区间最长且从上到下递减, #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <stdio.h> #include <vector> #include <set> using namespace std; #defi

第一次作业 0051

1939 Q1 Detective Dark Knight Gotham Robin Human Q2 1938 Kryptonian Metropolis The "S" symbol that became iconic Justice League Q1 Q2 <html><head><title>1939</title></head><body><h1>Q1</h1><div&