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

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

时间:2015-07-27 12:32:01      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

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 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
 
 
题目大意:跟你一个二分图。n个顶点,m条边。问你再添加多少条边,让这个二分图变成完全二分图。
 
 
解题思路:为了让加的边尽量多,那么就要保证该二分图两侧的顶点个数尽量相同。那么我们统计出各个二分图连通块两边各有多少个顶点,最后用dp来得出两边相差最少时,两边会有多少个顶点。最后套个公式就可以了。本来用二维dp01背包。但是超时。然后就换用了bitset进行优化。
 
 
// bitset::reset
#include <iostream>       // std::cout
#include <string>         // std::string
#include <bitset>         // std::bitset

int main ()
{
  std::bitset<4> foo (std::string("1011"));

  std::cout << foo.reset(1) << ‘\n‘;    // 1001
  std::cout << foo.reset() << ‘\n‘;     // 0000

  return 0;
}






// bitset::operator[]
#include <iostream>       // std::cout
#include <bitset>         // std::bitset

int main ()
{
  std::bitset<4> foo;

  foo[1]=1;             // 0010
  foo[2]=foo[1];        // 0110

  std::cout << "foo: " << foo << ‘\n‘;

  return 0;
}

  

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<bitset>
#include<vector>
using namespace std;
const int maxn=1e4+20;
#define min(a,b) ((a)<(b)?(a):(b))
vector<int>G[maxn];
int color[maxn],d[maxn][3];
int n,m;
void dfs(int cc,int u,int col){
  //  printf("%d---%d----\n",u,cc);
    if(!color[u]){
        color[u]=col;
        d[cc][col+1]++;
    }
    for(int i=0;i<G[u].size();i++){
        int v=G[u][i];
        if(!color[v]){
            dfs(cc,v,-col);
        }
    }
}
int main(){
    int t,a,b;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++){
            scanf("%d%d",&a,&b);
            G[a].push_back(b);
            G[b].push_back(a);
        }
        int c=0;
        for(int i=1;i<=n;i++){
            if(!color[i]){
                c++;
                dfs(c,i,1);
            }
        }
        const int V=5001;
        bitset<V>dp;
        dp.reset();
        dp[0]=1;
        for(int i=1;i<=c;i++){
            dp=(dp<<d[i][0])|(dp<<d[i][2]);
        }
        int k=1;
        for(int i=n/2;i>=1;i--){
            if(dp[i]){
                k=i;
                break;
            }
        }
        printf("%d\n",(n-k)*k-m);
        for(int i=0;i<=n;i++)
            G[i].clear();
        memset(color,0,sizeof(color));
        memset(d,0,sizeof(d));
    }
    return 0;
}


/*
8
8 6
1 2
2 3
2 4
5 6
6 7
6 8
*/

  

 
 
 

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

标签:

原文地址:http://www.cnblogs.com/chengsheng/p/4679746.html

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