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;
    }
    

      

  • 相关阅读:
    让我们一起Go(八)
    让我们一起Go(七)
    让我们一起Go(六)
    VTemplate模板引擎的使用入门篇
    超时时间已过或服务器未响应的解决方法
    SQL Server 2005使用BCP命令将数据表导出到Excel第一行包括表头
    VTemplate模板引擎的使用进阶篇
    VTemplate模板引擎的使用认识篇
    免费开源的模板引擎VTemplate
    .NET 4.5 中新提供的压缩类
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4694185.html
Copyright © 2011-2022 走看看