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
考虑枚举每个子串开头的位置,然后答案转化成询问这个位置之后 哪个位置的前缀和 - 这位置的前缀和 最大(当然是已经把绝对值和正负的情况处理好了,可以发现按奇偶,这个序列只有两种情况),只需要预处理这两 ...
随机推荐
- bzero和memset
一. bzero和memset函数 1. bzero已不建议使用 原型:extern void bzero(void *s, int n); 2.memset void *memset(void *s ...
- inotify+rsync实现实时同步(附解决crontab中无法执行python脚本的问题)
1.准备环境 # 系统支持的话,下面的目录就会存在 ls /proc/sys/fs/inotify/ rpm -qa inotify-tools yum -y install inotify-tool ...
- spring lifeCycle
Lifecycle接口定义了每个对象的生命周期.如下 public interface Lifecycle { void start(); void stop(); boolean isRunning ...
- shell-异步执行
一.启动后台子任务 在执行命令后加&操作符,表示将命令放在子shell中异步执行.可以达到多线程效果.如下, sleep 10 #等待10秒,再继续下一操作 sleep 10 & #当 ...
- LINUX___的常用几个快捷键
linux下:ctrl-c 发送 SIGINT 信号给前台进程组中的所有进程.常用于终止正在运行的程序.ctrl-z 发送 SIGTSTP 信号给前台进程组中的所有进程,常用于挂起一个进程.ctrl- ...
- CODECHEF Nov. Challenge 2014 Chef & Churu
@(XSY)[分塊] Hint: 題目原文是英文的, 寫得很難看, 因此翻譯為中文. Input Format First Line is the size of the array i.e. \(N ...
- Eclipse工程中Java Build Path中的JDK版本和Java Compiler Compiler compliance level的区别(转)
在这里记录一下在eclipse中比较容易搞混淆和设置错误的地方.如下图所示的功能: 最精准的解释如下: Build Path是运行时环境 Compiler是编译时环境 假设,你的代码用到泛型,Bu ...
- 邁向IT專家成功之路的三十則鐵律 鐵律二十九 IT人富足之道-信仰
天地自然的循環法則,讓每一個人都必須經歷生.老.病.死.喜.怒.哀.樂,然而至始至終無盡的煩惱總是遠多於快樂,因此筆者深信每一個人在一生當中,都必須要有適合自己的正確信仰,他可以在你無法以物質力量來解 ...
- WinKawaks使用技巧
1 关于载入游戏:把游戏的zip文件放到roms目录下.注意不要随意修改文件名!也不要让文件名有中文!例如:"[游戏roms][neogeo]mslugx.zip"改成" ...
- spring security开发步骤
1.web.xml中加载spring ,spring security 2.spring security配置文件中配置好.... 3.自己写一个myFilter代替原有的FilterSecurity ...