D. Weird journey
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Little boy Igor wants to become a traveller. At first, he decided to visit all the cities of his motherland — Uzhlyandia.

It is widely known that Uzhlyandia has n cities connected with m bidirectional roads. Also, there are no two roads in the country that connect the same pair of cities, but roads starting and ending in the same city can exist. Igor wants to plan his journey beforehand. Boy thinks a path is good if the path goes over m - 2 roads twice, and over the other 2 exactly once. The good path can start and finish in any city of Uzhlyandia.

Now he wants to know how many different good paths are in Uzhlyandia. Two paths are considered different if the sets of roads the paths goes over exactly once differ. Help Igor — calculate the number of good paths.

Input

The first line contains two integers n, m (1 ≤ n, m ≤ 106) — the number of cities and roads in Uzhlyandia, respectively.

Each of the next m lines contains two integers u and v (1 ≤ u, v ≤ n) that mean that there is road between cities u and v.

It is guaranteed that no road will be given in the input twice. That also means that for every city there is no more than one road that connects the city to itself.

Output

Print out the only integer — the number of good paths in Uzhlyandia.

Examples
Input
5 4
1 2
1 3
1 4
1 5
Output
6
Input
5 3
1 2
2 3
4 5
Output
0
Input
2 2
1 1
1 2
Output
1
Note

In first sample test case the good paths are:

  • 2 → 1 → 3 → 1 → 4 → 1 → 5,
  • 2 → 1 → 3 → 1 → 5 → 1 → 4,
  • 2 → 1 → 4 → 1 → 5 → 1 → 3,
  • 3 → 1 → 2 → 1 → 4 → 1 → 5,
  • 3 → 1 → 2 → 1 → 5 → 1 → 4,
  • 4 → 1 → 2 → 1 → 3 → 1 → 5.

There are good paths that are same with displayed above, because the sets of roads they pass over once are same:

  • 2 → 1 → 4 → 1 → 3 → 1 → 5,
  • 2 → 1 → 5 → 1 → 3 → 1 → 4,
  • 2 → 1 → 5 → 1 → 4 → 1 → 3,
  • 3 → 1 → 4 → 1 → 2 → 1 → 5,
  • 3 → 1 → 5 → 1 → 2 → 1 → 4,
  • 4 → 1 → 3 → 1 → 2 → 1 → 5,
  • and all the paths in the other direction.

Thus, the answer is 6.

In the second test case, Igor simply can not walk by all the roads.

In the third case, Igor walks once over every road.

题意:n个点 m条路 问你有多少条好路(有自环的情况)

好路就是走过所有点 m-2条路走2遍 剩下两条路走一遍

我开始有点懵 后来发现只要是联通的  那么就可以满足这个条件  问题是选两条路走一遍

自环t1 其他 t2

1》两条都不是自环的  那么这两条一定有公共点

for(int i=1;i<=n;i++)ans=ans+a[i]*(a[i]-1)/2;

2》一条有自环  一条不是 t1*t2;

3》都是自环  t1*(t1-1)/2

