Codeforces Round #407 (Div. 1) B. Weird journey —— dfs + 图
题目链接:http://codeforces.com/problemset/problem/788/B
2 seconds
256 megabytes
standard input
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.
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 4
1 2
1 3
1 4
1 5
6
5 3
1 2
2 3
4 5
0
2 2
1 1
1 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. 2) D. Weird journey(欧拉路)
D. Weird journey time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- 【分类讨论】Codeforces Round #407 (Div. 2) D. Weird journey
考虑这个二元组中有一者是自环,则必然合法. 考虑这两条边都不是自环,如果它们不相邻,则不合法,否则合法. 坑的情况是,如果它是一张完整的图+一些离散的点,则会有解,不要因为图不连通,就误判成无解. # ...
- Codeforces Round #407 (Div. 1)
人傻不会B 写了C正解结果因为数组开小最后RE了 疯狂掉分 AC:A Rank:392 Rating: 2191-92->2099 A. Functions again 题目大意:给定一个长度为 ...
- Codeforces Round #407 (Div. 2)
来自FallDream的博客,未经允许,请勿转载,谢谢. ------------------------------------------------------ A.Anastasia and ...
- Codeforces Round #407 (Div. 2) D,E
图论 D. Weird journey time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 【DFS】Codeforces Round #402 (Div. 2) B. Weird Rounding
暴搜 #include<cstdio> #include<algorithm> using namespace std; int n,K,Div=1,a[21],m,ans=1 ...
- 【Codeforces Round #428 (Div. 2) C】Journey
[Link]:http://codeforces.com/contest/839/problem/C [Description] 给一棵树,每当你到一个点x的时候,你进入x的另外一每一个出度的概率都是 ...
- Codeforces Round #407 (Div. 2)A B C 水 暴力 最大子序列和
A. Anastasia and pebbles time limit per test 1 second memory limit per test 256 megabytes input stan ...
- 【预处理】Codeforces Round #407 (Div. 2) C. Functions again
考虑枚举每个子串开头的位置,然后答案转化成询问这个位置之后 哪个位置的前缀和 - 这位置的前缀和 最大(当然是已经把绝对值和正负的情况处理好了,可以发现按奇偶,这个序列只有两种情况),只需要预处理这两 ...
随机推荐
- codeforces 979E(dp套dp)
题意: 有n个点,编号为1~n.有的点颜色是黑色,有的点颜色是白色,有的点的颜色待涂.你还可以连一些边,但这些边一定是从小编号连到大编号的点. 对于一个确定的图,我们去统计有多少条路径满足“该路径经过 ...
- IntelliJ IDEA常用统一设置2-Inspections检查设置(Linux/Mac/Windows)
Inspections检查设置功能,能检查一些普通问题,比如代码风格等等.当然,这个虽然没有CheckStyle这些插件专业,但也是另一种选择. 官方参考:https://www.jetbrains. ...
- Java中没有C#的out关键字,但可以通过数组实现类似的效果
其实传递的就是数组的指针,里面的每一项的值还是那块内存,所以能直接操作里面的值.如果单纯传指定的值,那么里面操作的就是新的一块内存块. 用数组实现的效果如下: class B{ String cnt= ...
- webpack+vue起步
本文基于vue1.x 基于vue2.x&webpack2.x请移步至 Vue2.x踩坑与总结Webpack2.x踩坑与总结 记得第一次知道Vue.js是在勾三股四大大的微博,那时候他开始翻译v ...
- 高仿QQ6.0側滑菜单之滑动优化(二)
好了,昨天已经实现了高仿QQ6.0的側滑大致框架.如有兴趣.能够去看下仿QQ6.0側滑之ViewDragHelper的使用(一) 可是之前的实现.仅仅是简单的能够显示和隐藏左側的菜单,可是特别生硬,并 ...
- .net 反射访问私有变量和私有方法 如何创建C# Closure ? C# 批量生成随机密码,必须包含数字和字母,并用加密算法加密 C#中的foreach和yield 数组为什么可以使用linq查询 C#中的 具名参数 和 可选参数 显示实现接口 异步CTP(Async CTP)为什么那样工作? C#多线程基础,适合新手了解 C#加快Bitmap的访问速度 C#实现对图片文件的压
以下为本次实践代码: using System; using System.Collections.Generic; using System.ComponentModel; using System ...
- PCB常用单位转换 mil 英尺
PCB常用单位转换 mil 英尺 相关常用单位 1mil = 0.0254mm 100mil = 2.54mm 1英寸 = 1000mil = 2.54cm 1英尺 = 12英寸 ...
- python缺省参数
def test(a,b=22): result=a+b print("resuLt=%d"%result) test(33,33) #缺省参数的意思就是,函数在有参数的情况下,调 ...
- 系统安全-PAM
Pluggable Authentication Modules(可插入验证模块,简称PAM) Linux-PAM(Pluggable Authentication Modules for Linux ...
- 【ZZ】Visual C++ 6.0 精简安装版(支持VA、ICC 等等安装)
(2012-04-22 08:10:10) 标签: it 分类: 软件_Software Visual C++ 6.0 精简安装版(支持VA.ICC 等等安装) 2012-04-16 21:07 想找 ...