首页 技术 正文
技术 2022年11月15日
0 收藏 556 点赞 3,975 浏览 2740 个字

题目链接:http://codeforces.com/problemset/problem/788/B

B. Weird journeytime 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 nm (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.

Examplesinput

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.

题解:

1.必要条件:有边的点构成的图必须是连通的。

2.连通图的性质:从随便一个点出发,当遍历完所有点又回到原点后,每一条边刚好都被经过2次。

3.当不考虑自环时:1次边必须有一个公共点(可以理解为起始边、结束边,但题目并不要求次序)。

当考虑自环时:1次边还可以是:自环边+自环边   or   自环边+除自环边之外的其他边(此种情况只能自环边作为结束边)。

代码如下:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-6;
const int INF = 2e9;
const LL LNF = 9e18;
const int maxn = 1e6+7;int n,m,vis[maxn], d[maxn],used[maxn];
vector<int>g[maxn];void dfs(int u)
{
vis[u] = 1;
for(int i = 0; i<g[u].size(); i++)
if(!vis[g[u][i]])
dfs(g[u][i]);
}int main()
{
int cnt = 0;
int rt; //用于记录有边的点
scanf("%d%d",&n,&m);
int x = 0;
for(int i = 1; i<=m; i++)
{
int u, v;
scanf("%d%d",&u,&v);
used[u] = used[v] = 1;
if(u==v)
{
cnt++;
continue;
}
g[u].push_back(v);
g[v].push_back(u);
rt = u;
} dfs(rt);
int B = 1;
for(int i = 1; i<=n; i++) //判断是否连通
if(used[i] && !vis[i])
B = 0; LL ans = 0;
for(int i = 1; i<=n; i++) //先不考虑自环的情况
if(vis[i])
ans += 1LL*g[i].size()*(g[i].size()-1)/2; ans += 1LL*cnt*(cnt-1)/2 + 1LL*cnt*(m-cnt); //加上自环的情况
cout<< 1LL*B*ans <<endl;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,490
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,905
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,738
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,491
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,129
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,292