1483: [HNOI2009] Fantasy pudding

Source: Internet
Author: User

1483: [HNOI2009] Fantasy PuddingDescription

N pudding is placed in a row for M operations. Each time a color's pudding is all turned into another color, then ask the current total number of colors. For example, four pudding with a color of 1,2,2,1, a total of 3 colors.

Input

The first line gives N,m the number of pudding and the number of times a friend is operating. Second row n number a1,a2 ... An indicates that the color of the first I pudding has M line from the third line, and for each operation, if the first number is 1 to change the color, then the two integer, x, y, or X, indicates that turning all colors into y,x may be equal to Y. If the first number is 2 to ask how many colors are currently in question, you should output an integer. 0

Output

Ask for the second type of action, and then output the current number of colors.

Sample Input 4 3
1 2 2 1
2
1 2 1
2
Sample Output3
1acty really big!!! ——————— the following ———————List plus optimization: a chain of each colorWe use heuristic merging, each time we combine the chain of fewer elements into the chain of elements, but one problem is that the title specifies X, Y, and turns the × color to Y. if the element of color x is more than Y, we can still merge y into X, just to maintain an father array, to indicate what color x actually represents.
#include <stdio.h>#include<iostream>#include<string.h>using namespacestd;Const intm=1000005;Const intn=100005;intN,m,i,ans,x,y,p,a[n],s[m],head[m],last[m],next[n],f[m];intMain () {scanf ("%d%d",&n,&m); Ans=0;  for(i=1; i<=n;i++) {scanf ("%d",&A[i]); F[a[i]]=A[i]; S[a[i]]++; Next[i]=Last[a[i]]; Last[a[i]]=i; if(!head[a[i]]) head[a[i]]=i; if(a[i]!=a[i-1]) ans++; }     while(m--) {scanf ("%d",&p); if(p==1) {scanf ("%d%d",&x,&y); if(x==y)Continue; if(s[f[x]]>S[f[y]) swap (f[x],f[y]); X=f[x];y=F[y]; if(!s[x])Continue;  for(i=last[x];i;i=Next[i]) {                if(a[i-1]==y) ans--; if(a[i+1]==y) ans--; }             for(I=last[x];i;i=next[i]) a[i]=y; S[y]+=S[x]; S[X]=0; Next[head[y]]=Last[x]; Head[y]=Head[x]; } Elseprintf"%d\n", ans); }    return 0;}

1483: [HNOI2009] Fantasy pudding

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.