zoukankan      html  css  js  c++  java
  • UVa 1663 Purifying Machine (二分匹配)

    题意:每一个01串中最多含有一个‘*’,‘*’既可表示0也可表示1,给出一些等长的这样的01串,问最少能用多少个这样的串表示出这些串。

    如:000、010、0*1表示000、010、001、011,最少只需用00*、01*这两个即可表示出来。

    析:因为最多只有一个星,所以每个串最多能代表两个串,所以就是要两两匹配的尽量多,也就是二分匹配喽,要注意,给的串可能会有重复的。

    代码如下:

    #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 double inf = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 2000 + 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<string> v;
    int V;
    vector<int> G[maxn];
    int match[maxn];
    bool used[maxn];
    
    void addEdge(int u, int v){
      G[u].push_back(v);
      G[v].push_back(u);
    }
    
    bool dfs(int v){
      used[v] = true;
      for(int i = 0; i < G[v].size(); ++i){
        int u = G[v][i], w = match[u];
        if(w < 0 || !used[w] && dfs(w)){
          match[v] = u;
          match[u] = v;
          return true;
        }
      }
      return false;
    }
    
    int solve(){
      int res = 0;
      memset(match, -1, sizeof match);
      for(int v = 0; v < V; ++v)  if(match[v] < 0){
        memset(used, 0, sizeof used);
        if(dfs(v))  ++res;
      }
      return res;
    }
    
    bool judge(int i, int j){
      int cnt = 0, k = 0;
      while(k < n){
        if(v[i][k] != v[j][k]) ++cnt;
        ++k;
      }
      return cnt == 1;
    }
    
    int main(){
      while(cin >> n >> m && m+n){
        v.clear();
        for(int i = 0; i < m ; ++i){
          string s;
          cin >> s;
          if(s.find('*') != string::npos){
            int pos = s.find('*');
            s[pos] = '1';
            v.push_back(s);
            s[pos] = '0';
          }
          v.push_back(s);
        }
        sort(v.begin(), v.end());
        V = unique(v.begin(), v.end()) - v.begin();
        for(int i = 0; i < V; ++i)  G[i].clear();
        for(int i = 0; i < V; ++i)
          for(int j = i+1; j < V; ++j)
            if(judge(i, j))  addEdge(i, j);
        printf("%d
    ", V - solve());
      }
      return 0;
    }
    
  • 相关阅读:
    linux下配置redis
    前端之JavaScript:JS之DOM对象一
    前端之JavaScript:JavaScript对象
    css样式之补充
    css属性中常见的操作方法
    css属性操作
    css选择器
    html 表单操作
    前端基础之html
    1231211221211221
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/6545101.html
Copyright © 2011-2022 走看看