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. 看项目得到info_freeCsdn-01闪屏页面

    /** * 渐变展示启动屏 */ private void startAnimation() { Animation aa = new Animation() { }; aa.setDuration( ...

  2. Memcache 详解

    这里收集了经常被问到的关于memcached的问题 一般的问题 什么是memcached? 从哪获得memcached? 怎么安装memcached? 哪些平台可以运行memcached? 什么情况下 ...

  3. 记录一些容易忘记的属性 -- NSTimer

    使定时器停止的方法: 1. //将定时器的启动时间设置为很久以后的将来,到这个时间,定时器才会开始工作            [_timer setFireDate:[NSDate distantFu ...

  4. C++学习 之const

    const在C++中很常用,在编程中也建议多使用const去告诉编译器和其他程序员某个值应该保持不变. const可以用在很多地方: (1)用在classes外部修饰global或namespace作 ...

  5. Tomcat 网站部署(三)

    一.Tomcat的部署方式有以下两种 1.自动部署 2.虚拟目录 二.自动部署 文件必须放在放在webapps就可以了,可以用这样访问 http://localhost:8080/放在webapps目 ...

  6. 去掉NavigationBar底部的黑线

    UINavigationBar *navigationBar = self.navigationController.navigationBar;   [navigationBar setBackgr ...

  7. C++语法疑点

    1函数模板不支持偏特化 2类内部的typedef 必须放在最前面,不然没法用: 疑问:为什么类声明处定义的函数体中能出现在后面在声明的成员变量??因为C++对于成员函数函数体的解析是放在整个类声明完毕 ...

  8. 【模块应用】MFRC522开发笔记

    一.了解基本概念 ①ISO-14443A协议:( 国际标准化组织:International Organization for Standardization)RFID协议的一种;   PICC:临近 ...

  9. BZOJ 1630/2023 Ant Counting 数蚂蚁

    DP. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> ...

  10. Angularjs相关理论

    1.AngularJS的工作流程: (1)浏览器载入HTML,然后把它解析成DOM (2)浏览器载入angularjs脚本 (3)AngularJS等到DOMContentLoaded事件触发 (4) ...