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

HDU 3729 I'm Telling the Truth (二分匹配)

时间:2017-07-29 21:25:18      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:algorithm   list   i+1   typedef   string   ios   put   sig   iostream   

题意:给定 n 个人成绩排名区间,然后问你最多有多少人成绩是真实的。

析:真是没想到二分匹配,。。。。后来看到,一下子就明白了,原来是水题,二分匹配,只要把每个人和他对应的区间连起来就好,跑一次二分匹配,裸的。

代码如下:

#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>
#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 = 101000 + 10;
const int mod = 10;
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];

void add(int u, int v){
  G[u].push_back(v);
  G[v].push_back(u);
}

int match[maxn];
bool vis[maxn];

bool dfs(int u){
  vis[u] = true;
  for(int i = 0; i < G[u].size(); ++i){
    int v = G[u][i];
    int w = match[v];
    if(w == -1 || !vis[w] && dfs(w)){
      match[u] = v;
      match[v] = u;
      return true;
    }
  }
  return false;
}

int main(){
  int T;  cin >> T;
  while(T--){
    scanf("%d", &n);
    for(int i = 0; i < maxn; ++i)  G[i].clear();
    for(int i = 0; i < n; ++i){
      int u, v;
      scanf("%d %d", &u, &v);
      for(int j = u+100; j <= v+100; ++j)
        add(i, j);
    }

    memset(match, -1, sizeof match);
    int ans = 0;
    for(int i = n-1; i >= 0; --i)  if(match[i] < 0){
      memset(vis, 0, sizeof vis);
      if(dfs(i))  ++ans;
    }
    printf("%d\n", ans);
    int cnt = 0;
    for(int i = 0; i < n; ++i)
      if(match[i] != -1)  printf("%d%c", i+1, ++cnt == ans ? ‘\n‘ : ‘ ‘);
  }
  return 0;
}

  

HDU 3729 I'm Telling the Truth (二分匹配)

标签:algorithm   list   i+1   typedef   string   ios   put   sig   iostream   

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

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