HDU 2874

简单的tarjan

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

#define N 10010
#define M 20010
#define C 2001000
using namespace std;

struct edge{
	int v,c;
	int next;
}City[M];

struct query{
	int v,index;
	int next;
}Query[C];
int ctot,qtot;
int chead[N],qhead[N];
int fa[N];
int Value[N];
int ans[C/2];
int rts[N];

void addcedge(int u,int v,int c){
	City[ctot].v=v;
	City[ctot].c=c;
	City[ctot].next=chead[u];
	chead[u]=ctot++;
}

void addqedge(int u,int v,int c){
	Query[qtot].v=v;
	Query[qtot].index=c;
	Query[qtot].next=qhead[u];
	qhead[u]=qtot++;
}

int find(int u){
	int p=u;
	while(p!=fa[p])
	p=fa[p];
	while(u!=p){
		int r=fa[u];
		fa[u]=p;
		u=r;
	}
	return p;
}

void Union(int u,int v){
	int uf=find(u);
	int uv=find(v);
	fa[v]=uf;
}

void tarjan(int now,int pre,int val,int root){
	rts[now]=root;
	fa[now]=now;
	Value[now]=val;
	for(int e=chead[now];e!=-1;e=City[e].next){
		int v=City[e].v;
		if(v==pre) continue;
		tarjan(v,now,val+City[e].c,root);
		Union(now,v);
	}
	for(int e=qhead[now];e!=-1;e=Query[e].next){
		int v=Query[e].v;
		if(ans[Query[e].index]==-1&&rts[v]==root){
			int vf=find(v);
			ans[Query[e].index]=Value[v]+Value[now]-2*Value[vf];
		}
	}
}

int main(){
	int n,m,c,g; int u,v,q;
	while(scanf("%d%d%d",&n,&m,&c)!=EOF){
		memset(chead,-1,sizeof(chead));
		memset(qhead,-1,sizeof(qhead));
		memset(fa,-1,sizeof(fa));
		memset(Value,0,sizeof(Value));
		memset(ans,-1,sizeof(ans));
		memset(rts,0,sizeof(rts));
		ctot=qtot=0;
		for(int i=1;i<=m;i++){
			scanf("%d%d%d",&u,&v,&g);
			addcedge(u,v,g);
			addcedge(v,u,g);
		}
		for(int i=1;i<=c;i++){
			scanf("%d%d",&u,&v);
			addqedge(u,v,i);
			addqedge(v,u,i);
		}
		for(int i=1;i<=n;i++){
			if(fa[i]==-1)
			tarjan(i,-1,0,i);
		}
		for(int i=1;i<=c;i++){
			if(ans[i]!=-1)
			printf("%d\n",ans[i]);
			else puts("Not connected");
		}
	}
	return 0;
}

  

时间: 2024-10-14 13:49:23

HDU 2874的相关文章

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 LCA离线算法

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

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离线算法实现)

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(LCA)

题目链接 Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5037    Accepted Submission(s): 1399 Problem Description After World War X, a lot of cities have been seriously d

【HDU 2874】Connections between cities(LCA)

dfs找出所有节点所在树及到树根的距离及深度及父亲. i和j在一棵树上,则最短路为dis[i]+dis[j]-dis[LCA(i,j)]*2. #include <cstring> #include <cstdio> #define N 10005 #define add(u,v,w) e[++cnt]=(edge){v,head[u],w};head[u]=cnt using namespace std; struct edge{ int to,next,w; }e[N<&

Hdu 2874 Connections between cities

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

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

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)

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