Question:
Link: Click to open the link
Question:
Set the number to 1 ~ Place n's dragon beads in the range of 1 ~ N. Now there are two types of operations: T and Q:
T: a B transfers all the dragon beads in the city where A is located to B.
Q: A indicates querying some information about longzhu: X (the city where A is located), y (the number of longzhu in the city where A is located ), Z (number of times a is moved to the city ).
Ideas:
Code:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int N = 10010;struct node{ int parent; int son; int TransNum;}p[N];int n,q;int a,b,c,ans;char ch;int findset(int x){ int temp; if(x == p[x].parent) return x; else { temp = p[x].parent; p[x].parent = findset(p[x].parent); p[x].TransNum += p[temp].TransNum; } return p[x].parent;}void mergeset(int x,int y){ int fx = findset(x); int fy = findset(y); if(fx != fy) { p[fx].parent = fy; p[fx].TransNum++; p[fy].son += p[fx].son; p[fx].son = 0; }}void init(){ for(int i=1; i<=n; i++) { p[i].parent = i; p[i].son = 1; p[i].TransNum = 0; }}int main(){ //freopen("input.txt","r",stdin); int t; int kase = 0; cin>>t; while(t--) { scanf("%d%d",&n,&q); getchar(); init(); printf("Case %d:\n",++kase); for(int i=0; i<q; i++) { scanf("%c",&ch); getchar(); if(ch == 'T') { scanf("%d%d",&a,&b); mergeset(a,b); } else { scanf("%d",&c); ans = findset(c); printf("%d %d %d\n",ans,p[ans].son,p[c].TransNum); } getchar(); } } return 0;}
----------------------------------------------
Fighting, never shrinking; fighting, never stopping ~~~~~~~~