码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5971 Wrestling Match (二分图)

时间:2017-05-01 22:18:19      阅读:313      评论:0      收藏:0      [点我收藏+]

标签:tac   hdu   puts   name   ++   1.0   map   产生   memset   

题意:给定n个人的两两比赛,每个人要么是good 要么是bad,现在问你能不能唯一确定并且是合理的。

析:其实就是一个二分图染色,如果产生矛盾了就是不能,否则就是可以的。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
  return r >= 0 && r < n && c >= 0 && c < m;
}

vector<int> G[maxn];
set<int> sets;
int a[maxn], b[maxn];
int color[maxn];
bool ok;

void dfs(int u, int x){
  if(!ok)  return ;
  for(int i = 0; i < G[u].size(); ++i){
    int v = G[u][i];
    if(color[v] && color[v] + color[u] != 3){
      ok = false;  return ;
    }
    if(color[v])  continue;
    color[v] = x;
    dfs(v, 3 - x);
  }
}

int main(){
  int x, y;
  while(scanf("%d %d %d %d", &n, &m, &x, &y) == 4){
    for(int i = 1; i <= n; ++i)  G[i].clear();
    sets.clear();
    for(int i = 0; i < m; ++i){
      int u, v;
      scanf("%d %d", &u, &v);
      G[u].push_back(v);
      G[v].push_back(u);
      sets.insert(u);
      sets.insert(v);
    }
    memset(color, 0, sizeof color);
    for(int i = 0; i < x; ++i){
      scanf("%d", a+i);
      sets.insert(a[i]);
    }
    for(int i = 0; i < y; ++i){
      scanf("%d", b+i);
      sets.insert(b[i]);
    }
    if(sets.size() != n){ puts("NO");  continue; }
    ok = true;
    for(int i = 0; i < x && ok; ++i){
      if(color[a[i]] && color[a[i]] != 1) ok = false;
      color[a[i]] = 1;
      dfs(a[i], 2);
    }
    for(int i = 0; i < y && ok; ++i){
      if(color[b[i]] && color[b[i]] != 2) ok = false;
      color[b[i]] = 2;
      dfs(b[i], 1);
    }
    for(int i = 1; i <= n && ok; ++i)  if(!color[i]){
      color[i] = 1;
      dfs(i, 2);
    }
    printf("%s\n", ok ? "YES" : "NO");
  }
  return 0;
}

  

HDU 5971 Wrestling Match (二分图)

标签:tac   hdu   puts   name   ++   1.0   map   产生   memset   

原文地址:http://www.cnblogs.com/dwtfukgv/p/6792855.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!