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>
#include <set>
#include <stdio.h>
using namespace std;
#define LL __int64
#define pi acos(-1.0)
const int mod=1e9+7;
const int INF=0x3f3f3f3f;
const double eqs=1e-9;
const int MAXN=200000+10;
int head[10001], head1[10001], cnt, cnt1, lca[1000001], bin[10001], d[10001], ffa[10001], n;
struct node
{
        int v, w, next;
}edge[20001];
struct N
{
        int v, id, next;
}edge1[2000001];
void add(int u, int v, int w)
{
        edge[cnt].v=v;
        edge[cnt].w=w;
        edge[cnt].next=head[u];
        head[u]=cnt++;
}
void add1(int u, int v, int id)
{
        edge1[cnt1].v=v;
        edge1[cnt1].id=id;
        edge1[cnt1].next=head1[u];
        head1[u]=cnt1++;
}
bool vis[10001];
int find1(int x)
{
        return bin[x]==x?x:bin[x]=find1(bin[x]);
}
void tarjan(int u, int root)
{
        vis[u]=1;
        ffa[u]=root;
        int i;
        for(i=head1[u];i!=-1;i=edge1[i].next){
                int v=edge1[i].v;
                if(!vis[v]||ffa[u]!=ffa[v]) continue ;
                lca[edge1[i].id]=d[u]+d[v]-2*d[find1(bin[v])];
        }
        for(i=head[u];i!=-1;i=edge[i].next){
                int v=edge[i].v;
                if(vis[v]) continue ;
                d[v]=d[u]+edge[i].w;
                tarjan(v,root);
                bin[v]=u;
        }
}
void init()
{
        memset(head,-1,sizeof(head));
        memset(head1,-1,sizeof(head1));
        cnt=cnt1=0;
        memset(lca,-1,sizeof(lca));
        memset(vis,0,sizeof(vis));
        for(int i=1; i<=n; i++) {
                bin[i]=i;
        }
}
int main()
{
        int m, qu, i, j, u, v, w;
        while(scanf("%d%d%d",&n,&m,&qu)!=EOF) {
                init();
                while(m--) {
                        scanf("%d%d%d",&u,&v,&w);
                        add(u,v,w);
                        add(v,u,w);
                }
                for(i=0; i<qu; i++) {
                        scanf("%d%d",&u,&v);
                        add1(u,v,i);
                        add1(v,u,i);
                }
                for(i=1; i<=n; i++) {
                        if(!vis[i]) {
                                d[i]=0;
                                tarjan(i,i);
                        }
                }
                for(i=0; i<qu; i++) {
                        if(lca[i]==-1) puts("Not connected");
                        else printf("%d\n",lca[i]);
                }
        }
        return 0;
}
时间: 2024-11-07 23:13:00

HDU 2874 Connections between cities (离线LCA)的相关文章

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)

After World War X, a lot of cities have been seriously damaged, and we need to rebuild those cities. However, some materials needed can only be produced in certain places. So we need to transport these materials from city to city. For most of roads h

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 带权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

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

HDU 2874 Connections between cities(LCA离线)

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

HDU - 2874 Connections between cities(LCA)

题目大意:给出N个点,M条线,Q个询问,询问的是两点之间的最短距离 解题思路:恶心的数据量,一不小心就超空间了 这题给图不是张连通图,是森林,所以计算两点之间的最短距离时还要考虑一下是否在同一棵树中 剩下的就是裸LCA了 #include <cstdio> #include <cstring> #define N 10010 #define M 20010 #define C 2000010 struct Edge{ int to, next, dis; }E[M]; struct

hdu 2874 Connections between cities hdu 2586 How far away ? LCA

两道lca模板题,用的是倍增法,nlogn预处理,logn查询. #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; #define maxn 10100 struct Edge { int u,v,w,next; }e[100100]; int n,m,c; int head[maxn],cnt; int fa[ma

Hdu 2874 Connections between cities

题意: 城市 道路  没有环 不一定连通的树 求两城市的最短距离 设想一下就是很多小树  那好办 思路: lca离线算法 然后有个技巧就是 每次我们tarjan一棵树不是最后树的节点都访问过并且孩子全能找到根嘛 那么我们只要做做做做 做到全部的城市都访问过了  就行了 反正你做这颗小树的时候又不会影响到其他树的 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; i