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

这道题我们的做法是二分图染色 如果原图是个二分图 那么同色距离为偶 所以答案就是 黑*白-m

如果不是 那么我们发现 图上存在环且为奇环 那么环上任意一对点都可以连边

这样可以不断拓出来使得整个图每对点都可以连边 这样答案就是n*(n-1)/2-m辣

#include<cstdio>
#include<cstring>
#include<algorithm>
int read(){
int ans=,f=,c=getchar();
while(c<''||c>''){if(c=='-') f=-; c=getchar();}
while(c>=''&&c<=''){ans=ans*+(c-''); c=getchar();}
return ans*f;
}
const int M=2e5+;
int n,m,s[],color[M],vis[M];
int first[M],cnt;
struct node{int to,next;}e[*M];
void ins(int a,int b){e[++cnt]=(node){b,first[a]}; first[a]=cnt;}
void insert(int a,int b){ins(a,b); ins(b,a);}
bool ly=false;
void dfs(int x){
vis[x]=; s[color[x]]++;
for(int i=first[x];i;i=e[i].next){
int now=e[i].to;
if(!vis[now]) color[now]=-color[x],dfs(now);
if(vis[now]&&color[now]==color[x]){ly=true; return ;}
}
}
int main(){
int x,y;
n=read(); m=read();
for(int i=;i<=m;i++) x=read(),y=read(),insert(x,y);
dfs();
if(ly) printf("%lld\n",1LL*n*(n-)/-m);
else printf("%lld\n",1LL*s[]*s[]-m);
return ;
}

CODE FESTIVAL 2017 qual B C - 3 Steps的更多相关文章

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

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

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

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

  3. CODE FESTIVAL 2017 qual B C 3 Steps(补题)

    总感觉这题是个题意杀,理解错题目了,看了好久才发现题目意思:操作是让,只要两点没有直接相连,而且只要有一条路的距离3,就可以把这两点连接起来. 按照题解中讲的,可以把图分为二分图和非二分图来解.不过题 ...

  4. CODE FESTIVAL 2017 qual B B - Problem Set【水题,stl map】

    CODE FESTIVAL 2017 qual B B - Problem Set 确实水题,但当时没想到map,用sort后逐个比较解决的,感觉麻烦些,虽然效率高很多.map确实好写点. 用map: ...

  5. CODE FESTIVAL 2017 qual B

    昨晚因为有点事就去忙了,没打后悔啊 A - XXFESTIVAL Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem ...

  6. 【AtCoder】CODE FESTIVAL 2017 qual B

    最近不知道为啥被安利了饥荒,但是不能再玩物丧志了,不能颓了 饥荒真好玩 A - XXFESTIVAL CCFESTIVAL #include <bits/stdc++.h> #define ...

  7. CODE FESTIVAL 2017 qual B 题解

    失踪人口回归.撒花\^o^/ 说来真是惭愧,NOI之后就没怎么刷过题,就写了几道集训队作业题,打了几场比赛还烂的不行,atcoder至今是蓝名=.= 以后还是多更一些博客吧,我可不想清华集训的时候就退 ...

  8. 【题解】Popping Balls AtCoder Code Festival 2017 qual B E 组合计数

    蒟蒻__stdcall终于更新博客辣~ 一下午+一晚上=一道计数题QAQ 为什么计数题都这么玄学啊QAQ Prelude 题目链接:这里是传送门= ̄ω ̄= 下面我将分几个步骤讲一下这个题的做法,大家不 ...

  9. Atcoder CODE FESTIVAL 2017 qual C D - Yet Another Palindrome Partitioning 回文串划分

    题目链接 题意 给定一个字符串(长度\(\leq 2e5\)),将其划分成尽量少的段,使得每段内重新排列后可以成为一个回文串. 题解 分析 每段内重新排列后是一个回文串\(\rightarrow\)该 ...

随机推荐

  1. “今日校园” App 用户体验分析

    一.背景 为进一步提升信息化应用水平,更好的服务师生,南通大学智慧校园移动端APP“今日校园”定于11月5日正式上线运行.登陆APP可浏览学校新闻.校园生活.各部门微信公众号等内容,查看校内通知.校内 ...

  2. Windows网络编程系列教程之四:Select模型

    讲一下套接字模式和套接字I/O模型的区别.先说明一下,只针对Winsock,如果你要骨头里挑鸡蛋把UNIX下的套接字概念来往这里套,那就不关我的事. 套接字模式:阻塞套接字和非阻塞套接字.或者叫同步套 ...

  3. Jenkins系列-Jenkins邮件通知

    一.安装邮件插件 由于Jenkins自带的邮件功能比较鸡肋,因此这里推荐安装专门的邮件插件,不过下面也会顺带介绍如何配置Jenkins自带的邮件功能作用. 可以通过系统管理→管理插件→可选插件,选择E ...

  4. Jekyll 使用 Rouge 主题

    今日发现我的 Github Pages 中的代码并没有高亮,看了一下代码发现,原来的没有设置 css 样式的原因,我使用的代码高亮器是 rouge highlighter: rouge Rouge 是 ...

  5. Jira & SVN & Chrome extensions

    Jira & SVN & Chrome extensions Plugins SVN & Jira Plugins ok selector bug document.query ...

  6. 第46天:setInterval与setTimeout的区别

    js的setTimeout方法用处比较多,通常用在页面刷新了.延迟执行了等等.今天对js的setTimeout方法做一个系统地总结. setInterval与setTimeout的区别 说道setTi ...

  7. SPD各模块总结

    一.用户角色绑定节点 1.库存操作员.库存主管.验货操作员:绑定任一节点 2.采购操作员.公药操作员:只能绑定药库节点 3.退库操作员.药品申领员:绑定药库以外的节点 二.采购计划模块 1.采购计划的 ...

  8. poj3074-Sodoku

    解数独. 分析 考虑如何把数独解合法的条件转化为经典的01精确覆盖: 每个格子只能填一个数,1-9 每一列刚好填了1-9 每一行刚好填了1-9 每个九宫格刚好填了1-9 也就是说,每个格子,列,行,九 ...

  9. bzoj3782上学路线

    题意:从n*m网格图的左下角走到右上角(n,m<=10^10),有t个坐标不能经过(t<=200),只能向上向右走,问有多少种不同的走法,对p取模, p只有两种取值,1000003(质数) ...

  10. 【题解】NOIP2015推销员

    ……普及组的题目都做不出来……(:´д`)ゞ……再这样下去要退役了啊…… 不过不管怎样感觉这题还是蛮好的,也要记录一下下~ 我们注意到数据的范围,n 是 1e5, 又有 1e5组询问,暴力大概是 \( ...