Test instructions
n messages need to be identified.
Two kinds of operations:
1. M X y:x and Y are the same message
2.S x:x is misjudged (meaning that X is "kicked out" by the set it is subordinate to, and the relationship of all other messages remains the same)
There are several kinds of mails at the end of the question.
Ideas:
The "merge" of the messages can be achieved by using the and check sets. But to "kick out" the individual x and then make sure that the other relationships are the same.
To get a double-layered structure, the first layer is used to describe the relationship before the message. The second layer is used to point to the first layer of the node.
The first layer is FA, and the second layer is VFA. When X is "kicked out", just add a new point to the first layer and then vfa[x] point to the new node.
The relationship between the original message was retained and the X was "kicked out" successfully.
Code:
intn,m;intfa[1200005];intvfa[100005];intFINDFA (intx) { returnfa[x]==x?fa[x]:fa[x]=Findfa (fa[x]);}intMain () {intt=0; while(SCANF ("%d%d", &n,&m), n| |L) {Rep (i,1, N) {Fa[i]=i; Vfa[i]=i; } intcn=N; while(m--){ Charope[5]; scanf ("%s", ope); if(ope[0]=='M'){ intb; scanf ("%d%d",&a,&b); intFX=FINDFA (vfa[a+1]); intFY=FINDFA (vfa[b+1]); if(fx!=FY) {FA[FX]=fy; } }Else{ intx; scanf ("%d",&x); Vfa[x+1]=++cn; FA[CN]=cn; }} Map<int,int>MP; Mp.clear (); intans=0; Rep (I,1, N) { intt=Findfa (Vfa[i]); if(mp[t]==0) {Mp[t]=1; ++ans; }} printf ("Case #%d:%d\n",++T,ans); } return 0;}
Hdu 2473 Junk-mail Filter (and set)