【 CodeForces 209C】 【欧拉回路推结论+并查集计算联通分量】 【给定n点m边无向图,可能有自环和重边。 问最少添加多少条边后,使得图存在从点1出发发又回到点1的欧拉回路】
2016-10-05 20:59
561 查看
传送门:C. Trails and Glades
描述:
C. Trails and Glades
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Input
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.
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
input
output
input
output
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边无向图,可能有自环和重边。 问最少添加多少条边后,使得图存在从点1出发又回到点1的欧拉回路(所给的边要保证经过一次)。 n,m ≤ 106
思路:
利用欧拉回路存在的性质,先求出每个连通块内度数是奇数的点的个数。 我们需要加边 以消除所有奇度数点。 然后我们还得把所有连通块连通起来。 可以发现,如果一个连通块包含奇数度数点,那 么就不需要额外加边就能与其他连通块连通 (想象把这些连通块的奇数点收尾相连)。 但如果一个连通块里没有奇度数点,那么必须额外花费1条边才能把它与其他部分连接起 来。 于是答案是:
• 如果全图连通,则答案是奇数度数点的个数/2.
• 否则答案是奇数度数点的个数/2+不含奇数度数点的连通块的个数。
注意:
有一些细节要考虑。 注意孤立点的特殊处理(有没有自环,有的话需要当做联通分量连入图中,没有的话就还是孤立点)。
代码:
#include <bits/stdc++.h>
#define pr(x) cout << #x << "= " << x << " " ;
#define pl(x) cout << #x << "= " << x << endl;
#define ll __int64
using namespace std;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
}
const int N=1e6+10;
int p
,deg
;
int flag
;//是否含有奇数点的联通分量
int vis
;
int n,m;
int Find(int x){
return p[x]==x?x:p[x]=Find(p[x]);
}
void unite(int x,int y){
x=Find(x);
y=Find(y);
if(x!=y)
p[y]=x;
}
int main(){
/* #ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif*/
read(n);read(m);
for(int i=1; i<=n; i++)p[i]=i;
vis[1]=1;
for(int i=1; i<=m; i++){
int u,v;
read(u);read(v);
if(u!=v){
deg[u]++; deg[v]++;
vis[u]++; vis[v]++;
unite(u, v);
}
else vis[u]++;
}
for(int i=1; i<=n; i++){
if(deg[i]&1){
flag[Find(i)]++;
}
}
int tong=0,ans=0,k=0;
for(int i=1; i<=n; i++){
if(vis[i] && Find(i)==i){
if(flag[i])ans+=flag[i];
else tong++;
k++;//计算几个联通分量
}
}
if(k==1)print(ans/2);//全图连通
else print(ans/2+tong);
return 0;
}
描述:
C. Trails and Glades
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Input
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.
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
input
3 3 1 2 2 3 3 1
output
0
input
2 5 1 1 1 2 1 2 2 2 1 2
output
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边无向图,可能有自环和重边。 问最少添加多少条边后,使得图存在从点1出发又回到点1的欧拉回路(所给的边要保证经过一次)。 n,m ≤ 106
思路:
利用欧拉回路存在的性质,先求出每个连通块内度数是奇数的点的个数。 我们需要加边 以消除所有奇度数点。 然后我们还得把所有连通块连通起来。 可以发现,如果一个连通块包含奇数度数点,那 么就不需要额外加边就能与其他连通块连通 (想象把这些连通块的奇数点收尾相连)。 但如果一个连通块里没有奇度数点,那么必须额外花费1条边才能把它与其他部分连接起 来。 于是答案是:
• 如果全图连通,则答案是奇数度数点的个数/2.
• 否则答案是奇数度数点的个数/2+不含奇数度数点的连通块的个数。
注意:
有一些细节要考虑。 注意孤立点的特殊处理(有没有自环,有的话需要当做联通分量连入图中,没有的话就还是孤立点)。
代码:
#include <bits/stdc++.h>
#define pr(x) cout << #x << "= " << x << " " ;
#define pl(x) cout << #x << "= " << x << endl;
#define ll __int64
using namespace std;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
}
const int N=1e6+10;
int p
,deg
;
int flag
;//是否含有奇数点的联通分量
int vis
;
int n,m;
int Find(int x){
return p[x]==x?x:p[x]=Find(p[x]);
}
void unite(int x,int y){
x=Find(x);
y=Find(y);
if(x!=y)
p[y]=x;
}
int main(){
/* #ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif*/
read(n);read(m);
for(int i=1; i<=n; i++)p[i]=i;
vis[1]=1;
for(int i=1; i<=m; i++){
int u,v;
read(u);read(v);
if(u!=v){
deg[u]++; deg[v]++;
vis[u]++; vis[v]++;
unite(u, v);
}
else vis[u]++;
}
for(int i=1; i<=n; i++){
if(deg[i]&1){
flag[Find(i)]++;
}
}
int tong=0,ans=0,k=0;
for(int i=1; i<=n; i++){
if(vis[i] && Find(i)==i){
if(flag[i])ans+=flag[i];
else tong++;
k++;//计算几个联通分量
}
}
if(k==1)print(ans/2);//全图连通
else print(ans/2+tong);
return 0;
}
相关文章推荐
- CodeForces 209C Trails and Glades(欧拉回路判断+并查集计算联通分量)
- uva 10972 添加几条变使得无向图为双联通分量
- 二、给定一个 n 行 m 列的地牢,其中 '.' 表示可以通行的位置,'X' 表示不可通行的障碍,牛牛从 (x0 , y0 ) 位置出发,遍历这个地牢,和一般的游戏所不同的是,他每一步只能按照一些指定的步长遍历地牢,要求每一步都不可以超过地牢的边界,也不能到达障碍上。地牢的出口可能在任意某个可以通行的位置上。牛牛想知道最坏情况下,他需要多少步才可以离开这个地牢。
- 添加最少括号使得给定括号字符串匹配
- 现在有一个城市销售经理,需要从公司出发,去拜访市内的商家,已知他的位置以及商家的位置,但是由于城市道路交通的原因,他只能在左右中选择一个方向,在上下中选择一个方向,现在问他有多少种方案到达商家地址。给定一个地图map及它的长宽n和m,其中1代表经理位置,2代表商家位置,-1代表不能经过的地区,0代表可以经过的地区,请返回方案数,保证一定存在合法路径。保证矩阵的长宽都小于等于10。
- poj 2404 Jogging Trails 求走最少距离使得所有边至少都遍历一次并回到原点(即sum+加上最少多少距离使得原图变成欧拉回路) FLOYD+状态压缩DP
- 添加最少括号使得给定括号字符串匹配
- 计算最少出列多少位同学,使得剩下的同学排成合唱队形(C++)
- 添加最少括号使得给定括号字符串匹配
- 【HDU 4514】【树的直径 dfs或者并查集判断环】【给定一个无向图,图可能是非连通的,如果图中存在环,就输出YES,否则就输出树的直径】
- HDU 1878(1Y) (判断欧拉回路是否存在 奇点个数为0 + 一个联通分量 *【模板】)
- 添加最少括号使得给定括号字符串匹配
- poj 1815 Friendship 最小割+邻接表 求最小点割集(求无向图中最少删除几个顶点使得s和t不联通) 输出顶点是要枚举
- poj 2117 Electricity 求无向图中去掉一个点后最大的联通分支数 无向图有可能不联通 tarjan求割点模板
- 计算单向链表的的长度,有可能有环存在
- 添加最少括号匹配给定括号字符串
- js根据给定的日期计算当月有多少天
- CodeForces 178B1 - Greedy Merchants tarjan求双联通分量+tarjan离线求最近公共祖先
- SDUT 2129树结构练习——判断给定森林中有多少棵树(并查集)