Wrestling Match hdu-5971(思维+STL)

Problem Description

Nowadays, at least one wrestling match is held every year in our country. There are a lot of people in the game is "good player”, the rest is "bad player”. Now, Xiao Ming is referee of the wrestling match and he has a list of the matches in his hand. At the same time, he knows some people are good players,some are bad players. He believes that every game is a battle between the good and the bad player. Now he wants to know whether all the people can be divided into "good player" and "bad player".

InputInput contains multiple sets of data.For each set of data,there are four numbers in the first line:N (1 ≤ N≤ 1000)、M(1 ≤M ≤ 10000)、X,Y(X+Y≤N ),in order to show the number of players(numbered 1toN ),the number of matches,the number of known "good players" and the number of known "bad players".In the next M lines,Each line has two numbersa, b(a≠b) ,said there is a game between a and b .The next line has X different numbers.Each number is known as a "good player" number.The last line contains Y different numbers.Each number represents a known "bad player" number.Data guarantees there will not be a player number is a good player and also a bad player.OutputIf all the people can be divided into "good players" and "bad players”, output "YES", otherwise output "NO".Sample Input

5 4 0 0
1 3
1 4
3 5
4 5
5 4 1 0
1 3
1 4
3 5
4 5
2

Sample Output

NO
YES题意:已知有n个人,他们进行了m场比赛,已知其中有X个好人,Y个坏人。比赛一定是在好人和坏人之间进行的。问是否能够把n个人划分成好人和坏人两个部分??

Input:
n,m,X,Y;接下来输入m行,每行输入两个数a和b,表示a和b之间进行了一场比赛。
Output:
如果能够把n个人分成好人和坏人两部分就输出YES;否则,输出NO。

思路:首先当a和b之间有比赛则说明a和b是对立的(即:若a好,则b坏;若a坏,则b好),所以凡是与a进行比赛的都是a的对立面。可以用vector容器进行统计a的对立都有哪些,进而把所有比赛人的对立都统计完全。

然后在输入好和坏的时候,分别这个人的对立(即vector[x]里的人)进行定义。如果是好(flag[x]=1),则vector[x]里的人全部是坏的(flag[t]=2);反之,同理。
在flag标记的时候,如果有flag[t]=1,又需要把flag[t]定义成2时,则说明t不能够清楚的分为好or坏。因此,ans=-1(ans的初始值为0)。
判断完已知的好坏之后,会发现有些人还是不能够分清楚。对之前的比赛进行分类,如果比赛的两个人都不能分清楚,则把第一个人定义为1,然后对第一个人的所有对立进行定义;如果只是第一个人没有进行定义,就把第一个人定义成第二个人的对立,然后对第一个人的所有对立进行定义。
最后,对所有人的flag进行遍历,查看是否有人的flag没有被定义(是否有人根本没有出现)。
然后根据ans,决定最后的输出结果。

