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 (uv) 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,BiN
  • 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

Copy
6 5
1 2
2 3
3 4
4 5
5 6

Sample Output 1

Copy
4

If we add edges as shown below, four edges can be added, and no more.


Sample Input 2

Copy
5 5
1 2
2 3
3 1
5 4
5 1

Sample Output 2

Copy
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(二分图)的更多相关文章

  1. Atcoder CODE FESTIVAL 2017 qual B C - 3 Steps 二分图

    题目链接 题意 给定一个无向图,\(n\)个点,\(m\)条边(\(n,m\leq 1e5\)). 重复如下操作: 选择相异的两点u,v满足从点u出发走三条边恰好能到达点v.在这样的u,v点对之间添一 ...

  2. [AtCoder Code Festival 2017 QualB C/At3574] 3 Steps - 二分图染色,结论

    给你一个n个点m条边的无向图,进行以下操作 如果存在两个点u和v,使得从u走三步能恰好到达v,那么在u和v之间连接一条边 重复这个操作直到不能再连接新的边,问最后有多少条边? n, m <= 1 ...

  3. CODE FESTIVAL 2017 qual B C - 3 Steps【二分图】

    CODE FESTIVAL 2017 qual B C - 3 Steps 题意:给定一个n个结点m条边的无向图,若两点间走三步可以到,那么两点间可以直接连一条边,已经有边的不能连,问一共最多能连多少 ...

  4. POJ2195 Going Home[费用流|二分图最大权匹配]

    Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22088   Accepted: 11155 Desc ...

  5. POJ 2195 Going Home (带权二分图匹配)

    POJ 2195 Going Home (带权二分图匹配) Description On a grid map there are n little men and n houses. In each ...

  6. 2018.06.27Going Home(二分图匹配)

    Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 24716 Accepted: 12383 Descript ...

  7. 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 ...

  8. POJ 2195 Going Home 【二分图最小权值匹配】

    传送门:http://poj.org/problem?id=2195 Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  9. HDU 1533:Going Home(KM算法求二分图最小权匹配)

    http://acm.hdu.edu.cn/showproblem.php?pid=1533 Going Home Problem Description   On a grid map there ...

随机推荐

  1. 「六」创建一个带 weblogic 服务的基础镜像

    Weblogic Weblogic 简单介绍以及其在 Docker 环境下的特殊应用 WebLogic是美国Oracle公司出品的一个application server确切的说是一个基于JAVAEE ...

  2. 怎样改动SharePoint管理中心的语言

    在安装了语言包之后,创建站点集的时候,就能够选择语言了. 可是SharePoint管理中心的语言没有变.这个时候.怎么才干让管理中心也使用新的语言呢? 能够依照下面方法. 首先去https://msd ...

  3. python abstractmethod 对象比较

    from functools import total_ordering from abc import ABCMeta,abstractmethod @total_ordering class Sh ...

  4. Django——如何处理请求(URL配置和视图)

    URLconfig—— 为了绑定视图函数和URL,我们使用URLconf. URLconf 就像是 Django 所支撑网站的目录. 它的本质是 URL 模式以及要为该 URL 模式调用的视图函数之间 ...

  5. Refactoring之——代码的坏味道(一)过长方法

    1 代码的坏味道 重构一书中提到了22种代码的坏味道,大致可以分为几类. 识别代码的坏味道,有助于发现代码的潜在问题,从而可以有的放矢的修改现有代码,使之不断完善. 1.1 Bloaters(臭鲱,暂 ...

  6. 02-3设置第一启动项--进入BIOS设置USB方式启动

    设置USB方式启动 https://zhinan.sogou.com/guide/detail/?id=1610014869 如何设置电脑从U盘启动呢?今天小编教大家如何进入BIOS设置USB方式启动 ...

  7. VUE入门实例,模版组件用法

    这里每一个例子可以直接拷进body运行. 本系列为学习记录,并非大神教学案例. 仅仅整理用法,至于VUE的原理,设计模式等等暂不讨论,文中如有不对,还请大家帮忙指正,万分感激. 下一篇会写父子组件交互 ...

  8. 几段表单处理的JQuery代码(转)

    1 只接受数字输入 $("#uAge").keydown(function(event) { // 允许退格和删除键 if ( event.keyCode == 46 || eve ...

  9. C语言基础(14)-递归

    一. 递归的定义 函数可以调用自己,这就叫函数的递归. 先序递归和后序递归 #include <stdio.h> void test(int n); void test1(int n); ...

  10. python C example:encode mp3 code

    #include <stdio.h> #include <stdlib.h> #include <lame.h> #define INBUFSIZE 4096 #d ...