FZU 2112 Tickets

Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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

For each test case, output an integer in a single line, indicates the smallest number of additional tickets you need to buy.

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 - 欧拉回路 - 并查集的更多相关文章

  1. hdu 1116 欧拉回路+并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=1116 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...

  2. HDU 1116 Play on Words(欧拉回路+并查集)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1116 Play on Words Time Limit: 10000/5000 MS (Java/Ot ...

  3. HDU1878 欧拉回路---(并查集+图论性质)

    http://acm.hdu.edu.cn/showproblem.php?pid=1878 欧拉回路 Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  4. hdu 3018 Ant Trip 欧拉回路+并查集

    Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem ...

  5. HDU 1116 || POJ 1386 || ZOJ 2016 Play on Words (欧拉回路+并查集)

    题目链接 题意 : 有很多门,每个门上有很多磁盘,每个盘上一个单词,必须重新排列磁盘使得每个单词的第一个字母与前一个单词的最后一个字母相同.给你一组单词问能不能排成上述形式. 思路 :把每个单词看成有 ...

  6. POJ2513——Colored Sticks(Trie树+欧拉回路+并查集)

    Colored Sticks DescriptionYou are given a bunch of wooden sticks. Each endpoint of each stick is col ...

  7. nyist 42 一笔画 (欧拉回路 + 并查集)

    nyoj42 分析: 若图G中存在这样一条路径,使得它恰通过G中每条边一次,则称该路径为欧拉路径. 若该路径是一个圈,则称为欧拉(Euler)回路. 具有欧拉回路的图称为欧拉图(简称E图).具有欧拉路 ...

  8. UVA - 10129 Play on Words(欧拉回路+并查集)

    2.解题思路:本题利用欧拉回路存在条件解决.可以将所有的单词看做边,26个字母看做端点,那么本题其实就是问是否存在一条路径,可以到达所有出现过的字符端点.由于本题还要求了两个单词拼在一起的条件是前一个 ...

  9. poj 1386 Play on Words门上的单词【欧拉回路&&并查集】

    题目链接:http://poj.org/problem?id=1386 题目大意:给你若干个字符串,一个单词的尾部和一个单词的头部相同那么这两个单词就可以相连,判断给出的n个单词是否能够一个接着一个全 ...

随机推荐

  1. Spring系列之Spring常用注解总结

    传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...

  2. ab 轻量的压测工具

    阅读:http://www.cnblogs.com/luminji/archive/2011/09/02/2163525.html

  3. 认识VTK工作原理

    VTk通过数据流实现变信息为图形数据的. 数据流一般为:source-filter--mapper--actor--render--renderwindow--interactor. 要理解工作原理, ...

  4. ActiveMQ 即时通讯服务 浅析

      一. 概述与介绍 ActiveMQ 是Apache出品,最流行的.功能强大的即时通讯和集成模式的开源服务器.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provi ...

  5. ACM 计算几何中的精度问题(转)

    http://www.cnblogs.com/acsmile/archive/2011/05/09/2040918.html 计算几何头疼的地方一般在于代码量大和精度问题,代码量问题只要平时注意积累模 ...

  6. 2016 GitHub章鱼猫观察报告之开源统计

    导读 GitHub 又发布了一年一度的章鱼猫观察报告.在这个报告中,分别对开源和社区做了一些有趣的统计,现将其中一些有趣的数据和趋势撷取出来分享给大家.完整的报告请移步Github. GitHub 上 ...

  7. js 时间构造函数

    js 时间构造函数,js中没有类似ToString("yyyy-mm-dd HH:mm:ss") 的方法,但是可以用下面的方式来初始化 var cdate = new Date(& ...

  8. zend studio 做前端推荐安装的插件

    zend studio 做前端推荐安装的插件 1.Aptana插件代码提示 Zend Studio的aptana插件,解决了Zend Studio对前台代码支持不足的问题,而且在某些方面还比诸如dw优 ...

  9. WIN7下VS2008生成Detours3.0

    Detours是微软开发的一个函数库,可用于捕获系统API.在用其进行程序开发之前,得做一些准备工作: 一.下载Detours       在http://research.microsoft.com ...

  10. C Primer Plus_第5章_运算符、表达式和语句_编程练习

    Practice 1. 输入分钟输出对应的小时和分钟. #include #define MIN_PER_H 60 int main(void) { int mins, hours, minutes; ...