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 (1≤T≤100), indicating the number of test cases. For each test case:

The first line contains two integers n and m, (2≤n≤10000,0≤m≤100000).

Each of the next m lines contains two integer u,v (1≤u,v≤n,v≠u) 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

BestCoder 1st Anniversary ($)

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

  

时间: 2024-10-11 05:08:32

HDU5313——DP+vector——Bipartite Graph的相关文章

HDU 5313——Bipartite Graph——————【二分图+dp+bitset优化】

Bipartite Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 840    Accepted Submission(s): 285 Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he w

hdu 5313 Bipartite Graph 完全二分图 深搜 bitset应用

Bipartite Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 577    Accepted Submission(s): 154 Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he

HDU 5313 Bipartite Graph(二分图染色+01背包水过)

Problem Description 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

hdu 5313 Bipartite Graph(dfs染色 或者 并查集)

Problem Description 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

HDU 5135 Little Zu Chongzhi&#39;s Triangles(状态压缩dp+Vector)

这道题是水题,当时直接贪心就过了. 多阶段决策,其实应该用dp,他人的代码使用Vector进行预处理. #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<vector> using namespace std; int n, a[12]; double dp[1<<12]; double cal(int a, int b, i

BZOJ 2878([Noi2012]迷失游乐园-树形DP+环加外向树+期望DP+vector的erase)

2878: [Noi2012]迷失游乐园 Time Limit: 10 Sec  Memory Limit: 512 MBSec  Special Judge Submit: 319  Solved: 223 [Submit][Status] Description 放假了,小Z觉得呆在家里特别无聊,于是决定一个人去游乐园玩.进入游乐园后,小Z看了看游乐园的地图,发现可以将游乐园抽象成有n个景点.m条道路的无向连通图,且该图中至多有一个环(即m只可能等于n或者n-1).小Z现在所在的大门也正好是

hdu5313 Bipartite Graph

题意描述: 有一个n个点m条边的二分图,通过加边使得这张图变成一个边数最多的完全二分图. 最多能够新加多少条边. 注意重边是不允许的. 解题思路: 1.先对二分图染色(dfs),统计二分图中每个连通块(注意:这个二分图并不一定连通)中黑色和白色的数量(黑.白是相对的,不同连通块之间的黑.白没有联系): 2.从每个连通块中选出黑或白的数量作为整个二分图中白色的那组,根据题目描述我们只需要找到怎样分组才能使完全二分图的边数最多(显然对于总共n个顶点的 二分图,如果两组顶点各一半时能使总的边数最大),

HDU 5313 Bipartite Graph (二分图着色,dp) TLE!!!

题意:Soda有一个$n$个点$m$条边的二分图, 他想要通过加边使得这张图变成一个边数最多的完全二分图. 于是他想要知道他最多能够新加多少条边. 注意重边是不允许的. 思路:二分图着色这个简单,主要是dp,还有时间限制.我觉得应该是找出所有连通分量,每个连通分量两边的点数存起来,后面统一进行DP.但是!!时间仅有1s,而且还100个例子,就是说大概要在100万的复杂度才行,可是有1万个点,当m=5000时,这就不行. 我不懂这道题如何下手才能保证一定正确且不超时,应该优化得很厉害~ 贴一个我认

hdu5354 Bipartite Graph

分治+并查集.假设要求[L,mid]的答案,那么很明显,如果一条边的两个端点都>mid的话或者一个端点>mid一个端点<L,说明询问[L,mid]这个区间中任何一点时候,这一条边都是连接的,否则的话递归下去处理.[mid+1,R]同理. 一个图不是二分图的话说明存在奇环,奇环可以用并查集处理.这里的并查集不使用路径压缩而使用按轶合并.并查集的还原操作可以用一个栈记录每次合并时的具体操作,然后按序还原即可. 代码 1 #include<cstdio> 2 #include<