poj 1962 Corporative Network
来源:互联网 发布:开淘宝店货源从哪里来 编辑:程序博客网 时间:2024/06/02 19:46
题目大意:给出一些点
E x操作是统计x点到他的根节点的长度
I x y操作是将y点当作x点的父节点
两点之间的距离是|x-y|%1000
一般解法为记录这些节点的关系和两点之间的距离,然后遇到E累加求出到其跟的距离,但此做法会超时。
其优化方法为在每次统计距离的操作的的同时,把这个点直接连接到它的根节点,并且距离值改为统计出
的距离。这样,在每次统计其他节点到根节点的时候时候,会略过之间以算过的距离,避免重复计算。
#include"stdio.h"#include"string.h"int par[20005];int num[20005];int i,s;int find(int x)//重点操作{ for(i=par[x];par[i]!=0;i=par[i]) num[x]+=num[i];//求出并改变距离值 par[x]=i;//把点直接连接到根节点上 return num[x];}int main(){ int Case; int n; int x,y; char c; scanf("%d",&Case); while(Case--) { scanf("%d",&n); getchar(); for(x=1;x<=n;x++) { num[x]=0; par[x]=0; } while((c=getchar())!='O') { if(c=='E') { scanf("%d",&x); printf("%d\n",find(x)); } else { scanf("%d%d",&x,&y); par[x]=y; num[x]=(x-y>0?(x-y):(y-x))%1000; } getchar(); } } return 0;}
- poj 1962 Corporative Network
- POJ-1962-Corporative Network
- POJ 1962 Corporative Network
- POJ-1962 Corporative Network
- POJ 1962 Corporative Network
- POJ 1962-Corporative Network
- poj 1962 Corporative Network
- POJ 1962 Corporative Network
- 【35.86%】【POJ 1962】Corporative Network
- 并查集 Corporative Network POJ 1962
- poj Corporative Network
- poj 1963 Corporative Network
- poj 1962 Corporative Network 并查集的应用
- poj 1962 Corporative Network 带权并茶集
- POJ 1962 Corporative Network 并查集+权值
- poj 1962 Corporative Network(并查集)
- POJ 1962/ ZOJ 2178: Corporative Network - 并查集
- poj 1962 Corporative Network(带权并查集)
- 跟我一起写 Makefile
- 恢复Joomla管理员密码
- poj 1517 u Calculate e
- 骗子改电脑配置信息兜售垃圾,怕上当进来学习一下
- Android中如何做到Service被关闭后又自动启动
- poj 1962 Corporative Network
- Oracle复习(一)
- 高质量C++编程_第7章_内存管理(2)
- SecureCRT登录主机慢
- j2ee中下载文件,js调用action,及获得tomcat的物理路径等知识点
- 日期控件
- 在衡量web用程序的性能时,哪些性能指标是比较重要的?
- 问题的提出方式。(【先】说出最关键的【结果】,然后再描述细节)
- android退出提示