zoukankan      html  css  js  c++  java
  • HDU5313——DP+vector——Bipartite Graph

    Soda has a bipartite graph with n vertices and m undirected edges. Now he wants to make the graph become a complete bipartite graph with most edges by adding some extra edges. Soda needs you to tell him the maximum number of edges he can add.

    Note: There must be at most one edge between any pair of vertices both in the new graph and old graph.

     


    Input
    There are multiple test cases. The first line of input contains an integer T (1T100), indicating the number of test cases. For each test case:

    The first line contains two integers n and m(2n10000,0m100000).

    Each of the next m lines contains two integer u,v (1u,vn,vu) which means there's an undirected edge between vertex u and vertex v.

    There's at most one edge between any pair of vertices. Most test cases are small.
     


    Output
    For each test case, output the maximum number of edges Soda can add.
     


    Sample Input
    2 4 2 1 2 2 3 4 4 1 2 1 4 2 3 3 4
     


    Sample Output
    2 0
     


    Source
     


    Recommend
    /*
    大意:找到最长的上升序列(要求连在一起)
    DP思想 dp[u] += dp[v]
    从最长的开始找上升
    */
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    using namespace std;
    int n;
    int b[500010];
    vector<int> G[500010];
    int dp[500010];
    struct edge{
        int num, id;
    }a[500010];
    
    bool cmp(edge i, edge j)
    {
        return i.num < j.num;
    }
    int main()
    {
        int x, y;
       while(~scanf("%d", &n)){
           for(int i = 1; i < n ; i++)
               G[i].clear();
        for(int i = 1; i <= n ; i++){
            scanf("%d", &a[i].num);
            a[i].id = i;
        }
        for(int i = 1; i <= n; i++)
            b[i] = a[i].num;
        sort(a + 1, a + n + 1,cmp);
        for(int i = 1; i < n ; i++){
            scanf("%d%d", &x, &y);
            G[y].push_back(x);
            G[x].push_back(y);
        }
        int max1 = 1;
        memset(dp, 0, sizeof(dp));
        for(int i = n ; i >= 1; i--){
            int u = a[i].id;
            dp[u] = 1;
            for(int j = 0 ; j < G[u].size(); j++){
                int  v = G[u][j];
                if(b[v] > b[u]) {
                    dp[u] += dp[v];
                   // printf("%d
    ", dp[u]);
            }
            }
            max1 = max(dp[u], max1);
        }
        printf("%d
    ", max1);
       }
       return 0;
    }
    

      

  • 相关阅读:
    运算符重载
    C++ 画星号图形——圆形(核心代码记录)
    C++ 画星号图形——空心三角形(星号左对齐)(核心代码介绍)
    C++ 画星号图形——空心三角形(星号居中对齐)(核心代码介绍)
    QMap迭代器
    QVector也是隐式数据共享的
    调试dump文件
    How does the compilation and linking process work?
    when to use reinterpret_cast
    构造类的时候,提供类型转换
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4694185.html
Copyright © 2011-2022 走看看