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

题意,给个二分图,要求添加最多的边形成一个完全二分图。

我们知道对于一个完全二分图,其边数为 x * y;xy分别为二分图两个集合的点数。那么如果给出的一个二分图,我们知道其可能并不是一个连通图,那么,用深搜,找出若干个小的二分图,只需要,合并若干个二分图,最终使得,整个二分图的两个集合的点数越接近,那么边数最多,加的边数也就最多。用dp的方法,dp[i]表示前i个二分图所能形成的最大点数(不超过n/2),状态转移就是dp[i] = dp[i-1] + x(x为一个二分图的点数。)这里用bitset优化,虽然是O(n*n)的复杂度也能过。还有方法,是用贪心的方法。把所有的二分图随机分入到x集和y集,得到x,y.然后,把x,y直接往n/2来凑,只要o(1)的复杂度,个人觉得这种方法,没有道理吧。但是由于,反例,还真的很难找出来,如果边太少,那么这种贪心方法,x,y必然可以达到接近n/2,如果边很多,又会形成一个大的整体,所以也没多大问题吧。故这样,也能过这题。

#define N 10005
#define M 100005
#define maxn 205
#define MOD 1000000007
int n,m,a,b,c,vis[N],ans[N][2],T;
vector<int> p[N];
bitset<N> sum;
void DFS(int f,int root){
    FI(p[f].size()){
        int v = p[f][i];
        if(vis[v] == -1){
            vis[v] = 1 - vis[f];
            ans[root][vis[v]]++;
            DFS(v,root);
        }
    }
}
int main()
{
    while(S(T)!=EOF)
    {
        while(T--){
            S2(n,m);
            FI(n) p[i+1].clear();
            FI(m){
                scan_d(a);scan_d(b);
                p[a].push_back(b);
                p[b].push_back(a);
            }
            memset(vis,-1,sizeof(vis));
            fill(ans,0);
            for(int i = 0;i<= n;i++) sum[i] = 0;
            sum[0] = 1;
            for(int i= 1;i<=n;i++){
                if(vis[i] == -1){
                    vis[i] = 0;ans[i][vis[i]]++;
                    DFS(i,i);//cout<<ans[i][0]<<" i "<<ans[i][1]<<endl;
                    sum|= ( sum << ans[i][0]) | ( sum << ans[i][1]);
                }
            }
            int maxx = 0;
            for(int i = n;i>=0;i--)
                if(sum[i])
                    maxx = max(maxx,i * (n - i) - m);
            printf("%d\n",maxx);
        }
    }
    return 0;
}

Source

BestCoder 1st Anniversary ($)

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-25 08:26:50

hdu 5313 Bipartite Graph 完全二分图 深搜 bitset应用的相关文章

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(二分图染色+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 5313 Bipartite Graph

题意:给一个二分图,问想让二分图变成完全二分图最多能加多少条边. 解法:图染色+dp+bitset优化.设最终的完全二分图两部分点集为A和B,A中点个数为x,B中点个数为y,边数则为x × y,答案即为x × y - m,那么用dp计算集合A中点个数的可能性.先用图染色计算每个连通分量里两种颜色点的个数,用dp[i][j]表示加入第i个连通分量时A集合中有j个点的可能性,可能为1,不可能为0,设联通分量为p,可以得到转移方程为dp[i][j] = dp[i - 1][j - p[i][0]] |

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

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

hdu 5313 Bipartite Graph(dfs+背包)

题意:n个点m条边,每条边的两个端点已知,求构成完全二分图能加的最多的边数: 参考:http://blog.csdn.net/acmhonor/article/details/47072399 思路:并不是一个二分图的题... n个点构成完全二分图是,两边的点数差值最小时边数最多(X+Y=n,求max(x*y)): 即在给定一些边的情况下,要求的是将点分在两个集合中数量差最小的情况: 先求出每个联通块中的点的情况,考虑孤立点的个数,通过贪心实现剪枝: 非贪心的情况使用背包,背包容量为n/2,推出

HDU 1016 素数环(深搜)

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 25134 Accepted Submission(s): 11222 Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1,

HDU 2553 N皇后问题(深搜DFS)

N皇后问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1757    Accepted Submission(s): 772   Problem Description 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上.你的任务是,对于给定的N,求出

HDU 2102 A计划 (深搜)

Problem Description 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验.魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老.年迈的国王正是心急如焚,告招天下勇士来拯救公主.不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出. 现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示.骑士们一进入时空传输机就会被转到另一层的相对位