#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. Snort里如何将读取的包记录存到二进制tcpdump文件下(图文详解)

    不多说,直接上干货! 如果网络速度很快,或者想使日志更加紧凑以便以后的分析,那么应该使用二进制的日志文件格式.如tcpdump格式或者pcap格式.  这里,我们不需指定本地网络了,因为所以的东西都被 ...

  2. 重写java.lang.String IndexOf()方法,实现对字符串以ASCII规则截取

    /** * 根据元数据和目标ascii位数截取字符串,失败返回-1 * @param sourceStr 元数据字符串 * @param endIndex 截取到第几位 * @return 结果字符串 ...

  3. Spring-----注解开发和Spring测试单元

    一.注解开发 导入jar包;spring-aop-xxx.jar 导入约束:(在官方文档xsd-configuration.html可找) <beans xmlns="http://w ...

  4. vue HTTP请求(针对vue-resource)

    //初始化页面需要做什么事情 //点击后需要做什么事情 //鼠标.键盘.冒泡.默认行为等事件 //前端调用接口就是按需展示数据//created和mounted里面都可以做数据处理,唯一不同的是cre ...

  5. C++ 多态、虚函数、虚析构函数

    1.若某种语言只支持类但不支持多态,则只能称为基于对象,不能说是面向对象. 2.多态:向不同对象发送同一个消息,不同的对象会产生不同的行为,发送消息可以是调用函数等操作.函数重载.运算符重载都是多态. ...

  6. vue 发布build 本地设置 相对路径 两个地方 一个根目录用./ css文件里面用../../ 【也不好用,还是得手改】

    build: { // Template for index.html index: path.resolve(__dirname, '../dist/index.html'), // Paths a ...

  7. lua 之 三木运算符

    在c语言中我三目运算符这么写: a?b:c 例如: max = a>b?a:b; 在lua中我们这么写 max = a>b and a or b 运行如下:

  8. TabWight

    //修改站号void CDlgParamView::OnPushButton_2_Tab8Clicked(){ // int iSel = m_listStation.GetSelectionMark ...

  9. 使用 reduce 实现数组 map 方法

    //使用 reduce 实现数组 map 方法 const selfMap2 = function (fn, context){ let arr = Array.prototype.slice.cal ...

  10. js编码处理(转)

    1.       使用 JS 中的 encodeURIComponent 或 encodeURI 方法. 说明: encodeURIComponent(String) 对传递参数进行设置.不编码字符有 ...