zoukankan      html  css  js  c++  java
  • UVaLive 6525 Attacking rooks (二分图最大匹配)

    题意:给定一个 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
    ", ans);
      }
      return 0;
    }
    

      

  • 相关阅读:
    tomcat目录简介
    静态链接库、动态链接库
    查看软、硬raid信息的方法
    linu流量监控
    EXT格式误删除恢复
    linux下securetty文件
    linux下快速删除大量文件
    linux路由配置负载均衡
    python代码 构建验证码
    分布式系统架构
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7816717.html
Copyright © 2011-2022 走看看