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个单词是否能够一个接着一个全 ...
随机推荐
- WordPress数据库优化技巧
各位站长都知道wordpress用久了就会越来越慢.今天就给大家介绍如何给自己的wordpress提速,分两种方法:1.插件属性wordpress的都知道其插件是相当的多,只要你能想得到的基本都有,在 ...
- UITabBarController 、TabBar背景颜色设置,UITabBarItem的文字样式(颜色和大小)UITabBarItem的位置调整
改变UITabBarController的颜色 UIView*mView=[[UIView alloc]initWithFrame:CGRectMake(0,0,320,48)];//这是部分tabb ...
- Java类中各种静态变量的加载顺序的学习
最近在补<thinking in java>...有一节提到了加载类需要做的一些准备...我照着书本敲了一下代码...同时稍微修改了一下书本上的代码.... package charpte ...
- Linux下的Apache和PHP安全设置,如何开启PHP的安全模式
Linux下的Apache和PHP安全设置 PHP安全模式开启,PHP5.3将不再有安全模式. (1) safe_mode:以安全模式运行php; 在php.ini文件中使用如下 safe_mode ...
- 【Maven】运行项目
1.run as maven clean 清除原有的编译结果重新编译一次. 2.run as maven bulid.. goals:package tomcat7:deploy 打包到Tomcat7 ...
- PHP PHPUnit的简单使用
1.window安装pear的教程:http://jingyan.baidu.com/article/ca41422fd8cf3d1eae99ed3e.html 2.在工作目录下,放两个文件: 1)C ...
- Types of CQRS
Types of CQRS By Vladimir Khorikov CQRS is a pretty defined concept. Often, people say that you eith ...
- Python 开发轻量级爬虫05
Python 开发轻量级爬虫 (imooc总结05--网页下载器) 介绍网页下载器 网页下载器是将互联网上url对应的网页下载到本地的工具.因为将网页下载到本地才能进行后续的分析处理,可以说网页下载器 ...
- 【目录】Qt
Qt 自己生成ui加入工程 C++ GUI Qt4 学习笔记1 C++ GUI Qt4 学习笔记2 C++ GUI Qt4 学习笔记3 C++ GUI Qt4 学习笔记4 C++ GUI Qt4 学习 ...
- Mac Pro 16G 安装MyEclipse提示虚拟内存(为0)不够
百度一下很多人都说开多一点程序,让程序占满内存,使其虚拟内存使用就能通过这一步骤,但这里有个更好一点的方案 通过执行: memory_pressure -l critical 用系统内存压力测试进程占 ...