HDU 3635 dragon bils (with permission and query set)

Source: Internet
Author: User

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 ~~~~~~~~


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.