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. 11月15日下午 ajax返回数据类型为XML数据的处理

    ajax返回数据类型为XML数据的处理 /*XML:可扩展标记语言 HTML:超文本标记语言 标签:<标签名></标签名> 特点: 1.必须要有一个根 2.标签名自定义 3.对 ...

  2. Java学习笔记11

    package welcome; import java.util.Scanner; /* * 代数问题:求解2x2线性方程 */ public class ComputeLinearEquation ...

  3. 界面通信之block传值

    block传值有两种方式 ⽅式⼀: 使⽤block属性实现回调传值 ⽅式⼆: 在⽅法中定义block实现回调传值 方式一比较便于理解而且常用,下面介绍方式一是如何传值的 使用block属性传值和代理传 ...

  4. 什么叫session和cookie-及其设置

    http的无状态? 保持状态, 是指当程序关闭后重启, 上一次操作的历史还能继续, 保持的. 如word中的 "选项"设置. 如windows系统的设置等等. http的设计目的, ...

  5. 用类方法------>快速创建一个autorelease的对象,在封装的类方法内部

    在封装的类方法内部,也就是+ (id)personWithName:(NSString *)name andAge:(int)age内部: 创建了一个person对象,并且创建了一个person*类型 ...

  6. ZOJ 3699 Dakar Rally

    Dakar Rally Time Limit: 2 Seconds      Memory Limit: 65536 KB Description The Dakar Rally is an annu ...

  7. 一些PHP性能优化汇总

    PHP优化对于PHP的优化主要是对php.ini中的相关主要参数进行合理调整和设置,以下我们就来看看php.ini中的一些对性能影响较大的参数应该如何设置. # vi /etc/php.ini (1) ...

  8. Python cumsums和cumprod函数

    >>>a = np.array([1,2,3],[4,5,6]]) >>>a array([[1,2,3], [4,5,6]]) >>>a.cum ...

  9. ViewPager切换滑动速度修改

    ViewPager的setCurrentItem 滑动速度是写死地 下面的方法可以修改,在此以做记录 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...

  10. python __call__ 内置函数的使用

    对象通过提供__call__(slef, [,*args [,**kwargs]])方法可以模拟函数的行为, 如果一个对象x提供了该方法,就可以像函数一样使用它,也就是说x(arg1, arg2... ...