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 ...
随机推荐
- Web服务器父与子 Apache和Tomcat区别
http://developer.51cto.com/art/201007/210894.htm 熟悉三国的朋友都知道曹操,曹操有二十五个儿子,其中最得曹操宠爱的是曹丕.曹植.曹彰三个,曹丕性格阴冷, ...
- Linux 信号详解四(pause,alarm)
pause函数 --将进程置为可中断睡眠状态,然后它调用内核函数schedule(),使linux进程调度器找到另一个进程来运行. --pause使调用者进程挂起,知道一个信号被捕获. alarm函数 ...
- HTTP04--CDN知识
一.CDN用途及概念 目的: CDN是内容分布网路(Content Delivery Network)的简称,目的是将网站内容发布到最接近用户的边缘,使用户就近获取内容,提高相应速度. 使用机制: 目 ...
- visual studio 2012 的制作ActiveX、打包和发布
开发环境是Vs 2012 Framework 4.0 源码和制作工具在文章最下边 一. ActiveX控件Demo 新建一个Window窗体控件库项目 在自动生成的UserControl1页面上添加 ...
- Oracle基础语法
--表create table tb_myTable( mname vardhar2(30), pwd varchar2(30)); --存储过程create or replace procedure ...
- Retro 2013
现在的team里每个迭代都会做一次retro,回顾这两周的情况,有哪些做得好的地方,有哪些做得不足的地方,并制定出一系列action,以期望在下一个迭代中解决这些问题.我觉得这种形式挺不错.因此今年的 ...
- 10-xargs 简明笔记
从标准你输入获取内容创建和执行命令 xargs [options] 选项 -n 数字,分组 示例 x ...
- restFull常用注解
@GET.@POST.@PUT.@DELETE.@HEAD您可以使用它们来绑定根资源或子资源内的 Java 方法与 HTTP 请求方法.HTTP GET 请求被映射到由 @GET 注释的方法,以此类推 ...
- Ubuntu 14.04 安装最新稳定版Nginx 1.6.0
如果已经安装,请先卸载sudo apt-get remove nginx最新的稳定版Nginx 1.6.0在ubuntuupdates ppa库中提供,网址http://www.ubuntuupdat ...
- 让apache后端显示真实客户端IP
公司是nginx做代理,后端的web服务用的是apache,然后我现在要分析日志,但是,我的apache日志上显示的是代理服务器的ip地址,不是客户的真实IP 所以这里我需要修改一下,让apache的 ...