Codeforces Round #407 (Div. 2) D. Weird journey(欧拉路)
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.
【分析】给你一个双向路径图(可能不连通且存在自环,同样的边不会出现两次),n个节点,m条边,要求划出一条路径,使得其中(m-2)条边每条边走两次,另外两条边每条边走一次,问有多少种不同的路径。
这题咋一看,像不像欧拉路,很像啊,欧拉路是划出一条路径经过所有的边一次且仅一次,而这个是选出(m-2)条边来走两次。那我们不妨将这(m-2)条边再建一条,比如,
u<-->v,那我们再建一条u<-->v。那么如果修改后的图能跑出一条欧拉路的话,那么就符合题意了,所以我们就枚举这些边。由于(m-2)可能比较大,那么我们就枚举这个2,即不增建的边。
我们判定欧拉路的依据是奇数度数节点只有两个或者没有。那么我们枚举每一条边,将此边不增,其他的每条边都扩增一条,那么此时除了这条边连接的两个端点(u!=v)
以外,其他的所有节点的度数都是偶数,这两个节点的度数都是奇数。等会,我们还差一条边,,,现在已经有两个节点度数是奇数了,那么我们要拆的下一条边就必须是与这两个节点相关的(可以自己想想为什么)那么这条边的贡献就是这两个节点所连得边的和-1了。还有就是如果你拆了某个自环的边,也是可以的。还有种情况就是你一开始选的是一个自环的边,那么你下一条要拆的随便那条边都行。
#include <cstdio>
#include <map>
#include <algorithm>
#include <vector>
#include <iostream>
#include <set>
#include <queue>
#include <string>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
typedef pair<int,int>pii;
typedef long long LL;
const int N=1e6+;
const int mod=1e9+;
int n,m,s,k,t,cnt;
bool vis[N];
vector<int>edg[N];
int loop=;
LL ans=;
int in[N];
struct man{
int u,v;
}a[N];
void dfs(int u){
vis[u]=;
for(int i=;i<edg[u].size();i++){
int v=edg[u][i];
if(!vis[v])dfs(v);
}
}
int main()
{
int x,y,w,l,r;
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++){
scanf("%d%d",&x,&y);
a[i].u=x;a[i].v=y;
if(x!=y){
edg[x].push_back(y);
edg[y].push_back(x);
}
else loop++;
in[x]++;in[y]++;
}
for(int i=;i<=n;i++)
if(in[i]!=){
dfs(i);
break;
}
for(int i=;i<=n;i++){
if(!vis[i]&&in[i]!=){
puts("");
return ;
}
}
for(int i=;i<=m;i++){
x=a[i].u;
y=a[i].v;
if(x!=y){
ans+=(edg[x].size()-+edg[y].size()-+loop);
}
else {
ans+=(m-);
}
}
printf("%lld\n",ans/);
return ;
}
Codeforces Round #407 (Div. 2) D. Weird journey(欧拉路)的更多相关文章
- 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 ...
- 【分类讨论】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 ...
- Codeforces 789D Weird journey - 欧拉路 - 图论
Little boy Igor wants to become a traveller. At first, he decided to visit all the cities of his mot ...
- 【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 ...
随机推荐
- Scala环境安装设置
Scala语言可以安装在任何类UNIX或Windows系统.要安装Scala,必须先安装Java1.5或更高版本安装在计算机上. Windows上安装Scala: 步骤(1):JAVA设置: 首先,必 ...
- 【BZOJ5010】【FJOI2017】矩阵填数 [状压DP]
矩阵填数 Time Limit: 10 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description 给定一个 h*w 的矩阵,矩阵的行 ...
- 【Foreign】咏叹 [模拟退火]
咏叹 Time Limit: 100 Sec Memory Limit: 256 MB Description 有n根木棍,第i根长度为ai.你要贴着墙围出一个矩形区域,木棍围成的矩形边缘必须平行或 ...
- 牛客网刷题(纯java题型 31~60题)
牛客网刷题(纯java题型 31~60题) 重写Override应该满足"三同一大一小"三同:方法名相同,参数列表相同,返回值相同或者子类的返回值是父类的子类(这一点是经过验证的) ...
- GPU硬件加速
现代浏览器大都可以利用GPU来加速页面渲染.每个人都痴迷于60桢每秒的顺滑动画.在GPU的众多特性之中,它可以存储一定数量的纹理(一个矩形的像素点集合)并且高效地操作这些纹理(比如进行特定的移动.缩放 ...
- jsp之jstl核心标签库
JSTL核心标签库技术 1. JSTL介绍 在JSP页面中即可书写html,也可以书写Java代码,导致页面混乱,维护,修改,升级难度加大,于是国际上不同的公司在实际应用中,根据页面的需求将Java代 ...
- 自定义ToolBar
一.Toolbar的简介 Toolbar 是 android 5.0 引入的一个新控件,Toolbar出现之前,我们很多时候都是使用ActionBar以及ActionActivity实现顶部导航栏的, ...
- linux内存占用查看
查看内存使用情况 free free -m //显示单位为:兆 查看占用内存最高的5个进程ps aux | sort -k4nr | head -n 5 查看占用CPU最高的5个进程ps aux | ...
- kernel cmdline
從 lk 傳送到 kerel 的 cmdline 會放在開機後的 adb /proc/cmdline 開到 android 後,又會被讀出來 /system/core/init/util.cpp 27 ...
- python基础===创建大量对象是节省内存方法
问题: 你的程序要创建大量(可能上百万) 的对象,导致占用很大的内存. 解决方案: 对于主要是用来当成简单的数据结构的类而言,你可以通过给类添加__slots__属性来极大的减少实例所占的内存.比如: ...