BZOJ 1532 POI 2005 Kos-Dicing 最大流+二分

题目大意

给出一些比赛,每场比赛有一个人会胜出,问胜出最多次的人最少胜出多少次。

思路

首先二分答案,转化成判定问题。观察题目,注意到每场比赛只有一个人胜出,那么这可以成为网络流建图流量限制的依据。
具体:
S->每个人 f:二分的最大胜出次数。
每个人->他参与的比赛 f:1
每场比赛->T f:1
每次判断最大流和比赛是否相等。

CODE

#define _CRT_SECURE_NO_WARNINGS

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXP 20010
#define MAXE 1000010
#define S 0
#define T (MAXP - 1)
#define INF 0x3f3f3f3f
using namespace std;

struct MaxFlow{
    int head[MAXP], total;
    int _next[MAXE], aim[MAXE], flow[MAXE];

    int deep[MAXP];

    void Reset() {
        total = 1;
        memset(head, 0, sizeof(head));
    }
    void Add(int x, int y, int f) {
        _next[++total] = head[x];
        aim[total] = y;
        flow[total] = f;
        head[x] = total;
    }
    void Insert(int x, int y, int f) {
        Add(x, y, f);
        Add(y, x, 0);
    }
    bool BFS() {
        static queue<int> q;
        while(!q.empty())   q.pop();
        memset(deep, 0, sizeof(deep));
        deep[S] = 1;
        q.push(S);
        while(!q.empty()) {
            int x = q.front(); q.pop();
            for(int i = head[x]; i; i = _next[i])
                if(flow[i] && !deep[aim[i]]) {
                    deep[aim[i]] = deep[x] + 1;
                    q.push(aim[i]);
                    if(aim[i] == T) return true;
                }
        }
        return false;
    }
    int Dinic(int x, int f) {
        if(x == T)  return f;
        int temp = f;
        for(int i = head[x]; i; i = _next[i])
            if(flow[i] && temp && deep[aim[i]] == deep[x] + 1) {
                int away = Dinic(aim[i], min(flow[i], temp));
                if(!away)   deep[aim[i]] = 0;
                flow[i] -= away;
                flow[i^1] += away;
                temp -= away;
            }
        return f - temp;
    }
}solver;

pair<int, int> match[MAXP];
int points, edges;

inline void BuildGraph(int ans)
{
    solver.Reset();
    for(int i = 1; i <= points; ++i)
        solver.Insert(S, i, ans);
    for(int i = 1; i <= edges; ++i) {
        solver.Insert(points + i, T, 1);
        solver.Insert(match[i].first, points + i, 1);
        solver.Insert(match[i].second, points + i, 1);
    }
}

int main()
{
    cin >> points >> edges;
    for(int i = 1; i <= edges; ++i)
        scanf("%d%d", &match[i].first, &match[i].second);
    int l = 1, r = edges, ans = 1;
    while(l <= r) {
        int mid = (l + r) >> 1;
        BuildGraph(mid);
        int max_flow = 0;
        while(solver.BFS())
            max_flow += solver.Dinic(S, INF);
        if(max_flow == edges)
            r = mid - 1, ans = mid;
        else
            l = mid + 1;
    }
    cout << ans << endl;
    return 0;
}

你可能感兴趣的:(poi,最大流,二分,bzoj)