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

UVaLive 6525 Attacking rooks (二分图最大匹配)

时间:2017-11-10 23:08:16      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:double   iostream   freopen   namespace   攻击   pre   ios   void   its   

题意:给定一个 n * n的图,X是卒, . 是空位置,让你放尽量多的车,使得他们不互相攻击。

析:把每行连续的 . 看成X集体的一个点,同理也是这样,然后求一个最大匹配即可。

代码如下:

#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>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#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 fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
//#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n)  for(int i = (x); i < (n); ++i)
#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 = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 100 + 10;
const int maxm = 3e5 + 10;
const ULL mod = 3;
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;
}

char s[maxn][maxn];
int a[maxn][maxn], b[maxn][maxn];

struct Edge{
  int to, next;
};
Edge edges[maxn*maxn];
int head[maxn*maxn], cnt;

void addEdge(int u, int v){
  edges[cnt].to = v;
  edges[cnt].next = head[u];
  head[u] = cnt++;
}

int match[maxn*maxn];
bool used[maxn*maxn];

bool dfs(int u){
  used[u] = 1;
  for(int i = head[u]; ~i; i = edges[i].next){
    int v = edges[i].to, w = match[v];
    if(w == -1 || !used[w] && dfs(w)){
      match[u] = v;
      match[v] = u;
      return true;
    }
  }
  return false;
}

int main(){
  while(scanf("%d", &n) == 1){
    int idx = 0;
    for(int i = 0; i < n; ++i){
      scanf("%s", s[i]);
      if(s[i][0] == ‘.‘)  a[i][0] = ++idx;
      for(int j = 1; j < n; ++j)
        if(s[i][j] == ‘.‘ && s[i][j] == s[i][j-1])  a[i][j] = a[i][j-1];
        else if(s[i][j] == ‘.‘)  a[i][j] = ++idx;
    }
    int row = idx;
    for(int j = 0; j < n; ++j){
      if(s[0][j] == ‘.‘)  b[0][j] = ++idx;
      for(int i = 1; i < n; ++i)
        if(s[i][j] == ‘.‘ && s[i][j] == s[i-1][j])  b[i][j] = b[i-1][j];
        else if(s[i][j] == ‘.‘) b[i][j] = ++idx;
    }
    cnt = 0;  ms(head, -1);
    FOR(i, 0, n)  FOR(j, 0, n) if(s[i][j] == ‘.‘){
      addEdge(a[i][j], b[i][j]);
      addEdge(b[i][j], a[i][j]);
    }
    int ans = 0;  ms(match, -1);
    for(int i = 1; i <= row; ++i)  if(match[i] < 0){
      ms(used, 0);  if(dfs(i))  ++ans;
    }
    printf("%d\n", ans);
  }
  return 0;
}

  

UVaLive 6525 Attacking rooks (二分图最大匹配)

标签:double   iostream   freopen   namespace   攻击   pre   ios   void   its   

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

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