博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu1962Corporative Network带权回路
阅读量:7187 次
发布时间:2019-06-29

本文共 1201 字,大约阅读时间需要 4 分钟。

1 /* 2     有N个企业,每个企业想要实现通信,要用线路来连接,线路的长度为abs(a-b)%1000; 3     如果企业a 链接到了企业b 那么b就是the center of the serving! 4     然后有两种操作: 5     E a : 输出企业a到serving center 的线路的距离 6     I a, b  将企业a连接到企业 b,那么b就成为了serving center(之前连接a的企业,他们的serving center也变成了b)  7      8    思路:并查集! (压缩路径时回溯求解) !  9 */ 10 #include
11 #include
12 #include
13 #include
14 #define M 2000515 using namespace std;16 int n;17 int f[M];18 int ans[M];//节点 i到 serving center的距离! 19 20 int getFather(int x){21 if(x==f[x]) return x;22 int ff=getFather(f[x]);23 ans[x]+=ans[f[x]];//节点x到serving center 的距离要加上其父节点到serving center的距离! 24 return f[x]=ff;25 }26 27 void Union(int a, int b){ 28 if(a==b) return;29 f[a]=b;30 ans[a]=abs(a-b) % 1000;31 }32 33 int main(){34 int t;35 char ch[3];36 cin>>t;37 while(t--){38 cin>>n;39 int a, b;40 memset(ans, 0, sizeof(ans));41 for(int i=1; i<=n; ++i)42 f[i]=i;43 while(cin>>ch && ch[0]!='O'){44 if(ch[0]=='E'){45 cin>>a;46 getFather(a);47 cout<
<
>a>>b;51 Union(a, b);52 }53 }54 }55 return 0;56 }

 

转载地址:http://awykm.baihongyu.com/

你可能感兴趣的文章
理解加密算法
查看>>
Linux运维人员成长之路学习书籍推荐(未删减版)
查看>>
Visual Studio常用插件整理
查看>>
swift编程入门教程
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
RIP、端口DHCP
查看>>
MySQL Explain命令详解--表的读取顺序,数据读取操作的类型等
查看>>
Windows 10剪贴板历史记录清理
查看>>
使用zabbix的ICMP Ping模版实现对客户端网络状态的监控
查看>>
90后美女的全能测试蜕变之路
查看>>
单网卡安装pptpd
查看>>
Cisco路由器配置ADSL上网
查看>>
qemu-kvm 启动
查看>>
IPSec ×××配置总结 2
查看>>
ImportFileHandler 附件上传
查看>>
curl提示不支持https协议解决方法
查看>>
Spring定时任务@Scheduled的cron表达式
查看>>
centos 防火墙设置
查看>>
SSH Secure Shell Client
查看>>