HDU 5313 Bipartite Graph(二分图染色+01背包水过)
来源:互联网 发布:淘宝质量好的女鞋店 编辑:程序博客网 时间:2024/05/21 08:45
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.
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 integersn and m , (2≤n≤10000,0≤m≤100000) .
Each of the nextm 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.
The first line contains two integers
Each of the next
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
24 21 22 34 41 21 42 33 4
Sample Output
20
Source
BestCoder 1st Anniversary ($)
Recommend
hujie | We have carefully selected several similar problems for you: 5315 5314 5312 5311 5310
大致题意:
有n个点,m条边的二分图(可能不连通),问最多还能加多少条边变成完全二分图
思路:
显然每一连通块,都染成两种颜色,最后要尽量使两种颜色总数相同解才最优
显然有两种决策,不是染白就是染黑,01背包
dp[i][val]表示前i个连通块能染成同一色点数<=val的最大值
显然dp[scc][all/2]是最优解
#pragma comment(linker, "/STACK:1024000000,1024000000")#include <iostream>#include <cstring>#include <cmath>#include <queue>#include <stack>#include <list>#include <map>#include <set>#include <sstream>#include <string>#include <vector>#include <cstdio>#include <ctime>#include <bitset>#include <algorithm>#define SZ(x) ((int)(x).size())#define ALL(v) (v).begin(), (v).end()#define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)#define REP(i,n) for ( int i=1; i<=int(n); i++ )using namespace std;typedef long long ll;#define X first#define Y secondtypedef pair<ll,ll> pii;const int N = 10000+100;const int M = 100000+1000;struct Edge{ int v,nxt; Edge(int v = 0,int nxt = 0):v(v),nxt(nxt){}}es[M*2];int n,m;int ecnt;int head[N];inline void add_edge(int u,int v){ es[ecnt] = Edge(v,head[u]); head[u] = ecnt++; es[ecnt] = Edge(u,head[v]); head[v] = ecnt++;}int col[N];int cnt[N][2];int top;int sum = 0;void dfs(int u,int fa){ col[u] = !col[fa]; cnt[top][col[u]]++; for(int i = head[u];~i;i = es[i].nxt){ int v = es[i].v; if(v == fa || col[v] != -1) continue; dfs(v,u); }}void ini(){ REP(i,n) head[i] = col[i] = -1,cnt[i][0] = cnt[i][1] = 0; col[0] = top = sum = ecnt = 0;}int dp[2][N];int main(){ int T; cin>>T; while(T--){ scanf("%d%d",&n,&m); ini(); REP(i,m){ int u,v; scanf("%d%d",&u,&v); add_edge(u,v); } for(int i = n; i>= 1;i--){ if(col[i] != -1) continue; top++; dfs(i,0); if(cnt[top][0] == 0 || cnt[top][1] == 0) { cnt[top][0] = cnt[top][1] = 0; top--; } else { sum += cnt[top][0],sum += cnt[top][1]; } } int nd = n-sum; for(int i = 0;i <= sum/2;i++) dp[0][i] = 0; REP(i,top){ for(int j = 0; j <= sum/2; j++){ dp[i&1][j] = -1; if(j-cnt[i][0] >= 0 && dp[(i-1)&1][j-cnt[i][0]] != -1) dp[i&1][j] = dp[(i-1)&1][j-cnt[i][0]]+cnt[i][0]; if(j-cnt[i][1] >= 0 && dp[(i-1)&1][j-cnt[i][1]] != -1) { dp[i&1][j] = max(dp[(i-1)&1][j-cnt[i][1]]+cnt[i][1],dp[i&1][j]); } } int minn,maxx = sum-dp[top&1][sum/2]; int t = min(nd,maxx-dp[top&1][sum/2]); minn = dp[top&1][sum/2]+t; nd -= t; if(nd) minn += nd/2, maxx += nd/2 + (nd&1); printf("%d\n",minn*maxx-m); }}
0 0
- HDU 5313 Bipartite Graph(二分图染色+01背包水过)
- [HDU 5313] Bipartite Graph 二分图染色+分组背包
- 【HDU】5313 Bipartite Graph【二分图+背包】
- hdu 5313 Bipartite Graph(二分图染色+dp+bitset优化)
- 【染色】 HDU 5313 Bipartite Graph
- hdu5313 Bipartite Graph(二分图染色+dp+位运算)
- HDU 5313 Bipartite Graph(二分图 + bitset)
- hdu 5313 Bipartite Graph 贪心+bfs染色
- hdu 5313 Bipartite Graph 完全二分图 深搜 bitset应用
- hdu 5313 Bipartite Graph
- hdu-5313 Bipartite Graph
- hdu 5313 Bipartite Graph
- HDU 5313 Bipartite Graph
- HDU 5313 Bipartite Graph(bitset + DP)
- [HDU 5354] Bipartite Graph
- Python 数据结构与算法——二分图(bipartite graph)
- fzu 2141 Sub-Bipartite Graph 贪心 二分图构建
- CodeForces 624C Graph and String(二分图染色)
- 使用 C# 开发智能手机软件:推箱子(七)
- 关于使用ArcMap10.0为几何网络建立流向
- 使用 C# 开发智能手机软件:推箱子(八)
- Deep Learning for Nature Language Processing --- 第七讲
- 使用 C# 开发智能手机软件:推箱子(九)
- HDU 5313 Bipartite Graph(二分图染色+01背包水过)
- Mysql5.6 Master Slave 配置
- perl特殊符号
- error: #929: incorrect use of va_arg
- 软件发布!DOTA2统计学
- 函数式编程初探
- HDU 5285 wyh2000 and pupil (DFS染色判二分图 + 贪心)
- 《Effective Java》——异常
- 设计模式_桥梁模式