hdu 3635
来源:互联网 发布:编程入门软件 编辑:程序博客网 时间:2024/06/12 09:53
一个并查集,别人都是开了一个struct,然后利用递归计算转移
,我这个方法用时会多一点。略显笨,但直观。
#include<cstdio>#include<cstring>const int maxn = 10000 + 5;int pre[maxn];int num[maxn];int croot(int x){ int cnt = 0; while(x != pre[x]) { cnt++; x = pre[x]; } return cnt;}int root(int x){ while(x != pre[x]) x = pre[x]; return x;}void merge_set(int x, int y){ int fx = root(x); int fy = root(y); //printf("%d %d %d\n", fx, fy, y); if(fx != fy) { pre[fx] = fy; num[fy] = num[fy] + num[fx]; }}int main(){ int T; scanf("%d", &T); for(int kase = 1; kase <= T; kase++) { int n, q; scanf("%d%d", &n, &q); //printf("%d %d\n", n, q); for(int i = 0; i <= n; i++) { pre[i] = i; //printf("%d %d\n", i, pre[i]); } //printf("%d\n", pre[3]); for(int i = 0; i <= n; i++) num[i] = 1; char cmd[10]; printf("Case %d:\n", kase); while(q--) { scanf("%s", cmd); if(cmd[0] == 'T') { int t1, t2; scanf("%d%d", &t1, &t2); merge_set(t1, t2); } if(cmd[0] == 'Q') { int temp; scanf("%d", &temp); printf("%d %d %d\n", root(temp), num[root(temp)], croot(temp)); } } } return 0;}
,我这个方法用时会多一点。略显笨,但直观。
阅读全文
0 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
- Linux查看系统配置常用命令
- 欢迎使用CSDN-markdown编辑器
- C语言宏定义##连接符和#符的使用
- 工厂设计模式
- TortoiseSVN checkout,导入到Eclipse不显示svn信息
- hdu 3635
- GetLastError()返回值列表
- Hdu 1025 Constructing Roads In JGShining's Kingdom(DP)
- vue2.0父子组件双向绑定
- 【Redis源码剖析】
- hjr-JAVA工作日记(八):本地模拟线上环境和重写
- TimeWait状态说明
- 批处理删除log文件夹及文件
- a标签置灰不可点击