zoukankan      html  css  js  c++  java
  • 四川第七届 D Vertex Cover(二分图最小点覆盖,二分匹配模板)

    Vertex Cover

    frog has a graph with nn vertices v(1),v(2),,v(n)v(1),v(2),…,v(n) and mm edges (v(a1),v(b1)),(v(a2),v(b2)),,(v(am),v(bm))(v(a1),v(b1)),(v(a2),v(b2)),…,(v(am),v(bm)).

    She would like to color some vertices so that each edge has at least one colored vertex.

    Find the minimum number of colored vertices.

    Input

    The input consists of multiple tests. For each test:

    The first line contains 22 integers n,mn,m (2n500,1mn(n1)22≤n≤500,1≤m≤n(n−1)2). Each of the following mm lines contains 22 integers ai,biai,bi (1ai,bin,aibi,min{ai,bi}301≤ai,bi≤n,ai≠bi,min{ai,bi}≤30)

    Output

    For each test, write 11 integer which denotes the minimum number of colored vertices.

    Sample Input

        3 2
        1 2
        1 3
        6 5
        1 2
        1 3
        1 4
        2 5
        2 6

    Sample Output

        1
        2

    题意:有n个点m条边,每条边至少有一个顶点染色,至少要染多少个点

    思路:二分图最小点覆盖,有一个公式 二分图最大匹配=最小点覆盖,匈牙利算法一代就出来了

    #include<cstdio>
    #include<string.h>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<cmath>
    #include<iostream>
    #include<algorithm>
    #include<deque>
    using namespace std;
    #define ll unsigned long long
    vector<int>v[505];
    int match[505];
    bool used[505];
    bool dfs(int x)
    {
        used[x]=1;//标记询问过了
        for(int i=0;i<v[x].size();i++)
        {
            int y=v[x][i];
            int w=match[y];
            if(w<0||!used[w]&&dfs(w))
            {//如果它没有匹配过或者(没有问过而且可以找到其它)
                match[x]=y;
                match[y]=x;
                return 1;
            }
        }
        return 0;
    }
    int main()
    {
        int n,m;
        while(cin>>n>>m)
        {
            for(int i=0;i<=502;i++) v[i].clear();
            for(int i=1;i<=m;i++)
            {
                int x,y;
                cin>>x>>y;
                v[x].push_back(y);
                v[y].push_back(x);
            }
            int res=0;
            memset(match,-1,sizeof(match));
            for(int i=1;i<=n;i++)
            {
                if(match[i]<0)//还没匹配
                {
                    memset(used,0,sizeof(used));//每次都要初始化
                    if(dfs(i))
                    //如果能匹配
                        res++;
                    }
                }
            }
            cout<<res<<endl;
        }
        return 0;
    }
  • 相关阅读:
    設計模式學習(一)觀察者模式(Observer)
    JavaScript對Table進行排序(jQuery)
    一个很好的String组合连接的方法(StringBuffer)
    关于C#静态构造函数的几点说明 (轉載)
    在DataGrid中添加序號
    C# 对象深拷贝、浅铐贝、直接拷贝(转)
    在做數據插入時,傳參數應該注意的小細節!
    JavaScript檢驗時間格式
    獲得鼠標位置(js)
    my tools
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/13271040.html
Copyright © 2011-2022 走看看