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

Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 14383   Accepted: 5063
Case Time Limit: 1000MS

Description

Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this problem consists of the same input as in "Navigation Nightmare",followed by a line containing a single integer K, followed by K "distance queries". Each distance query is a line of input containing two integers, giving the numbers of two farms between which FJ is interested in computing distance (measured in the length of the roads along the path between the two farms). Please answer FJ's distance queries as quickly as possible! 

Input

* Lines 1..1+M: Same format as "Navigation Nightmare"

* 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

* Lines 1..K: For each distance query, output on a single line an integer giving the appropriate distance. 

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

Farms 2 and 6 are 20+3+13=36 apart. 

Source

 
 
方向~~~忽悠人的。。。
 #include <algorithm>
#include <cstdio> using namespace std; const int N();
char s[];
int n,m,dis[N],head[N],sumedge;
struct Edge
{
int v,w,next;
Edge(int v=,int next=,int w=):
v(v),next(next),w(w){}
}edge[N<<];
inline void ins(int u,int v,int w)
{
edge[++sumedge]=Edge(v,head[u],w);
head[u]=sumedge;
} int size[N],top[N],dad[N],son[N],deep[N];
void DFS(int u,int fa,int deepth)
{
dad[u]=fa;
size[u]=;
deep[u]=deepth;
for(register int v,i=head[u];i;i=edge[i].next)
{
v=edge[i].v;
if(dad[u]==v) continue;
dis[v]=dis[u]+edge[i].w;
DFS(v,u,deepth+);
size[u]+=size[v];
if(size[son[u]]<size[v]) son[u]=v;
}
}
void DFS_(int u,int Top)
{
top[u]=Top;
if(son[u]) DFS_(son[u],Top);
for(int v,i=head[u];i;i=edge[i].next)
{
v=edge[i].v;
if(dad[u]!=v&&son[u]!=v) DFS_(v,v);
}
}
int LCA(int x,int y)
{
for(;top[x]!=top[y];x=dad[top[x]])
if(deep[top[x]]<deep[top[y]]) swap(x,y);
return deep[x]<deep[y]?x:y;
} int main()
{
scanf("%d%d",&n,&m);
for(int u,v,w;m--;)
{
scanf("%d%d%d%s",&u,&v,&w,s);
ins(u,v,w); ins(v,u,w);
}
DFS(,,); DFS_(,);
int k;scanf("%d",&k);
for(int u,v;k--;)
{
scanf("%d%d",&u,&v);
printf("%d\n",dis[v]+dis[u]-dis[LCA(u,v)]*);
}
return ;
}

POJ——T 1986 Distance Queries的更多相关文章

  1. POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 【USACO】距离咨询(最近公共祖先)

    POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 [USACO]距离咨询(最近公共祖先) Description F ...

  2. POJ.1986 Distance Queries ( LCA 倍增 )

    POJ.1986 Distance Queries ( LCA 倍增 ) 题意分析 给出一个N个点,M条边的信息(u,v,w),表示树上u-v有一条边,边权为w,接下来有k个询问,每个询问为(a,b) ...

  3. POJ 1986 Distance Queries 【输入YY && LCA(Tarjan离线)】

    任意门:http://poj.org/problem?id=1986 Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total ...

  4. POJ 1986 Distance Queries LCA两点距离树

    标题来源:POJ 1986 Distance Queries 意甲冠军:给你一棵树 q第二次查询 每次你问两个点之间的距离 思路:对于2点 u v dis(u,v) = dis(root,u) + d ...

  5. POJ 1986 Distance Queries(Tarjan离线法求LCA)

    Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 12846   Accepted: 4552 ...

  6. poj 1986 Distance Queries LCA

    题目链接:http://poj.org/problem?id=1986 Farmer John's cows refused to run in his marathon since he chose ...

  7. 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 ...

  8. 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 ...

  9. POJ 1986 Distance Queries(LCA Tarjan法)

    Distance Queries [题目链接]Distance Queries [题目类型]LCA Tarjan法 &题意: 输入n和m,表示n个点m条边,下面m行是边的信息,两端点和权,后面 ...

随机推荐

  1. 初识Git(一)

    以前经常听到Git,作为一个菜鸟级自学选手从没有真正去了解Git,借刘铁猛老师在油管上的<对答如刘>初步认识了git,作以下记录. 1.初始化一个git管理的文件夹 首先我在我的电脑C盘P ...

  2. 解决new Date的值为Invalid Date、NaN-NaN的问题

    错误代码: let timespan = 1515239514230; let dateTime = new Date(timespan); console.log(dateTime) // 返回 I ...

  3. BZOJ 2154/2693 Crash的数字表格/jzptab (莫比乌斯反演)

    题目大意:求$\sum_{i=1}^{n}\sum_{j=1}^{m}lcm(i,j)$的和 易得$\sum_{i=1}^{n}\sum_{j=1}^{m}\frac{ij}{gcd(i,j)}$ 套 ...

  4. exe文件作为服务启动

    一. 准备软件 instsrv.exe srvany.exe 这两个都是 Microsoft Windows Resource Kits 里面的小工具 链接:http://pan.baidu.com/ ...

  5. Hibernate简单的保存操作

    1.这里面我想先说一下session对象的创建,这个是我们操纵数据库的核心对象,因此首先我们应该获取相应的session对象. public static Configuration cfg; pub ...

  6. 紫书 例题8-6 UVa 1606(扫描法)

    这道题目用扫描法 扫描法:在枚举的过程中维护一些重要的量, 从而简化计算 这道题用到了极角, 叉积, 高一的我表示一脸懵逼 不过自己去百度了一下好像大概看得懂. 这道题我还有一些疑问, 先这样吧 #i ...

  7. PKU 2184 Cow Exhibition 01背包

    题意: 有一些牛,每头牛有一个Si值,一个Fi值,选出一些牛,使得max( sum(Si+Fi) ) 并且 sum(Si)>=0, sum(Fi)>=0 思路: 随便选一维做容量(比如Fi ...

  8. MATLAB插值

    转自原文 MATLAB插值 插值问题 在应用领域中,由有限个已知数据点,构造一个解析表达式,由此计算数据点之间的函数值,称之为插值. 实例:海底探测问题 某公司用声纳对海底进行测试,在5×5海里的坐标 ...

  9. C++归并排序总结

    #include <iostream> using namespace std; //归并排序非递归版. void Sort(int a[], int n,int high) { int ...

  10. NSURLConnection和NSRunLoop

    主线程中创建一个NSURLConnection并异步运行 [self performSelectorOnMainThread:@selector(start) withObject:nil waitU ...