Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
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 end of input is indicated by H = R = 0.
Output
Sample Input
Sample Output
N
Y
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int ind[];
int vis[];
int t[];
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==&&m==)
break;
memset(ind,,sizeof(ind));
memset(t,,sizeof(t));
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++){
t[i]=i;
}
int u,v;
vector<int>q[];
for(int i=;i<=m;i++){
scanf("%d%d",&u,&v);
ind[u]++;
ind[v]++;
q[u].push_back(v);
q[v].push_back(u);
}
sort(t+,t+n+,cmp); for(int i=;i<=n;i++){
int temp=t[i];
if(vis[temp]==){
for(int j=;j<q[temp].size();j++){
vis[q[temp][j]]=;
ind[q[temp][j]]--;
}
}
}
int ans=;
int tmin=;
for(int i=;i<=n;i++){
if(vis[i]){
ans++;
tmin=min(tmin,ind[i]);
}
} if(ans==tmin+)
printf("Y\n");
else
printf("N\n"); }
return ;
}