3 Steps(二分图初步)
来源:互联网 发布:网页游戏传奇源码下载 编辑:程序博客网 时间:2024/06/05 00:21
Problem Statement
Rng has a connected undirected graph with N vertices. Currently, there are M edges in the graph, and the i-th edge connects Vertices Ai and Bi.
Rng will add new edges to the graph by repeating the following operation:
- Operation: Choose u and v (u≠v) such that Vertex v can be reached by traversing exactly three edges from Vertex u, and add an edge connecting Vertices u and v. It is not allowed to add an edge if there is already an edge connecting Vertices u and v.
Find the maximum possible number of edges that can be added.
Constraints
- 2≤N≤105
- 1≤M≤105
- 1≤Ai,Bi≤N
- The graph has no self-loops or multiple edges.
- The graph is connected.
Input
Input is given from Standard Input in the following format:
N MA1 B1A2 B2:AM BM
Output
Find the maximum possible number of edges that can be added.
Sample Input 1
6 51 22 33 44 55 6
Sample Output 1
4
Sample Input 2
5 51 22 33 15 45 1
Sample Output 2
5
原题是在 AtCoder / CODE FESTIVAL 2017 qual B 中的C题。
题目大意为:
给你一个无向连通图,如果任意两节点之间可以通过三条边(不一定最短)到达,且该两点之间无边,则可以在这两点之间建一条边,问最多能建几条边。
解题思路为:
将该图黑白染色,则只要有路 白1--黑1--白2--黑2 ,则可以连接 白1--黑2,由此可以引入二分图的概念,如果该图是一副二分图,则其中白点为二分图的一点集,黑点为另一点集。而能建立的边都为两集合之间的任意边。而如果该图不是二分图,则意味着除 白--黑 之间能新建边外, 白--白 , 黑--黑 之间亦能新建边,最后导致能建立的边为任意两点之间的边。
综上,如果原图为二分图,则将两集合之间的所有边都连接起来,否则,就将所有点之间的边连接起来。
以下为AC代码:
#include<iostream>#include<cstdio>#include<algorithm>#include<vector>#include<queue>using namespace std;typedef long long ll;ll l=0,r=0;int bj[500010] = {0};/* 用bfs来计算二分图中两点集中点的数量 */ bool playbfs(vector<int> nn[],int n){int t,t1;queue<int> bfs;bfs.push(1);bj[1] = 1;while(!bfs.empty()){int w;int t;w = bfs.front();bfs.pop();/* 判断点为那一集合 */if(bj[w]&1)l++;elser++;for(t=0;t<nn[w].size();t++){if(bj[nn[w][t]] == 0){bj[nn[w][t]] = bj[w]+1;bfs.push(nn[w][t]);}else{/* 如果回路,判断该两点是否在同一集合内 */ if(bj[w]%2 == bj[nn[w][t]]%2)return 0;}}}return 1;}int main(){ll n,m;scanf("%lld%lld",&n,&m);vector<int> nn[n+1];int t;for(t=0;t<m;t++){int a,b;scanf("%d%d",&a,&b);nn[a].push_back(b);nn[b].push_back(a);}if(playbfs(nn,n))printf("%lld\n",l*r-m);elseprintf("%lld\n",n*(n-1)/2-m);return 0;}
- 3 Steps(二分图初步)
- ACM--steps--4.1.3--Pie(二分)
- 【二分图染色】AtCoder CODE FESTIVAL 2017(qual B)C[3 Steps]题解
- 3 Steps
- 二分初步学习
- 二分查找初步
- 整体二分初步
- hdu1997(Steps 2.2.3)
- hdu1584(Steps 4.3.3)
- acm steps 2.1.3
- Steps
- Steps
- Steps
- Steps
- Steps
- Steps
- Steps
- steps
- 运动目标跟踪算法综述
- RL论文
- jsp的九大内置对象
- c语言实现单链表面试题之进阶篇
- Error running app: Instant Run requires 'Tools | Android | Enable ADB integration' to be enabled.
- 3 Steps(二分图初步)
- 合并分支到master上
- freemarker的基本使用和常用标签
- bzoj 2813: 奇妙的Fibonacci 数学+线性筛
- 第三周-项目2
- [bzoj-1047][HAOI2007]理想的正方形 题解
- hbase
- 线程的使用及相关代码
- HttpClient的用法