poj——1986 Distance Queries
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 14392 | Accepted: 5066 | |
Case Time Limit: 1000MS |
Description
Input
* Line 2+M: A single integer, K. 1 <= K <= 10,000
* Lines 3+M..2+M+K: Each line corresponds to a distance query and contains the indices of two farms.
Output
Sample Input
7 6 1 6 13 E 6 3 9 E 3 5 7 S 4 1 3 N 2 4 20 W 4 7 2 S 3 1 6 1 4 2 6
Sample Output
13 3 36
Hint
Source
#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #define N 51000 using namespace std; int n,m,x,y,z,t,tot,ans; int fa[N],dis[N],top[N],deep[N],size[N],head[N]; struct Edge { int from,to,dis,next; }edge[N<<]; int read() { ,f=; char ch=getchar(); ; ch=getchar();} +ch-'; ch=getchar();} return x*f; } int add(int x,int y,int z) { tot++; edge[tot].to=y; edge[tot].dis=z; edge[tot].next=head[x]; head[x]=tot; } int dfs(int x) { size[x]=; deep[x]=deep[fa[x]]+; for(int i=head[x];i;i=edge[i].next) { int to=edge[i].to; if(fa[x]==to) continue; dis[to]=dis[x]+edge[i].dis; fa[to]=x,dfs(to);size[x]+=size[to]; } } int dfs1(int x) { ; if(!top[x]) top[x]=x; for(int i=head[x];i;i=edge[i].next) { int to=edge[i].to; if(fa[x]!=to&&size[t]<size[to]) t=to; } if(t) top[t]=top[x],dfs1(t); for(int i=head[x];i;i=edge[i].next) { int to=edge[i].to; if(fa[x]!=to&&to!=t) dfs1(to); } } int lca(int x,int y) { for(;top[x]!=top[y];x=fa[top[x]]) if(deep[top[x]]<deep[top[y]]) swap(x,y); if(deep[x]>deep[y]) swap(x,y); return x; } int main() { n=read(),m=read(); ;i<=m;i++) { x=read(),y=read(),z=read(); add(x,y,z),add(y,x,z); } dfs(),dfs1(); t=read(); ;i<=t;i++) { x=read(),y=read(); ans=dis[x]+dis[y]-*dis[lca(x,y)]; printf("%d\n",ans); } ; }
poj——1986 Distance Queries的更多相关文章
- POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 【USACO】距离咨询(最近公共祖先)
POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 [USACO]距离咨询(最近公共祖先) Description F ...
- POJ.1986 Distance Queries ( LCA 倍增 )
POJ.1986 Distance Queries ( LCA 倍增 ) 题意分析 给出一个N个点,M条边的信息(u,v,w),表示树上u-v有一条边,边权为w,接下来有k个询问,每个询问为(a,b) ...
- POJ 1986 Distance Queries LCA两点距离树
标题来源:POJ 1986 Distance Queries 意甲冠军:给你一棵树 q第二次查询 每次你问两个点之间的距离 思路:对于2点 u v dis(u,v) = dis(root,u) + d ...
- POJ 1986 Distance Queries 【输入YY && LCA(Tarjan离线)】
任意门:http://poj.org/problem?id=1986 Distance Queries Time Limit: 2000MS Memory Limit: 30000K Total ...
- POJ 1986 Distance Queries(Tarjan离线法求LCA)
Distance Queries Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 12846 Accepted: 4552 ...
- poj 1986 Distance Queries LCA
题目链接:http://poj.org/problem?id=1986 Farmer John's cows refused to run in his marathon since he chose ...
- POJ 1986 - Distance Queries - [LCA模板题][Tarjan-LCA算法]
题目链接:http://poj.org/problem?id=1986 Description Farmer John's cows refused to run in his marathon si ...
- poj 1986 Distance Queries(LCA)
Description Farmer John's cows refused to run in his marathon since he chose a path much too long fo ...
- POJ 1986 Distance Queries(LCA Tarjan法)
Distance Queries [题目链接]Distance Queries [题目类型]LCA Tarjan法 &题意: 输入n和m,表示n个点m条边,下面m行是边的信息,两端点和权,后面 ...
- poj 1986 Distance Queries 带权lca 模版题
Distance Queries Description Farmer John's cows refused to run in his marathon since he chose a pa ...
随机推荐
- svn工具的使用问题总结
前言: 最近在开发的时候,由于需求太多,开发周期长短不一,从主线上切了多个分支(一般不在主线trunk上开发,万一线上出问题可及时修改代码上线),在部分功能上线后,想把代码同步到新的分支上去,最开始的 ...
- docker-compose写法收集
version: '3.3' services: php: image: docker.ksyun.com/php7.:latest volumes: - ./env/log/apps:/data/l ...
- IO流-文件的写入和读取
1.文件写入 类: FileWriter继承自Writer(字符流基类之一,另外一个为Reader) 方法: writer(参数); 根据参数可以写入字符.字符数组.字符数组中的一部分.整型.字符串. ...
- 【BZOJ】3991: [SDOI2015]寻宝游戏 虚树+DFS序+set
[题意]给定n个点的带边权树,对于树上存在的若干特殊点,要求任选一个点开始将所有特殊点走遍后返回.现在初始没有特殊点,m次操作每次增加或减少一个特殊点,求每次操作后的总代价.n,m<=10^5. ...
- Netty 入门初体验
Netty简介 Netty是一款异步的事件驱动的网络应用程序框架,支持快速开发可维护的高性能的面向协议的服务器和客户端.Netty主要是对java 的 nio包进行的封装 为什么要使用 Netty 上 ...
- python初步学习-python模块之 os
os os 模块在运维工作中是很常用的一个模块.通过os模块调用系统命令.os模块可以跨平台使用. 在 import os的时候,建议使用import os而非from os import *.这样可 ...
- 8、V模型、W模型、H模型
软件测试&软件工程 ·软件测试与软件工程息息相关,软件测试是软件工程组成中不可或缺的一部分.·在软件工程.项目管理.质量管理得到规范化应用的企业,软件测试也会进行得比较顺利,软件测试发挥的价值 ...
- vs2012 error c4996: 'fopen': This function or variable may be unsafe
1>------ 已启动生成: 项目: 20130925, 配置: Debug Win32 ------1> stdafx.cpp1>d:\code\20130925\201309 ...
- TCP 传输控制协议(转)
开头先说几个协议: IP:网际协议 TCP:传输控制协议 Http:超文本传输协议 AMQP:高级消息队列协议 一:TCP是什么? TCP(Transmission Control Protocol ...
- LAMP网站架构解释
对于大流量.大并发量的网站系统架构来说,除了硬件上使用高 性能的服务器.负载均衡.CDN等之外,在软件架构上需要重点关注下面几个环节:使用高性能的操作系统(OS).高性能的网页服务器(Web Serv ...