AC代码:

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <queue>
  5 #include <algorithm>
  6 #include <map>
  7 #include <set>
  8 #include <stdlib.h>
  9 #include <stack>
 10 #include <vector>
 11 #include <cmath>
 12 #define ll long long
 13 using namespace std;
 14 vector<int>vec[1005];
 15 int mp[10005];
 16 int a[10005][2];
 17 int main()
 18 {
 19     int n,m,c,b,x,y;
 20     while(~scanf("%d%d%d%d",&n,&m,&c,&b))
 21     {
 22         memset(mp,0,sizeof(mp));
 23         memset(a,0,sizeof(a));
 24         int flag=0;
 25         for(int i=0; i<m; i++)
 26         {
 27             scanf("%d%d",&x,&y);
 28             a[i][0]=x;
 29             a[i][1]=y;
 30             vec[x].push_back(y);
 31             vec[y].push_back(x);
 32         }
 33         for(int i=0; i<c; i++)
 34         {
 35             scanf("%d",&x);
 36             if(mp[x]!=2)
 37                 mp[x]=1;
 38             else
 39                 flag=1;
 40             for(int j=0; j<vec[x].size(); j++)
 41             {
 42                 if(mp[vec[x][j]]!=1)
 43                     mp[vec[x][j]]=2;
 44                 else
 45                     flag=1;
 46             }
 47         }
 48         for(int i=0; i<b; i++)
 49         {
 50             scanf("%d",&x);
 51             if(mp[x]!=1)
 52                 mp[x]=2;
 53             else
 54                 flag=1;
 55             for(int j=0; j<vec[x].size(); j++)
 56             {
 57                 if(mp[vec[x][j]]!=2)
 58                     mp[vec[x][j]]=1;
 59                 else
 60                     flag=1;
 61             }
 62         }
 63         if(flag)
 64         {
 65             printf("NO\n");
 66         }
 67         else
 68         {
 69             for(int i=0; i<m; i++)
 70             {
 71                 if(mp[a[i][0]]==0&&mp[a[i][1]]==0)
 72                     mp[a[i][0]]=1;
 73                 else if(mp[a[i][0]]==0)
 74                 {
 75                     mp[a[i][0]]=mp[a[i][1]]%2+1;
 76                 }
 77                 x=a[i][0];
 78                 y=mp[x]%2+1;
 79                 for(int j=0; j<vec[a[i][0]].size(); j++)
 80                 {
 81                     if(mp[vec[x][j]]!=y%2+1)
 82                         mp[vec[x][j]]=y;
 83                     else
 84                         flag=1;
 85                 }
 86             }
 87             for(int i=1; i<=n; i++)
 88             {
 89                 if(mp[i]==0)
 90                 {
 91                     flag=1;
 92                     break;
 93                 }
 94             }
 95             if(flag)
 96                 printf("NO\n");
 97             else
 98                 printf("YES\n");
 99         }
100     }
101     return 0;
102 }

				
时间: 2024-11-06 09:49:24

Wrestling Match hdu-5971(思维+STL)的相关文章

hdu 5971 Wrestling Match 判断能否构成二分图

http://acm.hdu.edu.cn/showproblem.php?pid=5971 Wrestling Match Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 25    Accepted Submission(s): 15 Problem Description Nowadays, at least one wrestli

hdu 5971 Wrestling Match

Wrestling Match Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1031    Accepted Submission(s): 390 Problem Description Nowadays, at least one wrestling match is held every year in our country.

HDU Bombing (STL multiset+map)

题意:给你 n 个坐标(x,y),m 个询问(c,d) c==0,求出x==d有多少个,并删除这些点: c==1,求出y==d有多少个,并删除这些点. map+multiset的多重映射 #include<cstdio> #include<stdlib.h> #include<string.h> #include<string> #include<map> #include<cmath> #include<iostream>

HDU 4022 Bombing STL 模拟题

手动模拟.. #include<stdio.h> #include<iostream> #include<algorithm> #include<vector> #include<cmath> #include<queue> #include<set> #include<map> using namespace std; #define N 10100 #define inf 1000000010 map<

hdu 4883 思维题

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4883 TIANKENG’s restaurant Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 566    Accepted Submission(s): 267 Problem Description TIANKENG manages a

HDU 5971 Wrestling Match (二分图)

题意:给定n个人的两两比赛,每个人要么是good 要么是bad,现在问你能不能唯一确定并且是合理的. 析:其实就是一个二分图染色,如果产生矛盾了就是不能,否则就是可以的. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #

HDU 5971&quot;Wrestling Match&quot;(二分图染色)

传送门 •题意 给出 n 个人,m 场比赛: 这 m 场比赛,每一场比赛中的对决的两人,一个属于 "good player" 另一个属于 "bad player": 给出你 x 个已经确定的"good player" 和  y 个已经确定的 "bad player". 问是否可以将这 n 个人划分成两类,其中一类属于 "good player",另一类属于 "bad player": 即

HDU 2112 HDU Today(STL MAP + Djistra)

题目链接:HDU Today 立即集训要開始,抓紧时间练练手,最短路的基础题,第一次用STL的map 题目非常水,可是错了N遍.手贱了.本题不优点理的就是把地名转化为数字 #include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <map> #define N 155 #define INF 1e7 using namespace

HDU 5933/思维

题目链接[http://acm.hdu.edu.cn/showproblem.php?pid=5933]; 题意: 给出n堆物品,问能不能分成K个数量相等的堆,如果能分,则最少次数是多少.分的规则为:1.将一个堆分成相邻的两个堆.2.将相邻的两个堆合并.Power oj .均分纸牌. #include<bits/stdc++.h> const int maxn = 1e5+50; typedef long long LL; LL T,n,k; LL a[maxn]; int main () {