码迷,mamicode.com
首页 > 其他好文 > 详细

HDU5313——DP+vector——Bipartite Graph

时间:2015-08-01 17:12:16      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:

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\n", dp[u]);
        }
        }
        max1 = max(dp[u], max1);
    }
    printf("%d\n", max1);
   }
   return 0;
}

  

HDU5313——DP+vector——Bipartite Graph

标签:

原文地址:http://www.cnblogs.com/zero-begin/p/4694185.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!