URAL Mosaic(并查集)(欧拉回路)
Mosaic
Memory limit: 64 MB
no doubt that one of the most important and crucial things to do in
this world is to bring up children. May be, if you study properly and
reach good results at the competition you'll get a position of nanny in a
kindergarten. But you are to get ready for it! Let's consider some
problems that a nanny has to solve in a kindergarten.
colors. After playing the game children rarely put the pieces back to
their boxes correctly so that the color of the box and the colors of its
pirces would be the same. A nanny has to do that.
pieces in each box. Some pieces (possibly all of them) are located in
wrong boxes (i.e. boxes with pieces of a different color). Moving a hand
once one can take a piece from one box to another or simply move the
hand to another box. You may start from any box you like. The movement
towards the first box is not taken into account. Find out the minimal
number of movements one needs to put all the mosaic pieces to their
boxes.
Input
Output
Sample
input | output |
---|---|
4 3 |
6 |
移动,最后使得每张卡片都放到它应该在的盒子(第1种卡片都放入盒子1,第2种卡片都放入盒子2……)。一次移动是指把一张卡片从
当前手边的盒子里拿出放到另一个盒子,或者不拿卡片,只是把手从当前的盒子处移到另一个盒子处。
【分析】容易联想到欧拉路,如果盒子i里面有一张卡片j,就把i,j之间连一条边,表示至少要有一次从i到j的移动。容易发现这样建图后每个点
的出度必然等于入度(因为初始时盒子里就有N张卡片,后面拿出多少张也就要拿入多少张),也就是说对于每个连通分量,欧拉回路必
定存在,最少的移动次数实际上就是边的总数。在不同的连通分量之间必然要有一次空着手的移动。因此最后的答案就是 边数+连通分
量数-1(第一次开始时手可以在任何位置)。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
typedef long long ll;
using namespace std;
const int N = +;
const int M = +;
const int mod=1e9+;
int n,m,k,t;
int vis[N];
int s[N];
int parent[N];
int Find(int x)
{
if(parent[x]!=x)parent[x]=Find(parent[x]);
return parent[x];
}
void Union(int x,int y)
{
x=Find(x);y=Find(y);
if(x==y)return;
parent[y]=x;
}
int main()
{
int u,v,ans=;
for(int i=;i<N;i++)parent[i]=i;
scanf("%d%d",&m,&n);
for(int i=;i<=m;i++){
for(int j=;j<=n;j++){
scanf("%d",&u);
if(i!=u){
vis[i]=vis[u]=;
ans++;
Union(i,u);
}
}
}
for(int i=;i<=m;i++){
if(vis[i]&&!s[Find(i)]){
ans++;
s[Find(i)]=;
}
}
printf("%d\n",ans==?:ans-);
return ;
}
URAL Mosaic(并查集)(欧拉回路)的更多相关文章
- NYOJ 42 一笔画问题 (并查集+欧拉回路 )
题目链接 描述 zyc从小就比较喜欢玩一些小游戏,其中就包括画一笔画,他想请你帮他写一个程序,判断一个图是否能够用一笔画下来. 规定,所有的边都只能画一次,不能重复画. 输入 第一行只有一个正整数 ...
- poj2513 Colored Sticks —— 字典树 + 并查集 + 欧拉回路
题目链接:http://poj.org/problem?id=2513 题解:通过这题了解了字典树.用字典树存储颜色,并给颜色编上序号.这题为典型的欧拉回路问题:将每种颜色当成一个点.首先通过并查集判 ...
- hdu3018 Ant Trip (并查集+欧拉回路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 题意:给你一个图,每条路只能走一次.问至少要多少个人才能遍历所有的点和所有的边. 这是之前没有接 ...
- Codeforces 1499G - Graph Coloring(带权并查集+欧拉回路)
Codeforces 题面传送门 & 洛谷题面传送门 一道非常神仙的题 %%%%%%%%%%%% 首先看到这样的设问,做题数量多一点的同学不难想到这个题.事实上对于此题而言,题面中那个&quo ...
- ACM: FZU 2112 Tickets - 欧拉回路 - 并查集
FZU 2112 Tickets Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u P ...
- POJ 2513 Colored Sticks(欧拉回路,字典树,并查集)
题意:给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 无向图存在欧拉路的充要条件为: ① 图是连通的: ② 所有节 ...
- 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个字母看做端点,那么本题其实就是问是否存在一条路径,可以到达所有出现过的字符端点.由于本题还要求了两个单词拼在一起的条件是前一个 ...
随机推荐
- Android 之 JSON操作
Android默认已经集成了操作JSON相关的API,如下所示: 也可以不使用JSON工具类,直接使用字符串拼接. 注意:可以使用字符串来构造JSONArray和JSONObject,这就是JSON解 ...
- 【转发】linux文件系统变为只读的修复
详细解决方法:http://smartmontools.sourceforge.net/badblockhowto.html 相关问题,更换硬盘:http://blog.chinaunix.net/u ...
- C语言中最常用的三种输入输出函数scanf()、printf()、getchar()和putchar()
本文给大家介绍C语言中最常用的三种输入输出函数scanf().printf().getchar()和putchar(). 一.scanf()函数格式化输入函数scanf()的功能是从键盘上输入数据,该 ...
- 最新的goldengate monitor 12.1.3已经发布
Oracle GoldenGate管理包针对OGG提供企业级的监控和管理,包含有如下模块: Oracle Enterprise Manager Plug-in. 利用OEM框架查看.管理和预警OGG ...
- mine layer(2008 World Final C)
类似于扫雷游戏,在一些格子中散布着一些地雷,具体的埋藏位置并不清楚,但知道每个格子及其周围八个格子的地雷总数.请问此时正中间那一行最多可能有多少地雷(题目假定所有的输入都是奇数行的)? 输入: 第一行 ...
- BZOJ 2982 combination
lucas定理裸题. #include<iostream> #include<cstdio> #include<cstring> #include<algor ...
- 8、C#基础整理(数组和冒泡排序)
数组 概念:定义一组同类型的指定个数的变量,索引从0开始 例: ];//定义一组有10个数据的数组 shuname[] = ; Console.WriteLine(shuname[]);//打印出1 ...
- Unity3D ShaderLab 创建自定义高光类型
Unity3D ShaderLab 创建自定义高光类型 在上一篇,我们认识了Unity基础的高光实现,本次主要是研究如何对Phong高光类型进行顶点操作,以及在表面着色器中使用Input结构体的新参数 ...
- HDU 1811
http://acm.hdu.edu.cn/showproblem.php?pid=1811 中文码题 对于等号的情况,用并查集合并(因为编号不同一定可以分出先后) 然后判断能否构成拓扑排序,以及拓扑 ...
- INNO:检测程序是否已经安装,是则弹出卸载提示。
INNO:检测程序是否已经安装,是则弹出卸载提示. 作者:少轻狂 | 发布:2010-08-05 | 更新:2013-09-05 | 分类:部署 | Disposition | 热度:2816 ℃ 实 ...