首页 > 代码库 > URAL Mosaic(并查集)(欧拉回路)

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

inputoutput
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;
}

 

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