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. 算法与数据结构实验题 6.4 Summary

    ★实验任务 可怜的 Bibi 丢了好几台手机以后,看谁都像是小偷,他已经在小本本上记 下了他认为的各个地点的小偷数量. 现在我们将 Bibi 的家附近的地形抽象成一棵有根树.每个地点都是树上的 一个节 ...

  2. keydown事件下调用trigger事件执行两次

    $('button[type=button]').on('click',login); //登录 $(document).keydown(function(event){ if(event.keyCo ...

  3. ArrayList遍历(JAVA)

    假如有个ArrayList变量如下: ArrayList<String> list = new ArrayList<String>(); list.add("arra ...

  4. matplotlib中什么是后端

    在很多网上文档和邮件列表中提到了"后端",并且很多初学者会对这个术语迷惑.matplotlib把不同使用情形和输出格式作为目标.一些人用matplotlib在python shel ...

  5. codeforces 987 D. Fair

    D. Fair time limit per test 2 seconds memory limit per test 512 megabytes input standard input outpu ...

  6. tcp协议的六个标识位

    6个标识位: URG 紧急指针,告诉接收TCP模块紧要指针域指着紧要数据. ACK 置1时表示确认号(为合法,为0的时候表示数据段不包含确认信息,确认号被忽略. PSH 置1时请求的数据段在接收方得到 ...

  7. [BZOJ5339] [TJOI2018]教科书般的亵渎

    题目链接 BZOJ题面. 洛谷题面. Solution 随便推一推,可以发现瓶颈在求\(\sum_{i=1}^n i^k\),关于这个可以看看拉格朗日插值法. 复杂度\(O(Tm^2)\). #inc ...

  8. UOJ228:基础数据结构练习题——题解

    http://uoj.ac/problem/228 参考:https://www.cnblogs.com/ljh2000-jump/p/6357583.html 考虑当整个区间的最大值开方==最小值开 ...

  9. 有序数列第K小

    有序数列第K小 题目描述 给出两个长度分别为\(n,m\)的单调非递减数列,求出它们合并后的第\(k\)小值. 输入输出格式 输入格式: 第一行三个数,\(n,m,k\)如题意所述: 第二行\(n\) ...

  10. LG. 1003 铺地毯

    LG. 1003 铺地毯 题意分析 给出平面中地毯的左上角坐标和长宽,然后给出一点(x,y).求此点最上面是哪个地毯的编号,若没被覆盖则输出-1. 将所有地毯的信息存在一个结构体中,由于后埔地毯在上面 ...