Mosaic

Time limit: 0.25 second
Memory limit: 64 MB
There's
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.
Everyone knows the game "Mosaic". Playing the game, one is to lay out pictures of different colored pieces. Let there be M different boxes and N mosaic pieces of each of the M
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.
Children have already put the mosaic pieces to the boxes but possibly not correctly. There are N
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

The first line contains integers 2 ≤ M ≤ 500 (the number of colors) and 2 ≤ N ≤ 50 (the number of pieces of each color), Each of the next M lines contains N numbers in the range from 1 to M (the i+1-st line contains colors of pieces located in the i-th box). The numbers are separated with a space.

Output

the minimal possible number of hand movements that one has to make in order to take all the pieces to their boxes.

Sample

input output
4 3
1 3 1
2 3 3
1 2 2
4 4 4
6
Problem Author: Stanislav Vasilyev
【题意】有M种卡片,每种有N个,初始时放在M个盒子里,每个盒子里有N张,但是可能有某些卡片放错了位置,因此需要进行一些

移动,最后使得每张卡片都放到它应该在的盒子(第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(并查集)(欧拉回路)的更多相关文章

  1. NYOJ 42 一笔画问题 (并查集+欧拉回路 )

    题目链接 描述 zyc从小就比较喜欢玩一些小游戏,其中就包括画一笔画,他想请你帮他写一个程序,判断一个图是否能够用一笔画下来. 规定,所有的边都只能画一次,不能重复画.   输入 第一行只有一个正整数 ...

  2. poj2513 Colored Sticks —— 字典树 + 并查集 + 欧拉回路

    题目链接:http://poj.org/problem?id=2513 题解:通过这题了解了字典树.用字典树存储颜色,并给颜色编上序号.这题为典型的欧拉回路问题:将每种颜色当成一个点.首先通过并查集判 ...

  3. hdu3018 Ant Trip (并查集+欧拉回路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 题意:给你一个图,每条路只能走一次.问至少要多少个人才能遍历所有的点和所有的边. 这是之前没有接 ...

  4. Codeforces 1499G - Graph Coloring(带权并查集+欧拉回路)

    Codeforces 题面传送门 & 洛谷题面传送门 一道非常神仙的题 %%%%%%%%%%%% 首先看到这样的设问,做题数量多一点的同学不难想到这个题.事实上对于此题而言,题面中那个&quo ...

  5. ACM: FZU 2112 Tickets - 欧拉回路 - 并查集

     FZU 2112 Tickets Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u P ...

  6. POJ 2513 Colored Sticks(欧拉回路,字典树,并查集)

    题意:给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的.   无向图存在欧拉路的充要条件为: ①     图是连通的: ②     所有节 ...

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

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

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

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

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

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

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

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

随机推荐

  1. hihocoder 1236(2015北京网络赛 J题) 分块bitset乱搞题

    题目大意: 每个人有五门课成绩,初始给定一部分学生的成绩,然后每次询问给出一个学生的成绩,希望知道在给定的一堆学生的成绩比这个学生每门都低或者相等的人数 因为强行要求在线查询,所以题目要求,每次当前给 ...

  2. ScrollView中嵌入ListView,GridView冲突的解决(让ListView全显示出来)

    ScrollView中嵌入原生ListView或GridView,会出现ListView,GridView显示不全的问题. 解决方法:重新构造一个ListView或GridView,重写OnMeasu ...

  3. OpenLayers简单介绍以及简单实例

    OpenLayers是一个强大的JavaScript包,可以从它的官网免费下载.OpenLayers包含了很多强大的网页地图展示与操作功能,并且能够将不同源的图层展示在同一张地图中,支持各种第三方的地 ...

  4. vs2012 .netFramwork2.0发布到xp

    开发环境 windows server2008R2 VS2012  .net Framwork2.0 开发的winform程序 在有的xp系统下不能运行 选择 项目属性=>编译 Build=&g ...

  5. UIViewController

    UIViewController 在MVC模式中就是C.关于MVC,可以看 UIViewController 主要具有什么功能呢? View Management When you define a ...

  6. 【IOS基础知识】NSTimer定时器使用

    1.声明 NSTimer         *timer; 2.定义 timer = [NSTimerscheduledTimerWithTimeInterval:1.0ftarget:selfsele ...

  7. self进行weak化

    创建block匿名函数之前一般需要对self进行weak化,否则造成循环引用无法释放controller: __weak MyController *weakSelf = self 或者 __weak ...

  8. c规范(2)

    一:对齐 1 程序的分界符' {'和' }'应独占一行并且位于同一列,同时与引用它们的语句左对齐. 2  { }之内的代码块在' {'右边数格处左对齐. 二:注释 注释通常用于:( 1)版本.版权声明 ...

  9. iOS上架(转)

    自己的经验总结,有错的话请留言,第一时间更改. 先大概说一下IOSAPP上架的几个步骤(详细步骤见下图): 创建证书请求文件 登录苹果开发者中心生成发布者证书(下载下来要双击一下) 设置APPID(要 ...

  10. WebGis应用开发框架

    转自:http://www.cnblogs.com/zitsing/archive/2012/03/02/2377083.html 前言 Web Gis顾名思义就是通过浏览器方式操作的地理系统.通过浏 ...