文档章节

codeforces CF209C Trails and Glades 欧拉回路

o
 osc_1ee7cxmx
发布于 2018/08/06 16:24
字数 785
阅读 18
收藏 0

$ \rightarrow $戳我进CF原题

<center>C. Trails and Glades</center>


<center>time limit per test: 4 seconds</center> <center>memory limit per test: 256 megabytes</center> <center>input: standard input</center> <center>output: standard output</center>


  Vasya went for a walk in the park. The park has $ n $ glades, numbered from $ 1 $ to $ n $ . There are $ m $ trails between the glades. The trails are numbered from $ 1 $ to $ m $ , where the $ i $ -th trail connects glades $ x_i $ and $ y_i $. The numbers of the connected glades may be the same $ (x_i=y_i) $, which means that a trail connects a glade to itself. Also, two glades may have several non-intersecting trails between them.   Vasya is on glade $ 1 $ ,he wants to walk on all trails of the park exactly once, so that he can eventually return to glade $ 1 $ . Unfortunately, Vasya does not know whether this walk is possible or not. Help Vasya, determine whether the walk is possible or not. If such walk is impossible, find the minimum number of trails the authorities need to add to the park in order to make the described walk possible.   Vasya can shift from one trail to another one only on glades. He can move on the trails in both directions. If Vasya started going on the trail that connects glades $ a $ and $ a $ , from glade $ a $ , then he must finish this trail on glade $ b $ .  

Input

The first line contains two integers $ n $ and $ m (1 \le n \le 10^6;0 \le m \le 10^6) $ — the number of glades in the park and the number of trails in the park, respectively. Next m lines specify the trails. The $ i $-th line specifies the $ i $-th trail as two space-separated numbers, $ x_i,y_i (1\le x_i,y_i \le n) $ — the numbers of the glades connected by this trail.  

Output

Print the single integer — the answer to the problem. If Vasya's walk is possible without adding extra trails, print $ 0 $, otherwise print the minimum number of trails the authorities need to add to the park in order to make Vasya's walk possible.  

Examples

Input1

 3 3
 1 2
 2 3
 3 1

output1

 0 

input2

 2 5
 1 1
 1 2
 1 2
 2 2
 1 2

output2

 1 

 

Note

In the first test case the described walk is possible without building extra trails. For example, let's first go on the first trail, then on the second one, and finally on the third one.   In the second test case the described walk is impossible without adding extra trails. To make the walk possible, it is enough to add one trail, for example, between glades number one and two.

 

题目大意

  • 最少添加多少条边,是无向图有欧拉回路。

  • $ n,m \le 10^6 $

 

题解

  • 求出每个点的度数

  • 奇数点需要连一条新边

  • 仅有偶度数点的连通块需要连两条新边

  • 特判没有奇数点只有偶数点且只有一个连通块(已经成为欧拉回路)的情况,新边数为 $ 0 $

  • 答案为上面统计的新边数 $ / 2 $

  • 坑点:

  • $ 1. $ 没有自环的孤立点不用连边,没有边相连的点可以不用到达

  • $ 2. $ 但是$ 1 $ 必须到达,所以特定要访问 $ 1 $

  • $ 3. $ 注意,自环不能直接删掉,孤立点的自环也要访问

 

代码

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
int n,m,ans;
int deg[1000005],cnt;
vector<int>e[1000005];
bool vis[1000005],flag;
void dfs(int u){
	vis[u]=1;
	if(deg[u]&1) flag=1; 
	for(int i=0;i<e[u].size();++i)
		if(!vis[e[u][i]]) dfs(e[u][i]);
}
bool pot[1000005],p1;
int main(){
	scanf("%d %d",&n,&m);
	for(int u,v,i=1;i<=m;++i){
		scanf("%d %d",&u,&v);
		pot[u]=pot[v]=1;
		if(u==v) continue;
		++deg[u]; ++deg[v];
		e[u].push_back(v);
		e[v].push_back(u);
	}
	pot[1]=1;
	for(int i=1;i<=n;++i){
		if(deg[i]&1){ 
			++ans;
			p1=1;
		} else if(!vis[i]&&pot[i]){
			++cnt;
			flag=0;
			dfs(i);
			if(!flag) ans+=2;
		}
	}
	if(!p1&&cnt==1) ans-=2;
	printf("%d",ans/2);
	return 0;
}
o
粉丝 0
博文 500
码字总数 0
作品 0
私信 提问
加载中
请先登录后再评论。

暂无文章

pycurl libcurl link-time ssl backend (nss)

pip uninstall pycurlecho 'pycurl==7.19.5.1 --global-option="--with-nss"' > requires.pypip install -r requires.py...

小红手
35分钟前
17
0
计算机网络性能衡量

1、速率 单位时间(s)内传输信息(bit)量 单位:KB/s, MB/s, Gb/s K = 10^3 ,M = 10^6, G=10^9 一般表示的是理想的传输速率 2、带宽 计算机网络中的带宽和通信等领域的带宽概念不一样,计算机网...

osc_np3y0rbq
35分钟前
3
0
互联网掀起农家乐,巨头上演AI掘金战

配图来自Canva **前有网易、阿里AI养猪,后有腾讯AI养鹅,互联网大佬们纷纷玩起了“农家乐”,互联网的生意在尖端技术的引领之下频频跨界,巨头之间的较量也从线上延伸至线下。**自古“民以食...

osc_5cok9i01
37分钟前
9
0
原来!我在4年前就开始体验雾游戏了!

前有云游戏后有雾游戏,游戏的方式看来起来越来越多种多样。那么“震撼业界”的雾游戏到底是什么来头?它依靠什么改变游戏界?它的原理又是什么? 本月月初,著名的日本游戏杂志《Fami通》表...

osc_j34n26zn
38分钟前
11
0
活动预告|田溯宁与你相约GSMA Thrive·万物生晖,分享5G风口下的创新与投资洞察

在万物互联的时代背景下,5G+AI+IoT的技术变革与融合,正在引发一场深刻的全产业创新与变革。5G技术创新、行业应用及投资机遇已成为科技行业所瞩目的焦点。 6月30日,宽带资本董事长田溯宁将...

osc_0qnrwmy3
39分钟前
7
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部