J-justice LeagueTime
limit:1000MS
Memory Limit:32768KB
64bit IO Format:%i64d &%i64 U SubmitStatusPracticeHDU 1941
Description
Thirty five years ago, a group of super Heroes was chosen to form the Justice League, whose purpose were to protect the PLA NET Earth from the villains. After all those years helping mankind, their members were retiring and now it's time to choose the new members of the Justic E League. In order to keep their secret identity, let's say, secret, super Heroes usually use a integer number to identify Themselv Es. There is 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 the A of the Super heroes has already worked together in a mission. If This happened, we say that the heroes has a relationship.
There must is only one Justice league in the world, which could is formed by any number of super heroes (even-only one). Moreover, for any of the heroes in the new league, they must has a relationship.
Besides, consider the set of the heroes not chosen to take part in the Justice League. For any of the heroes on this set, they must not has a relationship. This prevents the formation of unofficial justice leagues.
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 all test case contains-integers separated by a single space, H (2 <= H <= 5x10 4) and R (1 &L t;= R <= 5), indicating, respectively, the number of heroes and the number of relationships. Each of the following R lines contains-integers separated by a single space, A and B (1 <= a < b <= H), Indic Ating that super hero A have a relationship with super hero B. Note that if a have a relationship with B, B also have a Relat Ionship with A. A relationship is never informed twice in 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's possible to select a sub Set of Heroes to form the Justice league according to the given restrictions, or the uppercase letter "N" otherwise.
Sample Input
5 51 22 31 31 43 59 81 22 33 44 55 66 77 88 94 31 22 33 40 0
Sample Output
Yny
#include <stdio.h>#include<string.h>#include<iostream>#include<vector>#include<algorithm>using namespacestd;intind[60000];intvis[60000];intt[60000];BOOLcmpintAintb) { returnind[a]<ind[b];}intMain () {intn,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(intI=0; i<=n;i++) {T[i]=i; } intu,v; Vector<int>q[60000]; for(intI=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(intI=1; i<=n;i++){ inttemp=T[i]; if(vis[temp]==0){ for(intj=0; J<q[temp].size (); j + +) {Vis[q[temp][j]]=1; IND[Q[TEMP][J]]--; } } } intans=0; inttmin=60000; for(intI=1; i<=n;i++){ if(Vis[i]) {ans++; Tmin=min (tmin,ind[i]); } } if(ans==tmin+1) printf ("y\n"); Elseprintf ("n\n"); } return 0;}
HDU 1937 J-justice League