Wrestling Match

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2097    Accepted Submission(s): 756

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".
 
Input
Input 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.
 
Output
If 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
 
Source
 
思路:现将已知身份的人染色,之后对每个人根据对应关系进行染色,若出现矛盾,或者有人没被染上(不联通),则输出NO,否则YES。
代码:
 #include<bits/stdc++.h>
//#include<regex>
#define db double
#include<vector>
#define ll long long
#define vec vector<ll>
#define Mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define MP make_pair
#define PB push_back
#define inf 0x3f3f3f3f3f3f3f3f
#define fr(i,a,b) for(int i=a;i<=b;i++)
const int N=1e3+;
const int mod=1e9+;
const int MOD=mod-;
const db eps=1e-;
const db PI=acos(-1.0);
using namespace std;
vector<int> g[N];
int n,m,x,y,ok;
int vis[N];
void dfs(int u)
{
int f=;
if(vis[u]==-) vis[u]=,f=;
for(int i=;i<g[u].size();i++){
int v=g[u][i];
if(vis[v]==-) vis[v]=-vis[u],dfs(v);//染色
else if(vis[v]==vis[u]&&f==) f=,vis[u]=-vis[v];//最多变换一次染色方式
else if(vis[v]==vis[u]) ok=;//矛盾
}
}
int main()
{
while(scanf("%d%d%d%d",&n,&m,&x,&y)==)
{
ok=;
for(int i=;i<=n;i++) g[i].clear();
memset(vis,-, sizeof(vis));
while(m--)
{
int u,v;
ci(u),ci(v);
g[u].push_back(v);
g[v].push_back(u);
}
while(x--){
int u;ci(u);vis[u]=;
}
while(y--){
int u;ci(u);vis[u]=;
}
for(int i=;i<=n;i++){
if(g[i].size()) dfs(i);
}
for(int i=;i<=n;i++) if(vis[i]==-) ok=;//不联通
if(ok) puts("YES");
else puts("NO");
}
return ;
}
 

最新文章

  1. ASP.NET MVC5+EF6+EasyUI 后台管理系统(61)-如何使用框架来开发
  2. SVN安装及使用
  3. JUCE 界面库显示中文乱码问题
  4. SAP RFC通信模式
  5. struts2 标签问题----日期显示
  6. K-th Number 线段树(归并树)+二分查找
  7. vi同类品
  8. linux上备份Oracle时EXP-00091的错误解决方法
  9. CSS三大样式
  10. 排序算法Java实现(堆排序)
  11. 网络爬虫BeautifulSoup库的使用
  12. Thread.join(), CountDownLatch、CyclicBarrier和 Semaphore区别,联系及应用
  13. Linux Swap交换分区探讨
  14. Python开发——数据类型【集合】
  15. Vue技巧
  16. 2D转换下的zoom和transform:scale的区别
  17. 2018.12.30 bzoj3027: [Ceoi2004]Sweet(生成函数+搜索)
  18. Codeforces Round #394 (Div. 2) A. Dasha and Stairs 水题
  19. 消息中间件activemq-5.13.0整合spring
  20. MyForm_参考django的Form组建

热门文章

  1. AngularJs在ng-click函数中如何获取代表当前元素的DOM对象
  2. 添加egit插件
  3. ArrayList,Vector, LinkedList 的存储性能和特性
  4. ArcSDE空间数据库中SDE用户使用探讨 (转载)
  5. repair table
  6. C#访问修改符
  7. pat乙级1049
  8. 302和VS启动后网站拒绝访问的解决方案
  9. 一、Web 如何工作的
  10. 深入理解计算机系统_3e 第十章家庭作业 CS:APP3e chapter 10 homework