http://poj.org/problem?id=2831

Can We Build This One?
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 1163   Accepted: 435
Case Time Limit: 2000MS

Description

“Highways are built, then life is rich.” Now people of Big Town want to become rich, so they are planning to build highways to connect their villages.

Big Town is really big and has many villages. Its people plan to build some highways between some pairs of villages so that every pair of villages is connected by the highways either directly or indirectly. After surveying the geographical surroundings, they find that there are some paths along with highways can be built. Every path is denoted by a triplet (abc) which means a highway can built between the a-th village and the b-th village with a cost of c. In order to save money, they will select only part of the paths to build highways along so that the total cost to build highways along the selected paths is minimal under the condition that every pair of villages is connected.

It is possible that multiple such selections exist. People from every village want to have those highways of good interest to them built. But some highways can never appear in the selection since they are much too costly. So people ask whether a certain highway can be selected if they agree to cut the cost. Your task is to design a program to answer their queries.

Input

The first line of input contains three integers NM and Q (1 < N ≤ 1,000, N − 1 ≤ M ≤ 100,000, 0 < Q ≤ 100,000), where N is the number of villages, M is the number of paths, and Q is the number of queries. Each of the next M lines contains three integers ab, and c (1 ≤ ab ≤ Na ≠ b, 0 ≤ c ≤ 1,000,000). The triplet (abc) describes a path. Each of following Q lines contains two integer i and x (1 ≤ i ≤ M, 0 ≤ x) describing a query, “Can a highway be built along the i-th path if the cost of is reduced to x?” x is strictly lower than the original cost of building a highway along the i-th path. It is assumed that every pair of village will be connected either directly or indirectly if all possible highways are built. And there may be more than one highway that can be built between a pair of villages.

Output

Output one line for each query. Output either “Yes” or “No” as the answer to the the query.

Sample Input

3 4 3
1 2 10
1 3 6
2 3 4
1 3 7
4 6
1 7
1 5

Sample Output

Yes
No
Yes

题意:村庄之间要修高速公路,让这n个村庄可以连通,给出可以建公路的建议,然后让你选择一些公路,其实就是最小生成树,但是询问的是如果把某条边的费用削减成x,问x是否可以有被选择的可能;

分析:首先任意选择一个最小生成树,对于没有选择的边枚举,每次加入一条边权值w,然后肯定会构成一个环,找出该环中除了这条边之外费用最大的边p;如果削减后的边w小于等于p,则可以构成最小生成树的组成成分;

程序:

