URAL Mosaic(并查集)(欧拉回路)

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 = 1000+10;
const int M = 124750+10;
const int mod=1e9+7;
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=0;
    for(int i=0;i<N;i++)parent[i]=i;
    scanf("%d%d",&m,&n);
    for(int i=1;i<=m;i++){
        for(int j=1;j<=n;j++){
            scanf("%d",&u);
            if(i!=u){
                vis[i]=vis[u]=1;
                ans++;
                Union(i,u);
            }
        }
    }
    for(int i=1;i<=m;i++){
        if(vis[i]&&!s[Find(i)]){
            ans++;
            s[Find(i)]=1;
        }
    }
    printf("%d\n",ans==0?0:ans-1);
    return 0;
}
时间: 2024-10-13 04:15:01

URAL Mosaic(并查集)(欧拉回路)的相关文章

并查集+欧拉回路+字典树 Colored Sticks POJ 2513

输入多组数据,每组数据两种颜色,表示一根木头两端的颜色,现在要将这些木头相连,要求相连部分颜色相同,问能否全部连通 提示 1)一个要判断所有的木头是否在一个集合中,即是否能相连 2)判断一种颜色出现的数量 3)一棵树如果只有0或2个点出现次数为奇数,则树可以一笔画成 #include <stdio.h> #include <string.h> #define maxn 500005 int tot; int f[maxn]; int num[maxn]; struct trie {

HDU 1878 欧拉回路 (并查集+欧拉回路)

题目地址:HDU 1878 这个题要注意欧拉回路与欧拉通路的区别.在都保证连通性的前提下,欧拉回路要求每个点的度数都是偶数,而欧拉通路允许两个点的度数是奇数.所以这题用并查集判断连通性后判断下度数就可以了. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib

Pilot Work Experience (URAL 1888 并查集+floyd)

Pilot Work Experience Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description Leonid had n Oceanic Airlines flights during his business trip. He studied the latest issue of the monthly on-board magazine o

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

2.解题思路:本题利用欧拉回路存在条件解决.可以将所有的单词看做边,26个字母看做端点,那么本题其实就是问是否存在一条路径,可以到达所有出现过的字符端点.由于本题还要求了两个单词拼在一起的条件是前一个单词的右端点和本单词的左端点一样.所以这是一个有向图.根据结论:有向图的底图(忽略边的方向后的图)必须连通:有向图中最多只能有两个端点的入度不等于出度,且必须是其中一点的入度比出度小1,另一点的入度比出度大1.因此先判断端点是否都连通,再判断每个端点的度数是否满足结论即可. 那么,如何判断连通性呢?

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

FZU 2112 Tickets Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Practice 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

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

nyoj42 分析: 若图G中存在这样一条路径,使得它恰通过G中每条边一次,则称该路径为欧拉路径. 若该路径是一个圈,则称为欧拉(Euler)回路. 具有欧拉回路的图称为欧拉图(简称E图).具有欧拉路径但不具有欧拉回路的图称为半欧拉图. 先说一下欧拉路径.欧拉回路的充要条件: 1.无向连通图G是欧拉图,当且仅当G不含奇数度结点(G的所有结点度数为偶数): 2.无向连通图G含有欧拉通路,当且仅当G有零个或两个奇数度的结点: 3.有向连通图D是欧拉图,当且仅当该图为连通图且D中每个结点的入度=出度

NYOJ42 一笔画问题 【欧拉回路】+【并查集】

一笔画问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 zyc从小就比较喜欢玩一些小游戏,其中就包括画一笔画,他想请你帮他写一个程序,判断一个图是否能够用一笔画下来. 规定,所有的边都只能画一次,不能重复画. 输入 第一行只有一个正整数N(N<=10)表示测试数据的组数. 每组测试数据的第一行有两个正整数P,Q(P<=1000,Q<=2000),分别表示这个画中有多少个顶点和多少条连线.(点的编号从1到P) 随后的Q行,每行有两个正整数A,B(0<

poj 2513 欧拉回路+并查集推断是否联通+Trie树

http://poj.org/problem? id=2513 最初看到 第一感觉---map  一看250000的数据量 果断放弃 然后记得曾经看过.trie取代map.尤其当数据量特别大的时候 学到了: 1.Trie取代map的思想,能够在单词结尾的tree[i][tk]  这个i作为字符串相应的int值 .当然这个int值也能够用于建立并查集 2.接上.通过并查集推断.全部的点在同一个集合图就是联通的,否则不联通,注意tree[i][tk]>0 表示是单词结尾. x=Find(x);//这

poj 2513 Colored Sticks 并查集 字典树 欧拉回路判断

点击打开链接题目链接 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 30273   Accepted: 8002 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sti

Catenyms+欧拉回路/欧拉路+并查集+POJ

Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9617   Accepted: 2524 Description A catenym is a pair of words separated by a period such that the last letter of the first word is the same as the last letter of the second. For e