Problem Description
  1. Five hundred years later, the number of dragon balls will increase unexpectedly, so it's too difficult for Monkey King(WuKong) to gather all of the dragon balls together.

  1. His country has N cities and there are exactly N dragon balls in the world. At first, for the ith dragon ball, the sacred dragon will puts it in the ith city. Through long years, some cities' dragon ball(s) would be transported to other cities. To save physical strength WuKong plans to take Flying Nimbus Cloud, a magical flying cloud to gather dragon balls.
  2. Every time WuKong will collect the information of one dragon ball, he will ask you the information of that ball. You must tell him which city the ball is located and how many dragon balls are there in that city, you also need to tell him how many times the ball has been transported so far.
 
Input
  1. The first line of the input is a single positive integer T( < T <= ).
  2. For each case, the first line contains two integers: N and Q ( < N <= , < Q <= ).
  3. Each of the following Q lines contains either a fact or a question as the follow format:
  4. T A B : All the dragon balls which are in the same city with A have been transported to the city the Bth ball in. You can assume that the two cities are different.
  5. Q A : WuKong want to know X (the id of the city Ath ball is in), Y (the count of balls in Xth city) and Z (the tranporting times of the Ath ball). ( <= A, B <= N)
 
Output
  1. For each test case, output the test case number formated as sample output. Then for each query, output a line with three integers X Y Z saparated by a blank space.
Sample Input
  1.  
  2. T
  3. T
  4. Q
  5.  
  6. T
  7. Q
  8. T
  9. Q
 
Sample Output
  1. Case :
  2. Case :
 
Author
possessor WC
 
Source
 
并查集,和前面的hdu2818很相似

主要是记录移动次数,其实每个根结点都是最多移动一次的,所以记录移动次数把自己的加上父亲结点的就是移动总数了

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<queue>
  5. #include<stdlib.h>
  6. using namespace std;
  7. #define N 10006
  8. int n,q;
  9. int fa[N];
  10. int mov[N];
  11. int num[N];
  12. void init(){
  13. for(int i=;i<N;i++){
  14. fa[i]=i;
  15. num[i]=;
  16. mov[i]=;
  17. }
  18. }
  19. int find(int son){
  20. if(fa[son]!=son){
  21. int t=find(fa[son]);
  22. mov[son]+=mov[fa[son]];
  23. fa[son]=t;
  24. }
  25. return fa[son];
  26. //return fa[x]==x?x:fa[x]=find(fa[x]);
  27. }
  28. void merge(int x,int y){
  29. int root1=find(x);
  30. int root2=find(y);
  31. if(root1==root2)return;
  32. fa[root1]=root2;
  33. mov[root1]=;
  34. num[root2]+=num[root1];
  35. }
  36. int main()
  37. {
  38. int ac=;
  39. int t;
  40. scanf("%d",&t);
  41. while(t--){
  42. init();
  43. scanf("%d%d",&n,&q);
  44.  
  45. char s[];
  46. int x,y;
  47. printf("Case %d:\n",++ac);
  48. for(int i=;i<q;i++){
  49. scanf("%s",s);
  50. if(s[]=='T'){
  51. scanf("%d%d",&x,&y);
  52. merge(x,y);
  53. }
  54. else{
  55. scanf("%d",&x);
  56. int city=find(x);
  57. printf("%d %d %d\n",city,num[city],mov[x]);
  58. }
  59. }
  60. }
  61. return ;
  62. }

hdu 3635 Dragon Balls(并查集应用)的更多相关文章

  1. hdu 3635 Dragon Balls(并查集)

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  2. hdu 3635 Dragon Balls (带权并查集)

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  3. HDU 3635 Dragon Balls(超级经典的带权并查集!!!新手入门)

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  4. hdu 3635 Dragon Balls(加权并查集)2010 ACM-ICPC Multi-University Training Contest(19)

    这道题说,在很久很久以前,有一个故事.故事的名字叫龙珠.后来,龙珠不知道出了什么问题,从7个变成了n个. 在悟空所在的国家里有n个城市,每个城市有1个龙珠,第i个城市有第i个龙珠. 然后,每经过一段时 ...

  5. hdu 3635 Dragon Balls (MFSet)

    Problem - 3635 切切水题,并查集. 记录当前根树的结点个数,记录每个结点相对根结点的转移次数.1y~ 代码如下: #include <cstdio> #include < ...

  6. hdu 3635 Dragon Balls

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  7. HDU 3635 Dragon Balls(带权并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=3635 题意: 有n颗龙珠和n座城市,一开始第i颗龙珠就位于第i座城市,现在有2种操作,第一种操作是将x龙珠所在城 ...

  8. hdu 3635 Dragon Balls(并查集)

    题意: N个城市,每个城市有一个龙珠. 两个操作: 1.T A B:A城市的所有龙珠转移到B城市. 2.Q A:输出第A颗龙珠所在的城市,这个城市里所有的龙珠个数,第A颗龙珠总共到目前为止被转移了多少 ...

  9. HDU 1811 拓扑排序 并查集

    有n个成绩,给出m个分数间的相对大小关系,问是否合法,矛盾,不完全,其中即矛盾即不完全输出矛盾的. 相对大小的关系可以看成是一个指向的条件,如此一来很容易想到拓扑模型进行拓扑排序,每次检查当前入度为0 ...

随机推荐

  1. [Angular 2] Using ng-for to repeat template elements

    This lesson covers Angular 2’s version of looping through data in your templates: ng-for. It’s conce ...

  2. Andriod Studio科学文章——4.常见问题解答有关编译

    1.android未安装支持库 只有编译,下面的例子演示了提样: Could not find any version that matches com.android.support:appcomp ...

  3. .net中将DataTable导出到word、Excel、txt、htm的方法

    dt:DataTable strFile:fileName strExt:type private void GridExport(DataTable dt, string strFile, stri ...

  4. c++11 之 decltype

    在C++中,decltype作为操作符,用于查询表达式的数据类型.decltype在C++11标准制定时引入,主要是为泛型编程而设计,以解决泛型编程中,由于有些类型由模板参数决定,而难以(甚至不可能) ...

  5. C-冒泡排序,选择排序,数组

    ——构造类型 ->数组      ->一维数组      ->相同类型的一组数据      ->类型修饰符--数组名—[数组的元素个数(必须是整型表达式或者是整型常量,不能是变 ...

  6. JavaScript 对象 - 与属性的相关知识

    function inherit(p){ if(p == null) throw TypeError(); if(Object.create) return Object.create(p); var ...

  7. 网页搜索功能 多表搜索sql

    SELECT ID, Title, FromTableFROM (SELECT ID, ArticleName AS Title, 'Article' AS FromTable        FROM ...

  8. Java中的try/catch/finally

    样例1: public class Test{ public static String output = ""; public static void foo(int i){ t ...

  9. C++中的int和short int

    #include <iostream> #include <string> #include <cstring> //strcpy #include <cst ...

  10. jquery简单的拖动效果

    <!DOCTYPE html> <html> <meta http-equiv="Content-Type" content="text/h ...