hdoj 2874 Connections between cities 【Tarjan离线LCA】

题目:hdoj 2874 Connections between cities

题意:战争过后,一些城市毁坏了。意思图不连通,让你求任意两点的距离、

分析:很明显求LCA

但是图不连通,所以我们Tarjan的时候要对每个点进行。然后标记即可。

另外,这个题目卡vector,看来以后要学着用数组模拟邻接表了。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
#define N 10010
#define M 10010
#define Q 1000010
int head[N];
struct edge{
    int u,v,w,next;
}e[2*M];
int __head[N];
struct ask{
    int u,v,lca,next;
}ea[2*Q];
int dir[N],fa[N],vis[N];
int k;
inline void add_edge(int u ,int v ,int w ,int &k)
{
    e[k].u = u; e[k].v = v; e[k].w = w;
    e[k].next = head[u]; head[u] = k++;
}
inline void add_ask(int u ,int v ,int &k)
{
    ea[k].u = u; ea[k].v = v; ea[k].lca = -1;
    ea[k].next = __head[u]; __head[u] = k++;
}

int find(int x){
    return  x == fa[x] ? x : fa[x] = find(fa[x]);
}

void Tarjan(int u ,int c)
{
    vis[u] = c; fa[u] = u;
    for(int k=head[u]; k!=-1; k=e[k].next)
        if(!vis[e[k].v])
        {
            int v = e[k].v , w = e[k].w;
            dir[v] = dir[u] + w;
            Tarjan(v,c);
            fa[v] = u;
        }
    for(int k=__head[u]; k!=-1; k=ea[k].next)
        if(vis[ea[k].v] == c)
            ea[k].lca = ea[k^1].lca = find(ea[k].v);
}

void Clear(int n)
{
    memset(head,-1,sizeof(head));
    memset(__head,-1,sizeof(__head));
    memset(vis,0,sizeof(vis));
}
int main()
{
    int n,m,c;
    while(~scanf("%d%d%d",&n,&m,&c))
    {
        Clear(n);
        k = 0;
        for(int i=0;i<m;i++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            add_edge(x,y,z,k);
            add_edge(y,x,z,k);
        }
        k = 0;
        for(int i=0;i<c;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            add_ask(x,y,k);
            add_ask(y,x,k);
        }
        int cnt = 0;
        for(int i=1;i<=n;i++)
        {
            if(vis[i]==0){
                dir[i] = 0;
                Tarjan(i,++cnt);
            }
        }
        for(int i=0;i<c;i++)
        {
            int f = 2*i;
            int ans = dir[ea[f].u] + dir[ea[f].v] - 2* dir[ea[f].lca];
            //printf("LCA:%d\n",e[f].lca);
            if(ea[f].lca==-1)
                puts("Not connected");
            else
                printf("%d\n",ans);
        }
    }
    return 0;
}
时间: 2024-10-11 15:16:13

hdoj 2874 Connections between cities 【Tarjan离线LCA】的相关文章

hdu 2874 Connections between cities 带权lca判是否联通

Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description After World War X, a lot of cities have been seriously damaged, and we need to rebuild those cities. However, some mat

hdoj 2586 How far away ? 【Tarjan离线LCA】

题目:hdoj 2586 How far away ? 题意:给出一个有权树,求任意两点的之间的距离. 分析:思想就是以一个点 root 作为跟变成有根数,然后深搜处理处所有点到跟的距离.求要求的两个点的LCA(最近公共祖先), 然后ans = dis[x] + dis[y] - 2 * dis[LCA(x,y)],可以画图分析一下就知道. 求LCA我用的是Tarjan离线lca,由于询问次数很多,所以这个比较快. AC代码: #include <iostream> #include <

hdu 2874 Connections between cities(lca-&gt;rmq)

Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4425    Accepted Submission(s): 1263 Problem Description After World War X, a lot of cities have been seriously damag

HDU——2874 Connections between cities

Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11927    Accepted Submission(s): 2775 Problem Description After World War X, a lot of cities have been seriously dama

hdoj2874 -- Connections between cities(LCA--tarjan离线)

Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8041    Accepted Submission(s): 1994 Problem Description After World War X, a lot of cities have been seriously damage

SPOJ 10628 Count on a tree(Tarjan离线LCA+主席树求树上第K小)

COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to N.Each node has an integer weight. We will ask you to perform the following operation: u v k : ask for the kth minimum weight on the path from node u 

HDU 2874 Connections between cities(LCA离线算法实现)

http://acm.hdu.edu.cn/showproblem.php?pid=2874 题意: 求两个城市之间的距离. 思路: LCA题,注意原图可能不连通. 如果不了解离线算法的话,可以看我之前博客写的解释http://www.cnblogs.com/zyb993963526/p/7295894.html 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cs

HDU 2874 Connections between cities (离线LCA)

题目地址:HDU 2874 好坑的一道题..MLE了好长时间....全用了前向星而且把G++改成了C++才过了.. LCA裸题,没什么好说的.. 代码如下; #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #in

HDU 2874 Connections between cities(LCA离线)

 题意:一个森林,询问两个节点距离,若无法到达,输出Not connected. 思路:还是求LCA的思想,只需再对每个询问的两个节点判断是否在一棵树内即可. 有一个问题是这道题的query很大,达到了1000000,所以离线算法空间上比较虚, 然而只会离线的.....于是把int改成short int险过.... #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #