ACM: FZU 2112 Tickets - 欧拉回路 - 并查集
Description
You have won a collection of tickets on luxury cruisers. Each ticket can be used only once, but can be used in either direction between the 2 different cities printed on the ticket. Your prize gives you free airfare to any city to start your cruising, and free airfare back home from wherever you finish your cruising.
You love to sail and don't want to waste any of your free tickets. How many additional tickets would you have to buy so that your cruise can use all of your tickets?
Now giving the free tickets you have won. Please compute the smallest number of additional tickets that can be purchased to allow you to use all of your free tickets.
Input
There is one integer T (T≤100) in the first line of the input.
Then T cases, for any case, the first line contains 2 integers n, m (1≤n, m≤100,000). n indicates the identifier of the cities are between 1 and n, inclusive. m indicates the tickets you have won.
Then following m lines, each line contains two integers u and v (1≤u, v≤n), indicates the 2 cities printed on your tickets, respectively.
Output
Sample Input
3
5 3
1 3
1 2
4 5
6 5
1 3
1 2
1 6
1 5
1 4
3 2
1 2
1 2
Sample Output
1
2
0
/*/
题意:
有N个城市,有M张免费的票,票上写了两个城市,每一张票可以任意在两个城市间移动,每张票只能用一次。现在可以买一些票去使用,问怎样购买最少的票将M张免费票用完,可以在任意起点和终点。
一开始秒想到欧拉回路,再加上并查集,但是因为一个小地方,改搓了,一直拖了2个小时中间还A了2题才过这个题目。
将所有的票连起来后,直接查找奇数入度的点,如果奇数入度的点超过2个,把奇数入度的点两两相连剩下两个,作为起点和终点。然后判断并查集,看下有多少个点是走过了,并且是根节点。 加一下就行了。要注意的是题目可能有从同一个 出发回到同一个点,即n==1&&m==1的情况。。。 AC代码:
/*/
#include"algorithm"
#include"iostream"
#include"cstring"
#include"cstdlib"
#include"cstdio"
#include"string"
#include"vector"
#include"stack"
#include"queue"
#include"cmath"
#include"map"
using namespace std;
typedef long long LL ;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define FK(x) cout<<"["<<x<<"]\n"
#define memset(x,y) memset(x,y,sizeof(x))
#define memcpy(x,y) memcpy(x,y,sizeof(x))
#define bigfor(T) for(int qq=1;qq<= T ;qq++) const int MX = 1111111;
int n,m;
bool tem[MX];
int p[MX],indegree[MX]; int find(int x) {
return p[x]==x?x:(p[x]=find(p[x]));
}
void union_town(int u,int v) {
int rt1=find(v);
int rt2=find(u);
if(rt1!=rt2) {
p[rt2]=rt1;
tem[rt1]=1;
}
}
void add(int u,int v) {
indegree[v]++;
indegree[u]++;
union_town(u,v);
} void init() {
for(int i=0; i<=n; i++) p[i]=i;
memset(indegree,0);
memset(tem,0);
} int main() {
int T;
scanf("%d",&T);
bigfor(T) {
scanf("%d%d",&n,&m);
init();
int u,v;
for(int i=0; i<m; i++) {
scanf("%d%d",&u,&v);
add(u,v);
}
if(m<=1||n==1) {
puts("0");
continue;
}
int ans=0;
int town=0;
int kuai=0;
int c[10];
for(int i=1; i<=n; i++) {
if(indegree[i]&1) {
c[++town]=i;
// FK(town);
// FK("i=="<<i);
}
if(town==4) {
ans++;
// FK("p[c[1]]=="<<p[c[1]]<<"p[c[3]]=="<<p[c[3]]);
if(p[c[2]]==p[c[4]]) {
union_town(c[3],c[4]);
} else {
union_town(c[2],c[4]);
c[2]=c[3];
}
town-=2;
}
}
for(int i=1; i<=n; i++)if(p[i]==i&&tem[i]) kuai++;
ans+=kuai-1;
printf("%d\n",ans);
}
return 0;
} /*/ 100
5 3
1 3
1 2
4 5 6 5
1 3
1 2
1 6
1 5
1 4 3 2
1 2
1 2 5 5
1 2
2 3
4 5
5 5
4 4 5 5
1 1
2 2
3 3
4 4
5 5 7 7
1 7
2 7
3 7
4 7
5 7
6 7
1 4 /*/
ACM: FZU 2112 Tickets - 欧拉回路 - 并查集的更多相关文章
- hdu 1116 欧拉回路+并查集
http://acm.hdu.edu.cn/showproblem.php?pid=1116 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...
- HDU 1116 Play on Words(欧拉回路+并查集)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1116 Play on Words Time Limit: 10000/5000 MS (Java/Ot ...
- HDU1878 欧拉回路---(并查集+图论性质)
http://acm.hdu.edu.cn/showproblem.php?pid=1878 欧拉回路 Time Limit: 2000/1000 MS (Java/Others) Memory ...
- hdu 3018 Ant Trip 欧拉回路+并查集
Ant Trip Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- HDU 1116 || POJ 1386 || ZOJ 2016 Play on Words (欧拉回路+并查集)
题目链接 题意 : 有很多门,每个门上有很多磁盘,每个盘上一个单词,必须重新排列磁盘使得每个单词的第一个字母与前一个单词的最后一个字母相同.给你一组单词问能不能排成上述形式. 思路 :把每个单词看成有 ...
- POJ2513——Colored Sticks(Trie树+欧拉回路+并查集)
Colored Sticks DescriptionYou are given a bunch of wooden sticks. Each endpoint of each stick is col ...
- nyist 42 一笔画 (欧拉回路 + 并查集)
nyoj42 分析: 若图G中存在这样一条路径,使得它恰通过G中每条边一次,则称该路径为欧拉路径. 若该路径是一个圈,则称为欧拉(Euler)回路. 具有欧拉回路的图称为欧拉图(简称E图).具有欧拉路 ...
- UVA - 10129 Play on Words(欧拉回路+并查集)
2.解题思路:本题利用欧拉回路存在条件解决.可以将所有的单词看做边,26个字母看做端点,那么本题其实就是问是否存在一条路径,可以到达所有出现过的字符端点.由于本题还要求了两个单词拼在一起的条件是前一个 ...
- poj 1386 Play on Words门上的单词【欧拉回路&&并查集】
题目链接:http://poj.org/problem?id=1386 题目大意:给你若干个字符串,一个单词的尾部和一个单词的头部相同那么这两个单词就可以相连,判断给出的n个单词是否能够一个接着一个全 ...
随机推荐
- No module named django.core
在虚拟环境中将django-admin.py startproject tango_with_django_project替换为django-admin startproject tango_with ...
- js只弹窗一次
<script> var alertmessage="检测到您当前浏览器为IE8或以下版本,建议您使用IE9或以上版本,或者火狐.谷歌浏览器,才能体验到最佳效果" fu ...
- .NET 多个程序配置文件合并到主app.config
.NET 多个程序配置文件合并到主app.config
- 2015.4.19 为什么footer下a的索引值那么大
1.问题demo:为什么footer下a的索引值那么大,index不是查找兄弟级别的元素么?而且还限定了范围在footer下的a的情况下. 解决方法:alert( $("#footer a& ...
- win7挂载VHD文件,模拟多系统并存
挂载vhd是win7 一个很特殊的功能,xp不能支持,一些服务器版的系统 像2008.2008R2这些可能也是支持的,只是没有测试过. 提前的准备: Win7 wim 镜像文件 Imagex.exe ...
- mysql安装配置
MySQL 是最流行的关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle公司. MySQL所使用的SQL语言是用于访问数据库的最常用标准化语言. MySQL由于其体积小.速度快 ...
- FMDB线程安全
//打开数据库 如果没有就创建 NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUse ...
- 【转~】初识贝塞尔曲线(Bézier curve)
本文图文大多转自http://www.html-js.com/article/1628 QAQ我居然去扒维基,,,看不懂啊,,,我要去补数学,,, 在做变形小鸡的时候用到CSS3 transition ...
- Marquee 滚动参数
new marquee("Layer3", 2, 1, 989, 68, 20, 0, 0, 238); 参数说明:Layer3 :容器ID 2 :向上滚动(0向上 1向下 2向左 ...
- fetch 资源请求
简介:fetch() 方法用于发起获取资源的请求.它返回一个 promise,这个 promise 会在请求响应后被 resolve,并传回 Response 对象. 为了更好的体验,可点击这里阅读 ...