3 Steps(二分图)
C - 3 Steps
Time limit : 2sec / Memory limit : 256MB
Score : 500 points
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
If we add edges as shown below, four edges can be added, and no more.

Sample Input 2
5 5
1 2
2 3
3 1
5 4
5 1
Sample Output 2
5
Five edges can be added, for example, as follows:
- Add an edge connecting Vertex 5 and Vertex 3.
- Add an edge connecting Vertex 5 and Vertex 2.
- Add an edge connecting Vertex 4 and Vertex 1.
- Add an edge connecting Vertex 4 and Vertex 2.
- Add an edge connecting Vertex 4 and Vertex 3.
//Atcoder的题目还是有新意啊,可以收获不少
题意: n 个点 m 条边, 组成一个无向连通图,重复操作, 如果 a 点到 b 点距离为 3 ,并且没有连回 a ,就添加一条 a - b 的边。没有自环,问最多能添加几条边。
分析可知,如有图有奇数环,必然可加成完全图
如果图是二分图,则会变成完全二分图,
否则最终变为完全图
二分图dfs染色即可
#include <bits/stdc++.h>
using namespace std;
# define LL long long
# define pr pair
# define mkp make_pair
# define lowbit(x) ((x)&(-x))
# define PI acos(-1.0)
# define INF 0x3f3f3f3f3f3f3f3f
# define eps 1e-
# define MOD inline int scan() {
int x=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-; ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-''; ch=getchar();}
return x*f;
}
inline void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
# define MX
/**************************/
int n,m;
vector<int> G[MX];
int clo[MX]; bool check()
{
bool ok=;
for (int i=;i<=n;i++)
{
if (G[i].size()>=)
{
if (ok) return ;
ok=;
}
}
return ;
} int dfs(int p,int s,int pre)
{
clo[p] = s%+;
for (int i=;i<G[p].size();i++)
{
int v = G[p][i];
if (v==pre) continue;
if (!clo[v])
{
if (!dfs(v,s+,p))
return ;
}
else if(clo[v]==clo[p]) return ;
}
return ;
} int bipartite()
{
memset(clo,,sizeof(clo));
if (!dfs(,,-)) return ;
return ;
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for (int i=;i<=n;i++) G[i].clear(); for (int i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
G[x].push_back(y);
G[y].push_back(x);
}
if (!check())
printf("0\n");
else if (!bipartite())
printf("%lld\n",(LL)n*(n-)/-m);
else
{
LL b=,w=;
for (int i=;i<=n;i++)
{
if (clo[i]==) b++;
else w++;
}
printf("%lld\n",b*w-m);
}
}
return ;
}
3 Steps(二分图)的更多相关文章
- Atcoder CODE FESTIVAL 2017 qual B C - 3 Steps 二分图
题目链接 题意 给定一个无向图,\(n\)个点,\(m\)条边(\(n,m\leq 1e5\)). 重复如下操作: 选择相异的两点u,v满足从点u出发走三条边恰好能到达点v.在这样的u,v点对之间添一 ...
- [AtCoder Code Festival 2017 QualB C/At3574] 3 Steps - 二分图染色,结论
给你一个n个点m条边的无向图,进行以下操作 如果存在两个点u和v,使得从u走三步能恰好到达v,那么在u和v之间连接一条边 重复这个操作直到不能再连接新的边,问最后有多少条边? n, m <= 1 ...
- CODE FESTIVAL 2017 qual B C - 3 Steps【二分图】
CODE FESTIVAL 2017 qual B C - 3 Steps 题意:给定一个n个结点m条边的无向图,若两点间走三步可以到,那么两点间可以直接连一条边,已经有边的不能连,问一共最多能连多少 ...
- POJ2195 Going Home[费用流|二分图最大权匹配]
Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22088 Accepted: 11155 Desc ...
- POJ 2195 Going Home (带权二分图匹配)
POJ 2195 Going Home (带权二分图匹配) Description On a grid map there are n little men and n houses. In each ...
- 2018.06.27Going Home(二分图匹配)
Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 24716 Accepted: 12383 Descript ...
- POJ2195 Going Home (最小费最大流||二分图最大权匹配) 2017-02-12 12:14 131人阅读 评论(0) 收藏
Going Home Description On a grid map there are n little men and n houses. In each unit time, every l ...
- POJ 2195 Going Home 【二分图最小权值匹配】
传送门:http://poj.org/problem?id=2195 Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- HDU 1533:Going Home(KM算法求二分图最小权匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=1533 Going Home Problem Description On a grid map there ...
随机推荐
- 【Java】Java_14 循环结构
循环结构 当型:当P条件成立时(T),反复执行A,直到P为“假”时才停止循环. 直到型:先执行A, 再判断P,若为T,再执行A,如此反复,直到P为F. 1.While循环 while循环的基本格式和流 ...
- HBase总结(十八)Hbase rowkey设计一
hbase所谓的三维有序存储的三维是指:rowkey(行主键),column key(columnFamily+qualifier),timestamp(时间戳)三部分组成的三维有序存储. 1.row ...
- iOS开发-XCode常用快捷键整理
前言:如果我们能够掌握并巧妙地使用快捷键,可以大大加快我们的工作效率,这个对经常使用快捷键的人们来说,应该很容易理解.因此我们需要做的是,针对于自己经常使用的快捷键去进行记忆.我不会推荐你们去把所有的 ...
- Pushlet后台推送
1.Pushlet 是一个开源的 Comet 框架,Pushlet 使用了观察者模型:客户端发送请求,订阅感兴趣的事件:服务器端为每个客户端分配一个会话 ID 作为标记,事件源会把新产生的事件以多播的 ...
- CentOS 6.3下部署LVS(NAT)+keepalived实现高性能高可用负载均衡(转)
一.简介 VS/NAT原理图: 二.系统环境 实验拓扑: 系统平台:CentOS 6.3 Kernel:2.6.32-279.el6.i686 LVS版本:ipvsadm-1.26 keepalive ...
- Google I/O 2014 大会总结 Android开发新方向
昨天晚上,Google I/O 2014大会召开,会上主要展示了下面几个部分的创新内容: Android L 操作系统 首先是界面,谷歌又一次设计了一套 UI 规范.并称之为"Materia ...
- 459. Repeated Substring Pattern【easy】
459. Repeated Substring Pattern[easy] Given a non-empty string check if it can be constructed by tak ...
- jvm参数设置和性能调优
1.Java虚拟机运行时的数据区 2.常用的内存区域调节参数 -Xms:初始堆大小,默认为物理内存的1/64(<1GB):默认(MinHeapFreeRatio参数可以调整)空余堆内存小于40% ...
- Java获取系统默认浏览器打开链接
package com.ylx.test; public class DesktopBrowers { public static void main(String[] args) { // 判断当前 ...
- Mysql 变量讲解
set语句的学习: 使用select定义用户变量的实践将如下语句改成select的形式: set @VAR=(select sum(amount) from penalties);我的修改: sele ...