http://acm.hdu.edu.cn/showproblem.php?pid=2874

给出一个森林,询问任意两点最短距离。

tarjan跑一遍即可,就是这个题卡内存,vector会MLE,换前向星就好了。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<set>
#include<stack>
#include<deque>
#include<bitset>
#include<unordered_map>
#include<unordered_set>
#include<queue>
#include<cstdlib>
#include<ctype.h>
#include<ctime>
#include<functional>
#include<algorithm>
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define inf 0x3f3f3f3f
#define debug puts("debug")
#define mid ((L+R)>>1)
#define lc (id<<1)
#define rc (id<<1|1)
const int maxn=;
const int maxm=;
const double PI=acos(-1.0);
const double eps=1e-;
const LL mod=1e9+;
LL gcd(LL a,LL b){return b==?a:gcd(b,a%b);}
LL lcm(LL a,LL b){return a/gcd(a,b)*b;}
LL qpow(LL a,LL b,LL c){LL r=; for(;b;b>>=,a=a*a%c)if(b&)r=r*a%c;return r;}
template<class T>
void prt(T v){for(auto x:v)cout<<x<<' ';cout<<endl;}
struct Edge{int v,w,next;}e,e1[],e2[];
int first1[maxn],first2[maxn],tot1,tot2;
void add1(int u,int v,int w){
e1[tot1].v=v;
e1[tot1].w=w;
e1[tot1].next=first1[u];
first1[u]=tot1++;
}
void add2(int u,int v,int w){
e2[tot2].v=v;
e2[tot2].w=w;
e2[tot2].next=first2[u];
first2[u]=tot2++;
} bool vis[maxn];
int f[maxn],d[maxn],ans[];
int getf(int u){return f[u]==u?u:f[u]=getf(f[u]);}
void tarjan(int u){
vis[u]=;
for(int i=first1[u];~i;i=e1[i].next){
e=e1[i];
int v=e.v,w=e.w;
if(!vis[v]){
d[v]=d[u]+w;
tarjan(v);
f[v]=u;
}
}
for(int i=first2[u];~i;i=e2[i].next){
e=e2[i];
int v=e.v,id=e.w;
if(vis[v]){
ans[id]=d[u]+d[v]-*d[getf(v)];
}
}
}
void read(int &n){
n=; char c=getchar();
while(c<''||c>'')c=getchar();
while(c>=''&&c<='') n=(n<<)+(n<<)+c-'',c=getchar();
}
int main(){
int t,n,m,i,j,u,v,w;
while(~scanf("%d%d%d",&n,&m,&t)){
for(i=;i<=n;++i)f[i]=i;
tot1=tot2=;
memset(first1,-,sizeof(first1));
memset(first2,-,sizeof(first2));
while(m--){
//scanf("%d%d%d",&u,&v,&w);
read(u),read(v),read(w);
add1(u,v,w);
add1(v,u,w);
}
for(i=;i<=t;++i){
ans[i]=-;
//scanf("%d%d",&u,&v);
read(u),read(v);
add2(u,v,i);
add2(v,u,i);
}
memset(d,-,sizeof(d));
for(i=;i<=n;++i){
if(d[i]==-) d[i]=,memset(vis,,sizeof(vis)),tarjan(i);
}
for(i=;i<=t;++i){
if(ans[i]==-) puts("Not connected");
else printf("%d\n",ans[i]);
}
}
return ;
}

HDU-2874-森林求LCA/tarjan的更多相关文章

  1. Connections between cities HDU - 2874(最短路树 lca )

    题意: 给出n个点m条边的图,c次询问 求询问中两个点间的最短距离. 解析: Floyd会T,所以用到了最短路树..具体思想为: 设k为u和v的最近公共祖先 d[i] 为祖结点到i的最短距离  则di ...

  2. Tarjan 算法求 LCA / Tarjan 算法求强连通分量

    [时光蒸汽喵带你做专题]最近公共祖先 LCA (Lowest Common Ancestors)_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili tarjan LCA - YouTube Tarj ...

  3. HDU 2874 Connections between cities(LCA Tarjan)

    Connections between cities [题目链接]Connections between cities [题目类型]LCA Tarjan &题意: 输入一个森林,总节点不超过N ...

  4. 【HDOJ2586】【Tarjan离线求LCA】

    http://acm.hdu.edu.cn/showproblem.php?pid=2586 How far away ? Time Limit: 2000/1000 MS (Java/Others) ...

  5. 倍增 Tarjan 求LCA

                                                                                                         ...

  6. hdu 2874 Connections between cities [LCA] (lca->rmq)

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  7. 【Tarjan】洛谷P3379 Tarjan求LCA

    题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询问的个数和树根结点的序号. 接下来N-1行每 ...

  8. 倍增\ tarjan求lca

    对于每个节点v,记录anc[v][k],表示从它向上走2k步后到达的节点(如果越过了根节点,那么anc[v][k]就是根节点). dfs函数对树进行的dfs,先求出anc[v][0],再利用anc[v ...

  9. Tarjan求LCA

    LCA问题算是一类比较经典的树上的问题 做法比较多样 比如说暴力啊,倍增啊等等 今天在这里给大家讲一下tarjan算法! tarjan求LCA是一种稳定高速的算法 时间复杂度能做到预处理O(n + m ...

随机推荐

  1. .NET 介绍

    In order to continue our effort of being modular and well factored we don’t just provide the entire ...

  2. sublime text 中文显示异常

    本文链接:sublime text 中文显示异常 http://www.cnblogs.com/daysme/p/7549857.html 前言 sublime text 3143 用了一年时候,最近 ...

  3. java笔试总结

    1. Java的IO操作中有面向字节(Byte)和面向字符(Character)两种方式.面向字节的操作为以8位为单位对二进制的数据进行操作,对数据不进行转换,这些类都是InputStream和Out ...

  4. phpstorm软件配置端口问题

    phpstorm默认的端口号是:63342 但是我装的apache服务器的默认端口是80 网上查找资料,都说可以加listen的端口,比如这里   #Listen 12.34.56.78:80 Lis ...

  5. 使用外网访问阿里云服务器ZooKeeper

    参考网址: zookeeper单机/集群安装详解 使用外网访问阿里云服务器ZooKeeper 阿里云服务管理控制台 1. 阿里云ECS安装zookeeper 环境:我安装的是zookeeper3.4. ...

  6. idea中svn的使用教程

    引言:以下是idea集成小乌龟后的svn使用教程,一张图足以说明问题,后续如果遇到了比较复杂一点的问题再来补充. 参考文档:https://blog.csdn.net/liuhailiuhai12/a ...

  7. Oracle(限定查询1)

    3.1.认识限定查询 例如:如果一张表中有100w条数据,一旦执行了“SELECT * FROM 表”语句之后,则将在屏幕上显示表中的全部数据行的记录,这样即不方便浏览,也有可能造成死机的问题出现,所 ...

  8. yum的配置文件介绍

    yum 的配置文件分为两部分:main 和repository   main 部分定义了全局配置选项,整个yum 配置文件应该只有一个main.常位于/etc/yum.conf 中. reposito ...

  9. Codeforces 932D - Tree

    932D - Tree 思路: 树上倍增 anc[i][u]:u的2^i祖先 mx[i][u]:u到它的2^i祖先之间的最大值,不包括u pre[i][u]:以u开始的递增序列的2^i祖先 sum[i ...

  10. Codeforces 801C - Voltage Keepsake

    C. Voltage Keepsake 题目链接:http://codeforces.com/problemset/problem/801/C time limit per test 2 second ...