开始edge[N]开小了  无限wa  懵比了

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<map>
#include<cstdlib>
#include<vector>
#include<set>
#include<queue>
#include<cstring>
#include<string.h>
#include<algorithm>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int N=+;
int m,n;
int flag[N];
int head[N];
int vis[N];
ll a[N],b[N];
int cnt=;
void init(){
memset(vis,,sizeof(vis));
cnt=;
memset(head,-,sizeof(head));
}
struct node{
int to,next;
}edge[N*+];
void add(int u,int v){
edge[cnt].to=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void DFS(int x){
vis[x]=;
for(int i=head[x];i!=-;i=edge[i].next){
int v=edge[i].to;
if(vis[v]==){
DFS(v);
}
}
}
int main(){
scanf("%d%d",&n,&m);
init();
ll t1=;
ll t2=;
int u,v;
memset(a,,sizeof(a));
for(int i=;i<m;i++){
scanf("%d%d",&u,&v);
b[u]=b[v]=;
if(u!=v){add(u,v);add(v,u);t1++;a[u]++;a[v]++;}
else
t2++;
}
DFS(u);
for(int i=;i<=n;i++){
if(vis[i]==&&b[i]){
cout<<<<endl;
return ;
}
}
ll ans=;
ans=ans+t1*t2;
ans=ans+(t2*(t2-))/;
for(int i=;i<=n;i++)
ans=ans+(a[i]*(a[i]-))/;
printf("%I64d\n",ans);
}

CodeForces - 789D Weird journey的更多相关文章

  1. Codeforces 789D Weird journey - 欧拉路 - 图论

    Little boy Igor wants to become a traveller. At first, he decided to visit all the cities of his mot ...

  2. CodeForces - 788B Weird journey 欧拉路

    题意:给定n个点,m条边,问能否找到多少条符合条件的路径.需要满足的条件:1.经过m-2条边两次,剩下两条边1次  2.任何两条路的终点和起点不能相同. 欧拉路的条件:存在两个或者0个奇度顶点. 思路 ...

  3. CodeForces 788B - Weird journey [ 分类讨论 ] [ 欧拉通路 ]

    题意: 给出无向图. good way : 仅有两条边只经过一次,余下边全经过两次的路 问你共有多少条不同的good way. 两条good way不同仅当它们所经过的边的集合中至少有一条不同 (很关 ...

  4. 【cf789D】Weird journey(欧拉路、计数)

    cf788B/789D. Weird journey 题意 n个点m条边无重边有自环无向图,问有多少种路径可以经过m-2条边两次,其它两条边1次.边集不同的路径就是不同的. 题解 将所有非自环的边变成 ...

  5. Codeforces Round #407 (Div. 1) B. Weird journey —— dfs + 图

    题目链接:http://codeforces.com/problemset/problem/788/B B. Weird journey time limit per test 2 seconds m ...

  6. codeforces 407 div1 B题(Weird journey)

    codeforces 407 div1 B题(Weird journey) 传送门 题意: 给出一张图,n个点m条路径,一条好的路径定义为只有2条路径经过1次,m-2条路径经过2次,图中存在自环.问满 ...

  7. Codeforces Round #407 (Div. 2) D. Weird journey(欧拉路)

    D. Weird journey time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  8. 【codeforces 789D】Weird journey

    [题目链接]:http://codeforces.com/problemset/problem/789/D [题意] 给你n个点,m条边; 可能会有自环 问你有没有经过某两条边各一次,然后剩余m-2条 ...

  9. 【题解】Weird journey Codeforces 788B 欧拉路

    传送门:http://codeforces.com/contest/788/problem/B 好题!好题! 首先图不连通的时候肯定答案是0,我们下面讨论图联通的情况 首先考虑,如果我们每条边都经过两 ...

随机推荐

  1. 获取Google地图位置坐标并嵌入到网页

    有时候做网页的时候,可能需要一个地图显示,可能会用到Google地图,所以就分享一下get到的新技能.在网上查资料的时候有这种方式 但是我没做成功,所以找了其他的方式. 首先,打开Google地图,查 ...

  2. HTML CSS, JavaScript 计算器

    效果图: 代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...

  3. Node.js概述

    Node.js最重要的特性:通过单线程实现异步处理环境 Node.js解决的问题: Node.js修改了客户端连接服务器端的连接方法,不需要为每个客户端连接创建一个新的线程,而是为每个客户端连接触发一 ...

  4. Vue项目优化首屏加载速度

    Vue项目部署上线后经常会发现首屏加载的速度特别慢:那么有那写能做的简单优化呢 一.路由的懒加载 路由懒加载也就是 把不同路由对应的组件分割成不同的代码块,然后当路由被访问的时候才加载对应组件. 结合 ...

  5. Python培训时长多久可以学会?马哥教育9年经验之谈

    在Python成为一门炙手可热的语言之后,很多人也开始准备向这个行业发展.技术入行也就是培训和自学两条路,各有优劣,不过培训因为学习比较系统比较快也受到不少人欢迎. 今天我就来给大家分享一下Pytho ...

  6. .net 学习视频

    http://www.iqiyi.com/a_19rrh9jx9p.html http://www.cnblogs.com/aarond/p/SQLDispatcher.html  --读写分离 ht ...

  7. [nodejs]在mac环境下如何将node更新至最新?

    在mac下安装angular-cli时,报出较多错误.初步怀疑是因为node环境版本过低导致. 在mac下,需要执行如下几步将node更新至最新版本,也可以更新到指定版本 1. sudo npm ca ...

  8. 洛谷——P1896 [SCOI2005]互不侵犯

    P1896 [SCOI2005]互不侵犯 状压DP入门题 状压DP一般需要与处理状态是否合法,节省时间 设定状态dp[i][j][k]表示第i行第j个状态选择国王数为k的方案数 $dp[i][j][n ...

  9. 【codeforces 768E】Game of Stones

    [题目链接]:http://codeforces.com/contest/768/problem/E [题意] NIM游戏的变种; 要求每一堆石头一次拿了x个之后,下一次就不能一次拿x个了; 问你结果 ...

  10. 洛谷 P1851 好朋友

    题目背景 小可可和所有其他同学的手腕上都戴有一个射频识别序列号码牌,这样老师就可以方便的计算出他们的人数.很多同学都有一个“好朋友” .如果 A 的序列号的约数之和恰好等于B 的序列号,那么 A的好朋 ...