URAL Mosaic (and set) (Euler circuit)

Source: Internet
Author: User

Mosaictime limit:0.25 Second
Memory limit:64 Mbthere's no doubt that one's the most important and crucial things to does in this world are to bring up C Hildren. May is, if you study properly and reach good results at the competition you'll get a position of nanny in a kindergarten. But you is to get the ready for it! Let's consider some problems that a nanny have 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 MDifferent boxes and NMosaic pieces of each of the MColors. After playing the game children rarely put the pieces back to their boxes correctly so that the color of the box and the C Olors of its pirces would is the same. A nanny have to does that. Children has already put the mosaic pieces to the boxes and possibly not correctly. There is NPieces in each box. Some Pieces (possibly all of them) is 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 could start from any box and like. The movement towards the first box is not taken to account. Find out the minimal number of movements one needs to put all the mosaic pieces to their boxes. Inputthe first line contains integers 2≤ M≤500 (the number of colors) and 2≤ N≤50 (the number of pieces of each color), and each of the next MLines contains NNumbers in the range from 1 to M(The I+1-st line contains colors of pieces located in the I-Th box). The numbers is separated with a space. Outputthe minimal possible number of hand movements that one have to do in order to take all the pieces to their boxes. Sample
input Output
4 31 3 12 3 31 2 24 4 4
6
problem Author:Stanislav Vasilyev "Test instructions" there are m cards, each with N, initially placed in m boxes, each box has n, but there may be some cards misplaced, so need to do some

Move, and finally make each card into the box it should be in (the 1th card is put in box 1, the 2nd card is put in Box 2 ...). )。 A move refers to a card from

Take out the box at hand and put it in another box, or do not take the card, just move the hand from the current box to another box.

"Analysis" is easy to think of Euler road, if there is a card in the box I J, the I,j is connected to an edge, indicating at least one move from I to J. It's easy to find that every point after this map

The out degree is necessarily equal to the degree of penetration (because there are n cards in the initial box, and how many sheets are taken out of the back), which means that for each connected component, the Euler loop must

The least number of moves is actually the total number of edges. There must be a movement of empty hands between the different connected components. So the final answer is the number of edges + connected points

Volume-1 (hand can be in any position at first start).

#include <iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<cmath>#include<string>#include<map>#include<stack>#include<queue>#include<vector>#defineINF 0x3f3f3f3f#defineMet (b) memset (a,b,sizeof a)#definePB Push_backtypedefLong Longll;using namespacestd;Const intN = ++Ten;Const intM =124750+Ten;Const intmod=1e9+7;intn,m,k,t;intVis[n];intS[n];intParent[n];intFind (intx) {    if(parent[x]!=x) parent[x]=Find (parent[x]); returnparent[x];}voidUnion (intXinty) {x=find (x); y=Find (y); if(x==y)return; Parent[y]=x;}intMain () {intu,v,ans=0;  for(intI=0; i<n;i++) parent[i]=i; scanf ("%d%d",&m,&N);  for(intI=1; i<=m;i++){         for(intj=1; j<=n;j++) {scanf ("%d",&u); if(i!=u) {Vis[i]=vis[u]=1; Ans++;            Union (I,u); }        }    }     for(intI=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 (and set) (Euler circuit)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.