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. AE套宗获取内环

    主要根据IPolygon4的get_InteriorRingBag方法. IGeometry geo = null; IRing exring = null; (geo as IPolygon2).Q ...

  2. linux shell脚本查找重复行/查找非重复行/去除重复行/重复行统计

    转自:http://blog.sina.com.cn/s/blog_6797a6700101pdm7.html 去除重复行 sort file |uniq 查找非重复行 sort file |uniq ...

  3. Html限制input输入框只能输入数字

    限制输入框只能输入数字, onafterpaste防止用户从其它地方copy内容粘贴到输入框: <input type="text" onkeyup="this.v ...

  4. Android 四大组件之Service

    ---恢复内容开始--- 1,Service的生命周期

  5. 2013成都网络赛 C We Love MOE Girls(水题)

    We Love MOE Girls Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. [MapReduce] Google三驾马车:GFS、MapReduce和Bigtable

    声明:此文转载自博客开发团队的博客,尊重原创工作.该文适合学分布式系统之前,作为背景介绍来读. 谈到分布式系统,就不得不提Google的三驾马车:Google FS[1],MapReduce[2],B ...

  7. 搭建一个简单struts2框架的登陆

    第一步:下载struts2对应的jar包,可以到struts官网下载:http://struts.apache.org/download.cgi#struts252 出于学习的目的,可以把整个完整的压 ...

  8. word20161213

    journal queue / 日志队列 journal quota / 日志配额 junction point / 交叉点 KDC, Key Distribution Center / 密钥分发中心 ...

  9. AngularJS 配置和运行phonecat错误

    安装node.js 就按照入门上的步骤来就行了 第一步:下载安装node.js,并将程序路径添加到环境变量中PATH中 第二步:下载git版本控制软件 第三步:在git目录下点击git-bash.ex ...

  10. GDI+中发生一般性错误的解决办法

    这个错误经常发生,代码如下: private  static  byte[] GetBytes (Image image) { try { if (image == null) return null ...