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

题意:Soda有一个$n$个点$m$条边的二分图, 他想要通过加边使得这张图变成一个边数最多的完全二分图. 于是他想要知道他最多能够新加多少条边. 注意重边是不允许的.

思路:二分图着色这个简单,主要是dp,还有时间限制。我觉得应该是找出所有连通分量,每个连通分量两边的点数存起来,后面统一进行DP。但是!!时间仅有1s,而且还100个例子,就是说大概要在100万的复杂度才行,可是有1万个点,当m=5000时,这就不行。

  我不懂这道题如何下手才能保证一定正确且不超时,应该优化得很厉害~

贴一个我认为对的TLE代码:

 1 #include <bits/stdc++.h>
 2 #define LL long long
 3 #define pii pair<int,int>
 4 #define INF 0x7f7f7f7f
 5 using namespace std;
 6 const int N=10100;
 7 vector<int> vect[N];
 8 int col[N];
 9 int set1, set2;
10
11 void color(int u, int c)
12 {
13     col[u]=c;
14     if(col[u]==1) set1++;//统计两边的数量
15     else        set2++;
16
17     for(int i=0; i<vect[u].size(); i++)
18     {
19         int t=vect[u][i];
20         if(!col[t])   //没色
21             color(t, 3-col[u] );
22     }
23 }
24
25
26 bitset<N/2> dp[N];
27 int s1[N/2],s2[N/2];
28 int cal(int n, int m)
29 {
30     if(m==n/2*(n-n/2) ) return 0;
31     for(int i=0; i<=n; i++) vect[i].clear(), dp[i].reset();
32
33     memset(col, 0, sizeof(col));
34     int ans=0, k=0;
35     for(int i=1; i<=n; i++)
36     {
37         if(!col[i])
38         {
39             set1=set2=0;
40             color(i, 1);
41
42             s1[k]=set1;//每个连通分量两边的点数
43             s2[k++]=set2;
44         }
45     }
46
47     if(k==1)    return s1[0] * s2[0] - m;//连通图
48     dp[0][0]=1;
49     for(int i=0; i<k; i++)//在这进行DP,尽量使得两边的点数平衡
50     {
51         ans=0;
52         set1=s1[i];
53         set2=s2[i];
54
55         for(int j=n/2; j>=set1; j--)
56             if( dp[i][j-set1] )    dp[i+1][j]=1,ans=max(ans,j);
57
58         for(int j=n/2; j>=set2; j--)
59             if( dp[i][j-set2] )    dp[i+1][j]=1,ans=max(ans,j);
60     }
61     return ans*(n-ans)-m;
62 }
63
64
65 int main()
66 {
67     //freopen("input.txt", "r", stdin);
68     int n, m, t, p, q, a, b;
69     cin>>t;
70
71     while(t--)
72     {
73         scanf("%d%d",&n,&m);
74         for(int i=0; i<m; i++)
75         {
76             scanf("%d%d",&a,&b);
77             vect[a].push_back(b);
78             vect[b].push_back(a);
79         }
80         printf("%d\n",cal(n, m));
81     }
82     return 0;
83 }

AC代码

时间: 2024-07-29 03:25:04

HDU 5313 Bipartite Graph (二分图着色,dp) TLE!!!的相关文章

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 完全二分图 深搜 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(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(dfs+背包)

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

[hdu 5354] Bipartite Graph 分治 并查集

题意 给定一张 $n$ 个点, $m$ 条边的无向图. 问删去每个点后, 原图是不是二分图. $1 \le n, m \le {10} ^ 5$ . 分析 一个图是二分图 $\Leftrightarrow$ 图中不存在奇环. 判定一个图是不是二分图, 可以使用并查集, 多维护一个当前点与父亲的关系的量 bond . 删除每一个点, 我们有两种维度: 区间加法, 区间减法. 这里考虑区间加法, 即考虑分治. 由于要支持撤销, 所以使用按秩合并的并查集. 注意按照大小合并... 按深度合并会 TLE

hdu5313Bipartite Graph(二分图染色+DP(bitset优化))

题意:给n个点m条边,问最多可以添加几条边使图为完全二分图 分析:如果二分图没有限制,看到是两边分别为n/2个点和n-n/2个点的最优但是可   能出现大于此点的情况,比如n=4,m=3,边为1 2,1 3,1 4.此时完全二分图边最多为3,所以要求得二分图左边或者右边可达到的离n/2最近的点数是多少为最优解,于是采用染色分别求出各个联通快的2种颜色的各个点数,然后用推出能达到的点数,这里dp[i]的下一个状态是dp[i+1]+d[i+1][0]和dp[i+1][1](dp[i]为前i个二分图的

2015多校第6场 HDU 5354 Bipartite Graph CDQ,并查集

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5354 题意:求删去每个点后图是否存在奇环(n,m<=1e5) 解法:很经典的套路,和这题一样:http://www.cnblogs.com/spfa/p/7358672.html CDQ套并查集. 这题最开始是看了南神的代码才懂的,http://blog.csdn.net/hdu2014/article/details/47450709    因为要判断每一个点,而且一旦一个点之外的几个点形成了奇环