#include<iostream>

#include<vector>

using namespace std;

const int N=40010;

int pre[N];//并查集

int visit[N];//是否经过

int ancestor[N];//祖先

int dis[N];//储存距离

int result[N];//储存结果

struct tre{

 int x,length;

};

vector<tre>tree[N];

struct  qu{

 int y,index;

};

vector<qu>qury[N];

void init(int n) {//初始化

 int i;

 for(i=1;i<=n;i++)

  tree[i].clear();

 for(i=1;i<=n;i++)

  qury[i].clear();

 memset(visit,0,sizeof(visit));

}

int find(int x) {//路径压缩

 if(x!=pre[x])

  pre[x]=find(pre[x]);

 return pre[x];

}

void infind(int x,int y) {//合并

 int f1=find(x);

 int f2=find(y);

    pre[f1]=f2;

 return ;

}

void tarjan(int u,int length) {

 visit[u]=1;

 dis[u]=length;//直接就可以的到距离

 ancestor[u]=u;

       pre[u]=u;

 int i;

 for(i=0;i<tree[u].size();i++) {

  int v=tree[u][i].x;

  if(visit[v]==0) {

   tarjan(v,length+tree[u][i].length);

   infind(u,v);

   ancestor[find(u)]=u;

  }

 }

  for(i=0;i<qury[u].size();i++) {

   int v=qury[u][i].y;

   if(visit[v])

    result[qury[u][i].index]=dis[u]+dis[v]-2*dis[ancestor[find(v)]];//重点

  }

  return ;

}

int main() {

 int n,m,i,j,k,a,b,c;

 char s[2];

 while(scanf("%d%d",&n,&m)!=EOF) {

  init(n);

  for(i=1;i<=m;i++) {

   scanf("%d%d%d%s",&a,&b,&c,s);

   tre h;

   h.x=b;

   h.length=c;

   tree[a].push_back(h);//储存

   h.x=a;

   h.length=c;

   tree[b].push_back(h);

  }

  scanf("%d",&k);

  for(i=1;i<=k;i++) {

   scanf("%d%d",&a,&b);

   qu h;

    h.y=b;

    h.index=i;

    qury[a].push_back(h);//储存

    h.y=a;

    h.index=i;

    qury[b].push_back(h);

  }

  tarjan(1,0);//

  for(i=1;i<=k;i++)

   printf("%d\n",result[i]);

 }

 return 0;

}

poj 1986tarjan模板题的更多相关文章

  1. A Plug for UNIX POJ - 1087(模板题 没啥好说的。。就用了一个map)

    题意: 几种插头,每一种都只有一个,但有无限个插头转换器,转换器(a,b) 意味着 可以把b转换为a,有几个设备,每个设备对应一种插头,求所不能匹配插头的设备数量 这个题可以用二分图做 , 我用的是最 ...

  2. poj 1330lca模板题离线算法

    #include<iostream> #include<vector> using namespace std; const int MAX=10001; int pre[MA ...

  3. Jungle Roads POJ - 1251 模板题

    #include<iostream> #include<cstring> #include<algorithm> using namespace std; cons ...

  4. POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]

    题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...

  5. POJ Oulipo KMP 模板题

    http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4 ...

  6. POJ 3264:Balanced Lineup(RMQ模板题)

    http://poj.org/problem?id=3264 题意:给出n个数,还有q个询问,询问[l,r]区间里面最大值和最小值的差值. 思路:RMQ模板题,开两个数组维护最大值和最小值就行. #i ...

  7. POJ 1741.Tree and 洛谷 P4178 Tree-树分治(点分治,容斥版) +二分 模板题-区间点对最短距离<=K的点对数量

    POJ 1741. Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 34141   Accepted: 11420 ...

  8. Sliding Window POJ - 2823 单调队列模板题

    Sliding Window POJ - 2823 单调队列模板题 题意 给出一个数列 并且给出一个数m 问每个连续的m中的最小\最大值是多少,并输出 思路 使用单调队列来写,拿最小值来举例 要求区间 ...

  9. POJ 1287 Networking【kruskal模板题】

    传送门:http://poj.org/problem?id=1287 题意:给出n个点 m条边 ,求最小生成树的权 思路:最小生树的模板题,直接跑一遍kruskal即可 代码: #include< ...

随机推荐

  1. 用for循环实现的菱形图案

    package com.wh.lingxing; import java.util.Scanner; public class LingXing { public static void main(S ...

  2. 事件模型的介绍与Button的ActionListener

    事件监听: 这是个很重要的概念,也是个很重要的模型,vb,vc都是这样用,甚至后面学的web框架也在用.    现在我们可以做很多按钮了吧,但是我们的按钮按它是没反应的,现在我们来看看怎么样才能让它有 ...

  3. serialize可以获取form表单里面的数值

    serialize属性 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  4. iOS-控件响应用户控制事件之事件处理

    事件处理 响应者对象 在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件.我们称之为“响应者对象” UIApplication.UIViewControlle ...

  5. Kotlin:数组、字符串模板

    一.数组 Kotlin 中的数组是带有类型参数的类,其元素类型被指定为相应的类型参数,使用 Array 类来表示, Array 类定义了 get 与 set 函数(按照运算符重载约定这会转变为 [ ] ...

  6. windows测试物理网络

    ping 192.168.10.88 -t ,参数-t是等待用户去中断测试 

  7. cce - 控制台中文环境

    语法 (SYNTAX) cce [-e program] 描述 (DESCRIPTION) 该程序是一个类似于 WZCE , yact 和 chdrv 的控制台中文平台.进入该环境后可以用“空格 + ...

  8. 除了上万的月薪之外,还有什么理由让我们必须学Python?

    虽然目前的编程语言有很多,但是基础语法上的概念,本质上都是相通的.可以做到一通百通.所以没有必要为了学哪门语言纠结太多. python是目前市面上,我个人认为是最简洁&&最优雅& ...

  9. 第3节 mapreduce高级:2、3、课程大纲&共同好友求取步骤一、二

    第五天课程大纲:1.社交粉丝的数据分析:求共同好友2.倒排索引的建立3.自定义inputFormat合并小文件 4.自定义outputformat5.分组求topN6.MapReduce的其他补充 了 ...

  10. linux 服务脚本

    #!/bin/bash # # chkconfig: # description: my_SERVICE_NAME is a my Service # # common function . /etc ...