带权并查集
来源:互联网 发布:mac py终端指的是什么 编辑:程序博客网 时间:2024/04/30 12:00
<pre name="code" class="cpp">
<pre name="code" class="cpp">//悟空寻找龙珠#include<iostream>#include<cstring>#include<cstdio>using namespace std;const int maxn=10000+7;int f[maxn],times[maxn],cnt[maxn];int find(int x){if(x==f[x]) return x;int t=f[x];f[x]=find(f[x]);times[x]+=times[t];return f[x];/* int r=x; while(r!=f[r]){ r=f[r];//查 ,查完后root就是祖宗 } int i=x,temp; while(i!=r){temp=f[i]; times[i]+=times[temp]; f[i]=r; i=temp; }//路径压缩 return r;*/}void join(int x,int y){ int fx=find(x); int fy=find(y); if(fx!=fy) {//若x,y的祖宗不一样 cnt[fy]+=cnt[fx]; //cnt[fx]=0; times[fx]=1; f[fx]=fy;//为何不是f[x]=fy; }} int main(){ freopen("data.in","r",stdin); int T,kase=0; scanf("%d",&T); while(T--){ printf("Case %d:\n",++kase); int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){//初始化 f[i]=i;//第i个龙珠的位置 cnt[i]=1;//第i个城市的龙珠数目 times[i]=0;//第i个龙珠移动次数,我觉得就是找了几次父类(路径压缩了的次数) } while(m--){ char cmd[5]; scanf("%s",cmd); int a,b; if(*cmd=='T'){ scanf("%d%d",&a,&b); join(a,b); }else if(*cmd=='Q'){ scanf("%d",&a); int temp=find(a); printf("%d %d %d\n",temp,cnt[temp],times[a]); } } } return 0;}
//poj 1182#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int maxn=50000+7;int f[maxn],w[maxn];int cnt,n;int find(int x){if(x!=f[x]){int t=f[x];f[x]=find(f[x]);w[x]=(w[x]+w[t])%3;}return f[x];} void join(int r,int x,int y){int fx=find(x);int fy=find(y);if(fx!=fy){f[fx]=fy;w[fx]=(w[y]+r-w[x]+3)%3;}}int istrue(int d,int x,int y){int fx,fy,r;if(x>n||y>n||(d==2&&x==y)) return 0;fx=find(x);fy=find(y);if(fx!=fy) return 1;else{if(w[x]==(d-1+w[y])%3) return 1;else return 0;}}int main(){int k;cnt=0;scanf("%d%d",&n,&k);for(int i=0;i<=n;i++){f[i]=i;w[i]=0;//权重 }while(k--){int x,y,d;scanf("%d%d%d",&d,&x,&y);if(!istrue(d,x,y)) cnt++;else join(d-1,x,y);}printf("%d\n",cnt);return 0;}
//poj 1962#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>using namespace std;const int maxn=20000+5,mod=1000;int f[maxn],w[maxn];int n;int find(int x){if(x!=f[x]){int t=f[x];f[x]=find(f[x]);w[x]=w[x]+w[t];}return f[x];}void join(int x,int y){int fx=find(x);int fy=find(y);if(fx!=fy){f[x]=y;w[x]=abs(x-y)%mod;}}void intialization(){for(int i=0;i<=n;i++){f[i]=i;w[i]=0;}}int main(){//freopen("data.in","r",stdin);int T;scanf("%d",&T);while(T--){scanf("%d",&n);intialization();while(1){char cmd[5];int x,y;scanf("%s",cmd);if(*cmd=='E'){scanf("%d",&x);find(x);printf("%d\n",w[x]);}else if(*cmd=='I'){scanf("%d%d",&x,&y);join(x,y);}else if(*cmd=='O') break;}}return 0;}
0 0
- 并查集--带权
- 带权并查集
- 带权并查集
- 带权并查集
- 带权并查集
- 带权并查集
- 带权并查集
- 带权并查集
- 带权并查集
- 并查集和带权并查集
- 并查集与带权并查集
- poj 1182 (带权并查集)
- hdu 2818 带权并查集
- hdu 3047 带权并查集
- hdu 3635 带权并查集
- poj1988&&hdu2818 带权并查集
- HDU 3635 带权并查集
- poj 1962 带权并查集
- leancloud 使用的坑lists
- 深度学习笔记(三):激活函数和损失函数
- 对链表排序
- Android Menu菜单
- Centos 学习之路
- 带权并查集
- Java之静态内部类(static class)
- MyBatis Insert获取自增主键ID
- XML解析--小项目实战(联系人管理系统)
- navigator.geolocation
- ORACLE sql语法的表示问题
- Linux常用命令
- 女友校招笔试编程题:网易测试岗-买苹果和求最大奇约数
- Android Fragment中嵌套Fragment,不显示view