cogs 14. [24 network flow problem] with the maximum pilot bipartite graph matching algorithms Hungary

14. [24 network flow problem] with pilots

★★ Input file: flyer.in   Output file: flyer.out   a simple comparison of
the time limit: 1 s memory limit: 128 MB

【Problem Description】
    Flying brigade has several drivers from around the world, specializing in driving one model of aircraft, aircraft have two pilots per aircraft, to be a positive driver and a co-pilot. Due to various reasons, such as problems with each other, some drivers can not fly on the same plane, with the driver and asked how to make the most of the aircraft sail.

 
As shown, assume that the driver 10, as shown by V1, V2, ..., V10 on behalf of the driver 10, wherein the V1, V2, V3, V4, V5 is a positive driver, V6, V7, V8, V9 , V10 is the co-pilot. If being a pilot and a co-pilot can fly the same plane, on behalf of even a line between the two of them, two people can not fly the same plane, not even. For example V1 and V7 can fly the same plane, while V1 and V8 can not. Please pilots with the most aircraft set sail. Note: Because driving strict division of labor, two positive or two pilot co-pilot can not fly the same plane.
 
[Input Format]
Several rows input file
a first row, two integers n and n1, there are n represents pilots (2 <= n <= 100 ), which pilots n1 is a positive driver.
Here are several rows, each row having two figures a, b. It represents a positive pilot and co-pilot b can fly the same plane.
Note: the number of positive front of the driver, i.e., a positive number less than the driver's passenger number.
[Output format]
The output file has a line
of the first row, an integer representing the maximum number of aircraft taking off.
[O] Sample
Enter the file name: flyer.in
10 5 
1 7 
2 6 
2 10 
3 7 
4 8 
5 9 
 
Output File Name: flyer.out
4
 
 
#include<bits/stdc++.h>
#define maxn 105
using namespace std;
int n,n1,n2,ans=0;
vector<int> v[maxn];
int vis[maxn],tim,hav[maxn];
bool Dfs(int rt){
    for(int i=0;i<v[rt].size();i++){
        int to=v[rt][i];
        if(vis[to]!=tim){
            vis[to]=tim;
            if(!hav[to]||Dfs(hav[to])){
                hav[to]=rt;
                return true;
            }
        }
    }
    return false;
}
int main(){
//    freopen("flyer.in","r",stdin);
//    freopen("flyer.out","w",stdout);
    scanf("%d%d",&n,&n1);int n2=n-n1;
    int x,y;
    while(scanf("%d%d",&x,&y)!=EOF) v[x].push_back(y);
    for(int i=1;i<=n1;i ++ )
        Tim++,ans+=Dfs(i);
    printf("%d",ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Tidoblogs/p/11315545.html