LA 3644 X-Plosives && LA 3027 Corporative Network (并查集入门)
2015-08-03 20:20
375 查看
题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=12648 and http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33982
题意:两题都是比较裸的并查集。。。LA3644判断环路,LA3027算点到根节点的距离。
分析:用带路径压缩的并查集查找操作即可。
LA3644代码:
LA3027代码:
题意:两题都是比较裸的并查集。。。LA3644判断环路,LA3027算点到根节点的距离。
分析:用带路径压缩的并查集查找操作即可。
LA3644代码:
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int maxn = 1e5+6; int f[maxn]; int Find(int x) { if(f[x]==x) return x; return f[x]=Find(f[x]); } int main() { int x,y,i,j,fx,fy,ans; while(scanf("%d",&x)==1) { for(i=0;i<maxn;i++) f[i]=i; ans=0; while(x!=-1) { scanf("%d",&y); fx=Find(x); fy=Find(y); if(fx==fy) ans++; else f[fx]=fy; scanf("%d",&x); } printf("%d\n",ans); } return 0; }
LA3027代码:
#include <iostream> #include <cstdio> using namespace std; const int maxn = 2e5+6; const int mod =1000; int dis[maxn],f[maxn]; int ABS(int a) { if(a<0) return -a; return a; } struct node { int d,pa; node(int a=0,int b=0) :d(a),pa(b){} }; node Find(int x) { if(f[x]==x) return node(0,x); node temp=Find(f[x]); f[x]=temp.pa; dis[x]=dis[x]+temp.d; return node(dis[x],f[x]); } int main() { int n,u,d,p,ncase; char str[10]; scanf("%d",&ncase); while(ncase--) { scanf("%d",&n); for(int i=0;i<maxn;i++) { dis[i]=0; f[i]=i; } while(scanf("%s",str)&&str[0]!='O') { if(str[0]=='E') { scanf("%d",&u); Find(u); printf("%d\n",dis[u]); } else { scanf("%d%d",&u,&p); f[u]=p; dis[u]=ABS(u-p)%mod; } } } return 0; }
相关文章推荐
- JNI_最简单的Java调用C/C++代码(自己整理)
- linux_c 开发(5-5)进程间通讯_消息队列
- Java文件字节流和字符流FileInputStream,FileOutputStream,FileReader,FileWriter
- ASCII表
- 把 >/dev/null 2>&1 干掉!
- 输入与输出函数(printf、putchar、puts和scanf、getchar)
- 逆置字符串
- HDU 1532 Drainage Ditches(最大流)
- UIKit 选项卡栏控制器 UITabBarController (未完成)
- JAVA中volatile关键字的含义
- Codeforces 547 Div1 A. Mike and Frog(找循环节)
- day14
- MySQL列属性约束及存储引擎
- OSG漫游:实现按键控制的操作器
- hdu 2079 选课时间(题目已修改,注意读题) 母函数
- 鸟哥的Linux私房菜 基础学习篇(第三版)
- javascript里Dom问题之document.getElementById('xx').innerHTML
- 循环语句输出三角形
- 比较字符串的大小后再按从小到大的顺序排列
- matlab结果输出到文本的方法