#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. L 裁纸片 贪心 + 模拟

    https://biancheng.love/contest-ng/index.html#/123/problems 如果只是输出最小的值,那么好办,a升序,b降序,这样是最优的. 但是需要次数,这就 ...

  2. Effective Java读书笔记完结啦

    Effective Java是一本经典的书, 很实用的Java进阶读物, 提供了各个方面的best practices. 最近终于做完了Effective Java的读书笔记, 发布出来与大家共享. ...

  3. CF599B Spongebob and Joke

    思路: 模拟,注意特判. 实现: #include <iostream> #include <cstdio> using namespace std; ], x[], y[], ...

  4. ES3之bind方法的实现模拟

    扩展Function原型方法,此处用myBind来模拟bind实现 Function.prototype.myBind = function(o /*,args*/){       //闭包无法获取t ...

  5. 机器学习-随机梯度下降(Stochastic gradient descent)和 批量梯度下降(Batch gradient descent )

    梯度下降(GD)是最小化风险函数.损失函数的一种常用方法,随机梯度下降和批量梯度下降是两种迭代求解思路,下面从公式和实现的角度对两者进行分析,如有哪个方面写的不对,希望网友纠正. 下面的h(x)是要拟 ...

  6. hibernate对象状态 的小问题

    Class classA{ List a; public void setA(List a) { this.a =a; } public List getA() { return this.a; } ...

  7. vuex的应用和解决的实际问题

    这是vuex的语法结构内容 简单的理解vuex: new Vue({ // state data () { return { count: 0 } }, // view template: ` < ...

  8. LeetCode137只出现一次的数字——位运算

    题目 题目描述:给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现三次.找出那个只出现一次的元素. 说明:你的算法应该具有线性时间的复杂度.你可以不使用额外的空间来实现吗? 思路 题 ...

  9. laravel JWTAuth实现api接口鉴权(基础篇)

    官网:https://jwt-auth.readthedocs.io 参考:https://learnku.com/articles/10885/full-use-of-jwt#99529f 1.to ...

  10. Solaris 默认Shell 修改

    ssh登陆远程的solaris 10,backspace出现乱码. ssh登陆远程的solaris 10默认Shell不是bash 把solaris10的shell环境改为bash就行 dev13% ...