Codeforces Round #407 (Div. 1) B. Weird journey —— dfs + 图
来源:互联网 发布:东方财富国际证券知乎 编辑:程序博客网 时间:2024/06/06 03:01
题目链接:http://codeforces.com/problemset/problem/788/B
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.
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.
Print out the only integer — the number of good paths in Uzhlyandia.
5 41 21 31 41 5
6
5 31 22 34 5
0
2 21 11 2
1
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;}
- Codeforces Round #407 (Div. 1) B. Weird journey —— dfs + 图
- Codeforces Round #407 (Div. 1) B. Weird journey
- Codeforces Round #407 (Div. 1) B. Weird journey
- [欧拉路] Codeforces Round #407 (Div. 1) 788B. Weird journey
- Codeforces Round #407 (Div. 2) D Weird journey [dfs]
- Codeforces Round #407 (Div. 2) D. Weird journey
- Codeforces Round #407 (Div. 2)-D. Weird journey
- CodeForces 788B Weird journey
- Codeforces Round #402 (Div. 2) B. Weird Rounding
- Codeforces Round #402 (Div. 2) B. Weird Rounding
- Codeforces Round #402 (Div. 2)B. Weird Rounding【暴力枚举】
- Codeforces Round #402 (Div. 2) B. Weird Rounding(爆搜)
- Codeforces Round #402 (Div. 2) B. Weird Rounding
- codeforces 788B Weird journey (欧拉路)
- codeforces 788B Weird journey (欧拉路)
- 【dfs找环】Codeforces Beta Round #80 (Div. 1 Only)——B. Cthulhu
- Codeforces Round #374 (Div. 2) C. Journey(dfs+dp)
- Codeforces Round #428 (Div. 2) Journey(数学期望+dfs)
- 《加密与解密》笔记四(三)
- Hibernate--一对一的关联映射
- 信源编码作业五
- Nginx入门以及开源博客Tale的部署
- HDU1533:Going Home(最小费用流)
- Codeforces Round #407 (Div. 1) B. Weird journey —— dfs + 图
- mysql基本总结
- C++中,关于#include<***.h>和#include"***.h"的区别
- JAVA 基础知识总结——三大特性
- 类和接口
- 火狐浏览器 selenium 'geckodriver' executable needs to be in PATH
- 脑咋了?脑炸了
- bzoj 4869: [Shoi2017]相逢是问候 数论+线段树
- 安卓recycleView添加viewheader