hdu 3635
来源:互联网 发布:js修改div样式 编辑:程序博客网 时间:2024/05/16 13:39
题目
wa了n次,最后网上搜代码。
要先进行移动的步数的更新,在路径压缩,否则就是wa
代码如下;
#include<iostream>#include<cstdio>using namespace std;#define maxn 10010int pre[maxn],num[maxn],times[maxn];inline int find(int x){ if(x==pre[x]) return x; int t=pre[x]; pre[x]=find(pre[x]); times[x]+=times[t]; return pre[x];}//先把移动的值记录下来,再进行路径的压缩inline void Union(int x,int y){ int fx=find(x); int fy=find(y); if(fx!=fy){ pre[fx]=fy; num[fy]+=num[fx]; times[fx]=1; }}inline int input(){ char c; int ret=0; c=getchar(); while(c<'0'||c>'9') c=getchar(); while(c>='0'&&c<='9') ret=ret*10+c-'0',c=getchar(); return ret;}int main(){ int T,i,n,m,ncase=1; int a,b; T=input(); while(T--){ printf("Case %d:\n",ncase++); n=input();m=input(); for(i=1;i<=n;i++){ pre[i]=i; times[i]=0; num[i]=1; } while(m--){ char str[5]; scanf("%s",&str); if(str[0]=='T'){ a=input();b=input(); Union(a,b); } else{ a=input(); int ans=find(a); printf("%d %d %d\n",ans,num[ans],times[a]); } } } return 0;}
- hdu 3172+hdu 3635+hdu 3926
- hdu 3635
- HDU 3635
- hdu 3635
- HDU 3635
- (3635)HDU
- hdu 3635
- hdu 3635
- hdu 3635
- hdu 3635 Dragon Balls
- HDU-3635 Dragon Balls
- HDU 3635 Dragon Balls
- HDU-3635-Dragon Balls
- hdu 3635 Dragon Balls
- HDU 3635 Dragon Balls
- HDU 3635 Dragon Balls
- HDU 3635 Dragon Balls
- HDU 3635 Dragon Balls
- JS 对象
- cocos2d-x 数学函数、常用宏粗整理
- Unity 获取Terrain大小的方法
- defunct进程
- 中断处理中tasklet与工作队列的使用
- hdu 3635
- CentOS6.4+transmission-daemon
- hdu 4608 I-number 模拟
- application 使用
- JVM内存原理,weblogic内存的调优
- Android RelativeLayout 属性
- UITableView 详解 教程
- 压力大每天吃枣 协助分泌肾上腺皮质素
- gcc中内嵌汇编的用法