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 M A1 B1 A2 B2 : AM BM
Output
Find the maximum possible number of edges that can be added.
Sample Input 1
6 5 1 2 2 3 3 4 4 5 5 6
Sample Output 1
4
Sample Input 2
5 5 1 2 2 3 3 1 5 4 5 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++;
else
r++;
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);
else
printf("%lld\n",n*(n-1)/2-m);
return 0;
}