#include"stdio.h"
#include"string.h"
#include"stack"
#include"math.h"
#include"algorithm"
#include"stdlib.h"
#define M 1009
#define inf 100000000
#define eps 1e-12
using namespace std;
struct node
{int u,v,w,next,id,mark;
}e[100009],edge[M*2];
int f[M],head[M],t,pre[M],deep[M],prep[M];
int num[100009],use[100009];
void init()
{t=0;memset(head,-1,sizeof(head));
}
void add(int u,int v,int w)
{edge[t].u=u;edge[t].v=v;edge[t].w=w;edge[t].next=head[u];head[u]=t++;
}
int finde(int x)
{if(x!=f[x])f[x]=finde(f[x]);return f[x];
}
void dfs(int u,int f,int id)
{deep[u]=id;for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].v;if(v==f)continue;pre[v]=u;prep[v]=edge[i].w;dfs(v,u,id+1);}
}
int Lca(int u,int v//Lca搜最大的边
{int maxi=-inf;while(u!=v){if(deep[u]>deep[v]){maxi=max(maxi,prep[u]);u=pre[u];}else{maxi=max(maxi,prep[v]);v=pre[v];}}return maxi;
}
int cmp(node a,node b)
{return a.w<b.w;
}
int main()
{int n,m,k,i,x;while(scanf("%d%d%d",&n,&m,&k)!=-1){for(i=0;i<m;i++){scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);e[i].id=i;}sort(e,e+m,cmp);for(i=0;i<m;i++)num[e[i].id]=i;for(i=1;i<=n;i++)f[i]=i;memset(use,0,sizeof(use));init();for(i=0;i<m;i++){int x=finde(e[i].u);int y=finde(e[i].v);if(x!=y){f[x]=y;use[i]=1;add(e[i].u,e[i].v,e[i].w);add(e[i].v,e[i].u,e[i].w);}}dfs(1,1,1);for(i=0;i<m;i++)//枚举不在最小生成树里面的边if(!use[i])e[i].mark=Lca(e[i].u,e[i].v);//记录可以成为最小生成树的边的上限while(k--){scanf("%d%d",&i,&x);i--;if(use[num[i]])printf("Yes\n");else{if(x<=e[num[i]].mark)printf("Yes\n");elseprintf("No\n");}}}return 0;
}

转载于:https://www.cnblogs.com/mypsq/p/4348178.html

最小生成树(削减某条边后该边能否构成最小生成树的成分)相关推荐

  1. 用Python分析《工作细胞》的一万多条评论后,非漫迷也要入番了

    作者 | 量化小白一枚,上财研究生在读,专注于数据分析与量化投资 来源 | 量化小白上分记(公众号 id:quanthzp) 责编 | Jane 人工智能的现状及今后发展趋势如何?  https:// ...

  2. sql 统计记录条数后 打印出所有记录_用SQL完成购买行为分析(下篇II)

    (接<用SQL完成购买行为分析(下篇I)>内容) 12)查询首条记录为fav,总记录条数为14的记录.将前面getNum(3)红框处替换为12,运行getNum(14)得到第2条记录的数量 ...

  3. linux断电关机后,进度条满后卡在那里

    系统 :CentOS 6.5 故障:系统启动后,进度条满了,但是不显示登陆提示符 解决方法: 1.重启后按 esc 或 f5 键,可以查看系统启动过程 可以发现,在certomonger 启动 OK ...

  4. select超过固定条数后出现滚动条_12万公里的路虎维修,两个小小的胶套损坏,导致两条后轮胎偏磨!...

    前段时间,汽修师傅维修了一辆路虎,两个小小的后轴承座胶套损坏,导致车辆行驶在不平路面有异响,四轮定位数据也发生变化,两后轮出现偏磨,方向盘也不正. 经过仔细检查,师傅给出的维修建议是:更换两个轴承座胶 ...

  5. mysql查询表中前一条和后一条数据

    标题 1.查询前一条记录:select * from 表名 where id = (select id from 表名 where ranking < #{ranking,jdbcType=BI ...

  6. 安装matlab2014b软件时,在安装进度条完成后,没有弹出产品配置说明

    1.问题描述:安装matlab2014b软件时,在安装进度条完成100%后,就没有了页面,没有弹出下一步的产品配置说明. -解决方案:去matlab安装目录下找到bin文件夹,点击matlab.exe ...

  7. 基于Vue+element表格删除最后一页的最后一条数据后回显数据为空解决

    当我们在删除最后一页的最后一条数据时,我们想要的效果是回到上一页,查询处上一页的数据显示出来,当前页码变成上一页,而真正删除的时候显示出来是个空的,而且页码也还在当前页 这时我们就需要去判断当前删除的 ...

  8. 教程,使用YCSB测试MYSQL数据库,获取千万条测试后的数据

    Yahoo! Cloud Serving Benchmark (YCSB)是一个数据库特别是nosql数据库性能测试的benchmark.在GitHub中有3.9千个star和1.9千个forks.本 ...

  9. oracle或mysql分组查询并且获取前3条排序后的数据

    因为这个需求,所以百度找到了对于不同数据库的两种写法并附带上自己的浅显的理解,如果大家有更好的理解的方式或者更好的写法,请在评论处奉上您的想法,再次拜谢了 mysql : select a.* fro ...

最新文章

  1. 依赖类型dependency type在maven中的作用
  2. Linux centos 6.7设置MySQL为开机启动
  3. SAP UI5 control id generation by jQuery
  4. 不停机上线服务_【必看】10月25日本市增值税发票管理系统停机前,这些事一定要办...
  5. html无损转换pdf,Pdf2html :高保真PDF至HTML转换
  6. Spring Boot文档阅读笔记-Creating Asynchronous Methods解析
  7. Eureka-zookeeper的服务发现替代方案
  8. [转载]taking photos with live image preview
  9. PXe Server Install for Linux
  10. C/C++静态代码检查工具CodeChecker(一)简介
  11. 析砂性土层php泥浆护壁,土未工程施工习题集2
  12. 现代数字信号处理课后作业【第六章】
  13. 常用十六进制颜色对照表代码
  14. 学习总结1-跟开涛学SpringMVC
  15. 赛扬J4105和赛扬N5095哪个好
  16. java得到某时间前2小时的时间
  17. linux最后一行awk,51CTO博客-专业IT技术博客创作平台-技术成就梦想
  18. 常用财务软件有哪些功能模块
  19. 帧中继网络与NBMA/P2MA
  20. 用 FC FOR nexenta 山寨SAN存储

热门文章

  1. c c++ sizeof
  2. rollup配置及使用
  3. java提示框easyui风格_[Java教程]jQuery EasyUI 提示框(Messager)用法
  4. 如果工作时间固定,居住城市是可以实现的
  5. 在职场中,什么是职场大忌?
  6. Panorama是什么意思
  7. 我想在杭州买一套房一百平米左右的房子大概多少钱?
  8. 创业者在创业时经常会问到的一个问题
  9. 读书和不读书有什么区别呢?
  10. 微信用久了,越来越占内存怎么办?