hdoj 3635 Dragon Balls【并查集求节点转移次数+节点数+某点根节点】
Dragon Balls
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4384 Accepted Submission(s):
1673
will increase unexpectedly, so it's too difficult for Monkey King(WuKong) to
gather all of the dragon balls together.

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.
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.
integer T(0 < T <= 100).
For each case, the first line contains two
integers: N and Q (2 < N <= 10000 , 2 < Q <= 10000).
Each of the
following Q lines contains either a fact or a question as the follow
format:
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.
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). (1 <= A, B <= N)
formated as sample output. Then for each query, output a line with three
integers X Y Z saparated by a blank space.
- #include<stdio.h>
- #include<string.h>
- #define MAX 20000
- int set[MAX];
- int path[MAX];
- int time[MAX];
- int find(int fa)
- {
- int t;
- if(fa==set[fa])
- return fa;
- t=set[fa];
- set[fa]=find(set[fa]);
- time[fa]+=time[t];
- return set[fa];
- }
- void mix(int x,int y)
- {
- int fx;
- int fy;
- fx=find(x);
- fy=find(y);
- if(fx!=fy)
- {
- set[fx]=fy;
- path[fy]+=path[fx];
- time[fx]++;
- }
- }
- int main()
- {
- int t,n,m,x,y,b,i;
- char a;
- scanf("%d",&t);
- int k=0;
- while(t--)
- {
- scanf("%d%d",&n,&m);
- printf("Case %d:\n",++k);
- for(i=1;i<=n;i++)
- {
- set[i]=i;
- path[i]=1;
- time[i]=0;
- }
- while(m--)
- {
- getchar();
- scanf("%c %d",&a,&x);
- if(a=='T')
- {
- scanf("%d",&y);
- mix(x,y);
- }
- else
- {
- y=find(x);//此处必须用一个变量值来表示find(x)
- printf("%d %d %d\n",y,path[y],time[x]);
- }
- }
- }
- return 0;
- }
hdoj 3635 Dragon Balls【并查集求节点转移次数+节点数+某点根节点】的更多相关文章
- hdu 3635 Dragon Balls(并查集)
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- hdu 3635 Dragon Balls(加权并查集)2010 ACM-ICPC Multi-University Training Contest(19)
这道题说,在很久很久以前,有一个故事.故事的名字叫龙珠.后来,龙珠不知道出了什么问题,从7个变成了n个. 在悟空所在的国家里有n个城市,每个城市有1个龙珠,第i个城市有第i个龙珠. 然后,每经过一段时 ...
- Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 并查集求奇偶元环
D. Dividing Kingdom II Long time ago, there was a great kingdom and it was being ruled by The Grea ...
- C. Edgy Trees Codeforces Round #548 (Div. 2) 并查集求连通块
C. Edgy Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- HDU 3018 Ant Trip (并查集求连通块数+欧拉回路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 题目大意:有n个点,m条边,人们希望走完所有的路,且每条道路只能走一遍.至少要将人们分成几组. ...
- hdu 3635 Dragon Balls(并查集应用)
Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...
- hdu 3635 Dragon Balls (带权并查集)
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 3635 Dragon Balls(超级经典的带权并查集!!!新手入门)
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- hdu 3635 Dragon Balls(并查集)
题意: N个城市,每个城市有一个龙珠. 两个操作: 1.T A B:A城市的所有龙珠转移到B城市. 2.Q A:输出第A颗龙珠所在的城市,这个城市里所有的龙珠个数,第A颗龙珠总共到目前为止被转移了多少 ...
随机推荐
- 查看Safari和钥匙串中的密码
Safari Safari的同步书签功能很棒,还可以看到其他设备没关掉的网页.为了省时间,一些经常进的网站,比如博客,邮箱等,我都会选择让Safari保存密码,还使用iCloud同步!因为一直很放心苹 ...
- 理解ThreadLocal背后的概念
介绍 我之前在任何场合都没有使用过thread local,因此没有注意到它,直到最近用到它的时候. 前提信息 线程可以理解为一个单独的进程,它有自己的调用栈.在java中每一个线程都有一个调用栈或者 ...
- js跳转页面方法
<span id="tiao">3</span><a href="javascript:countDown"></ ...
- C# 禁止 Webbrowser 控件的弹出脚本错误对话框
当IE浏览器遇到脚本错误时浏览器,左下 角会出现一个黄色图标,点击可以查看脚本错误的详细信息,并不会有弹出的错误信息框.当我们使用 WebBrowser控件时有错误信息框弹出,这样程序显的很不友好,而 ...
- StringBuilder字符串拼接类
StringBuilder StringBuilder是在using System.Text命名空间下的一个成员. 在做字符串拼接的时候,因为字符串是引用类型,新的字符串是会再内存中创建的,所以用+号 ...
- 使用SourceTree将bitbucket的远程仓库回滚到某一次提交-b
目的: 使用SourceTree将bitbucket的远程仓库回滚到某一次提交. 原理: 在本地需要回滚的commit上创建一个分支,将该分支合并到远程仓库. 准备: 远程仓库有一个master主分支 ...
- 移动App设计之分层架构+MVC
http://www.cnblogs.com/Logen/archive/2012/11/08/2760638.html 场景分析:我们知道,一个移动设备的应用大多与网络有关,也就是说,我在移动设备上 ...
- "Cannot convert value '0000-00-00' from column 2 to TIMESTAMP"mysql时间转换bug
今天在项目中遇到这样的一个bug,Cannot convert value '0000-00-00' from column 2 to TIMESTAMP 仔细一查,经过http://blog.csd ...
- liveReload
依赖条件: 1.安装liveReload浏览器插件: http://livereload.com/extensions/ chrome可以直接去在线商店安装liveReload. P.S.也可以贴代码 ...
- 7 Tools for Data Visualization in R, Python, and Julia
7 Tools for Data Visualization in R, Python, and Julia Last week, some examples of creating visualiz ...