*浙大PAT甲级 1114 并查集
来源:互联网 发布:淘宝投放手机详情 编辑:程序博客网 时间:2024/06/06 02:11
考察并查集,并附加记录额外信息。
为了得到一个家族中的最小id,我们可以将最小的id做为这个家族的根。并将整个家族的信息如家族人数,家族房产数,家族的房产面积。
然后通过排序,输出相应结果。
AC代码:
#include<iostream>#include<map>#include<cstdio>#include<algorithm>#include<queue>#include<cstring>#include<set>#include<stack>#include<cmath>#include<vector>#include<hash_map>#define ll long long#define inf 24*60*60using namespace std;int father[10000];int mark[10000];struct node{ int id; int shu=1; double sets=0; double area=0;};node bb[10000];vector<node> v;int findroot(int x){ if(father[x]==-1) return x; else { int tmp=findroot(father[x]); father[x]=tmp; return tmp; }}bool cmp(node x,node y){ if(fabs(x.area/x.shu-y.area/y.shu)<1e-5) { return x.id<y.id; } else { return x.area/x.shu>y.area/y.shu; }}int main(){ int n; scanf("%d",&n); memset(father,-1,sizeof(father)); for(int i=0;i<n;i++) { int d; scanf("%d",&d); int lp,rp; scanf("%d %d",&lp,&rp); int root1=findroot(d); bb[d].id=d; mark[d]=1; if(lp!=-1) { int root2=findroot(lp); mark[lp]=1; if(root1!=root2) { int minn=min(root1,root2); int maxx=max(root1,root2); father[maxx]=minn; bb[minn].sets+=bb[maxx].sets; bb[maxx].sets=0; bb[minn].area+=bb[maxx].area; bb[maxx].area=0; bb[minn].shu+=bb[maxx].shu; bb[maxx].shu=0; bb[minn].id=minn; root1=minn; } } if(rp!=-1) { int root2=findroot(rp); mark[rp]=1; if(root1!=root2) { int minn=min(root1,root2); int maxx=max(root1,root2); father[maxx]=minn; bb[minn].sets+=bb[maxx].sets; bb[maxx].sets=0; bb[minn].area+=bb[maxx].area; bb[maxx].area=0; bb[minn].shu+=bb[maxx].shu; bb[maxx].shu=0; bb[minn].id=minn; root1=minn; } } int k; scanf("%d",&k); for(int j=0;j<k;j++) { int c; scanf("%d",&c); mark[c]=1; int root2=findroot(c); if(root1!=root2) { int minn=min(root1,root2); int maxx=max(root1,root2); father[maxx]=minn; bb[minn].sets+=bb[maxx].sets; bb[maxx].sets=0; bb[minn].area+=bb[maxx].area; bb[maxx].area=0; bb[minn].shu+=bb[maxx].shu; bb[maxx].shu=0; bb[minn].id=minn; root1=minn; } } double sets; double area; scanf("%lf %lf",&sets,&area); bb[root1].sets+=sets; bb[root1].area+=area; //cout<<root1<<endl; } //cout<<1<<endl; int ans=0; for(int i=0;i<10000;i++) { if(mark[i]==1&&bb[i].shu!=0) { ans++; v.push_back(bb[i]); } } sort(v.begin(),v.end(),cmp); printf("%d\n",ans); for(int i=0;i<v.size();i++) { printf("%04d %d %.3lf %.3lf\n",v[i].id,v[i].shu,v[i].sets/v[i].shu,v[i].area/v[i].shu); }}
0 0
- *浙大PAT甲级 1114 并查集
- *浙大PAT甲级 1107 并查集
- 浙大pat甲级 1023
- 浙大pat甲级 1024
- 浙大pat甲级 1025
- 浙大PAT甲级 1026
- 浙大PAT甲级 1028
- 浙大PAT甲级 1029
- 浙大PAT甲级 1030
- 浙大PAT甲级 1031
- 浙大PAT甲级 1032
- 浙大PAT甲级 1033
- 浙大PAT甲级 1035
- 浙大PAT甲级 1037
- 浙大PAT甲级 1039
- 浙大PAT甲级 1040
- 浙大PAT甲级 1038
- 浙大PAT甲级 1041
- FFmpeg简介
- JDK 1.6 与JDK1.7的区别
- location.herf 和 location.replace
- 百度传课 amazeUI 按钮交互 作业
- mina源码学习之IoSession
- *浙大PAT甲级 1114 并查集
- 汉诺塔系列1
- OpenGL实现二维图的平移
- Elasticsearch笔记六之中文分词器及自定义分词器
- hdu 5437优先队列
- 【工控机开发】‘close' was not declared in this scope
- WIN32 进程间通讯-共享内存
- java io流详解(二)对象输入输出流
- java例题学习:面向对象之员工类