CodeForces 209C Trails and Glades
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 xi and yi. The numbers of the connected glades may be the same (xi = yi), 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 b, from glade a, then he must finish this trail on glade b.
The first line contains two integers n and m (1 ≤ n ≤ 106; 0 ≤ m ≤ 106) — 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, xi, yi(1 ≤ xi, yi ≤ n) — the numbers of the glades connected by this trail.
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.
3 3
1 2
2 3
3 1
0
2 5
1 1
1 2
1 2
2 2
1 2
1
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.
并查集判联通块。
如果只有一个联通块,答案为奇度数节点数/2,
如果有多个联通块,答案为奇度数节点数/2 + 没有奇度数点的联通块个数
由于1点必须在欧拉回路中,1点默认要设为存在。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define LL long long
using namespace std;
const int mx[]={,,,-,};
const int my[]={,,,,-};
const int INF=1e9;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m;
int fa[mxn];
int find(int x){
if(fa[x]==x)return x;
return fa[x]=find(fa[x]);
}
void init(int x){
for(int i=;i<=x;i++)fa[i]=i;
}
int deg[mxn];
int cnt[mxn],ans=;
bool flag[mxn];
bool vis[mxn];
int cct=;
int main(){
n=read();m=read();
int i,j,u,v;
init(n);
vis[]=;
for(i=;i<=m;i++){
u=read();v=read();
if(u!=v){
deg[u]++;
deg[v]++;
vis[u]=;vis[v]=;
u=find(u);v=find(v);
fa[u]=v;
}
else vis[u]=;
}
for(i=;i<=n;i++){
if(!vis[i])continue;
if(find(i) ==i)cct++;
}
for(i=;i<=n;i++){
if(!vis[i] || deg[i]%==)continue;
int x=find(i);
cnt[x]++;
}
int res=;
for(i=;i<=n;i++){
if(!vis[i])continue;
if(find(i)==i){
if(!cnt[i])ans++;
else res+=cnt[i];
}
}
if(cct==)printf("%d\n",res/);
else printf("%d\n",ans+res/);
return ;
}
CodeForces 209C Trails and Glades的更多相关文章
- Codeforces.209C.Trails and Glades(构造 欧拉回路)
题目链接 \(Description\) 给定一张\(n\)个点\(m\)条边的无向图,允许有自环重边.求最少加多少条边后,其存在从\(1\)出发最后回到\(1\)的欧拉回路. 注意,欧拉回路是指要经 ...
- Codeforces 209 C. Trails and Glades
Vasya went for a walk in the park. The park has n glades, numbered from 1 to n. There are m trails b ...
- CF209C Trails and Glades
题目链接 题意 有一个\(n\)个点\(m\)条边的无向图(可能有重边和自环)(不一定联通).问最少添加多少条边,使得可以从\(1\)号点出发,沿着每条边走一遍之后回到\(1\)号点. 思路 其实就是 ...
- CF209C Trails and Glades(欧拉路)
题意 最少添加多少条边,使无向图有欧拉回路. n,m≤106 题解 求出每个点的度数 奇度数点需要连一条新边 仅有偶度数点的连通块需要连两条新边 答案为上面统计的新边数 / 2 注意:此题默认以1为起 ...
- codeforces 459E
codeforces 459E E. Pashmak and Graph time limit per test 1 second memory limit per test 256 megabyte ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
随机推荐
- SQL Server Management Studio无法记住密码
用sa账户登录sql server 2008,勾选了“记住密码”,但重新登录时,SQL Server Management Studio无法记住密码. 后来发现,在重新登录时,登录名显示的并非是s ...
- react实现的tab切换组件
我有点想要吐槽,因为用原生的js实现起来挺简单的一个小东西,改用react来写却花了我不少时间,也许react的写法只有在复杂的web应用中才能体现出它的优势吧!不过吐槽归吐槽,对react这种优雅的 ...
- WebHeaderCollection 类
https://msdn.microsoft.com/zh-cn/library/system.net.webheadercollection(v=VS.95).aspx /// <summar ...
- 柯尔莫可洛夫-斯米洛夫检验(Kolmogorov–Smirnov test,K-S test)
柯尔莫哥洛夫-斯米尔诺夫检验(Колмогоров-Смирнов检验)基于累计分布函数,用以检验两个经验分布是否不同或一个经验分布与另一个理想分布是否不同. 在进行cumulative probab ...
- mysql中导入txt文件
1 windows 下 mysql导入txt文件(使用mysql的workbench) load data local infile 'path' into table table_name fiel ...
- JavaScript学习笔记-new Date() 与 Date() 的区别
var today1 = Date() //返回一个字符串(string),没有getDate等日期对象方法,内容为当前时间 var today2 = new Date() //返回一日期对象,内容为 ...
- 【Python】[函数式编程]高阶函数,返回函数,装饰器,偏函数
函数式编程高阶函数 就是把函数作为参数的函数,这种抽象的编程方式就是函数式编程.--- - -跳过,不是很理解,汗 - ---
- navigationView 的使用和布局文件的绑定
今天项目进行到了细化内容的部分啦- 需要美化侧滑菜单,并且填充数据.在博客上看了好久发现大家的都大同小异 而且很少有提到如何绑定内容各处求助终于在一片博客上发现了蛛丝马迹!!上大神的帖子:blog.c ...
- mybatis Generator配置文件详解
这里按照配置的顺序对配置逐个讲解,更细的内容可以配合中文文档参照. 1. 配置文件头 <?xml version="1.0" encoding="UTF-8&quo ...
- Android NestedScrolling嵌套滑动机制
Android NestedScrolling嵌套滑动机制 最近项目要用到官网的下拉刷新SwipeRefreshLayout,它是个容器,包裹各种控件实现下拉,不像以前自己要实现事件的拦截,都是通过对 ...