【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<<1];
int head[N],cnt,f[N],dis[N],deep[N],tr[N],t;
int fa(int i,int j){
	while(i!=j)
		if(deep[i]>deep[j])
			i=f[i];
		else
			j=f[j];
	return i;
}
void dfs(int x,int p){
	tr[x]=t;
	deep[x]=deep[p]+1;
	f[x]=p;
	for(int i=head[x];i;i=e[i].next){
		int v=e[i].to;
		if(v==p)continue;
		dis[v]=dis[x]+e[i].w;
		dfs(v,x);
	}
}
int main(){
	int n,m,c,u,v,w;
	while(~scanf("%d%d%d",&n,&m,&c)){
		memset(head,0,sizeof head);
		memset(f,0,sizeof f);
		cnt=t=0;
		for(int i=1;i<=m;i++){
			scanf("%d%d%d",&u,&v,&w);
			add(u,v,w);
			add(v,u,w);
		}
		for(int i=1;i<=n;i++)
			if(f[i]==0){
				t++;
				dfs(i,0);
			}
		for(int i=1;i<=c;i++){
			scanf("%d%d",&u,&v);
			if(tr[u]!=tr[v])
				puts("Not connected");
			else
				printf("%d\n",dis[u]+dis[v]-dis[fa(u,v)]*2);
		}
	}
}

  

时间: 2024-10-12 07:39:26

【HDU 2874】Connections between cities(LCA)的相关文章

【CodeForces 697C】Lorenzo Von Matterhorn(LCA)

Least Common Ancestors 节点范围是1~1e18,至多1000次询问. 只要不断让深的节点退一层(>>1)就能到达LCA. 用点来存边权,用map储存节点和父亲连边的权值. #include<cstdio> #include<map> #define ll long long using namespace std; map<ll,ll>m; ll u,v,w; void add(){ while(u!=v){ if(u<v){ m

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 5402】Travelling Salesman Problem(构造)

被某题卡SB了,结果这题也没读好...以为每个格子可以有负数就当搜索做了,怎么想也搜不过去,后来发现每个格子是非负数,那么肯定就是构造题. 题解如下: 首先如果nn为奇数或者mm为奇数,那么显然可以遍历整个棋盘. 如果n,mn,m都为偶数,那么讲棋盘黑白染色,假设(1,1)(1,1)和(n,m)(n,m)都为黑色,那么这条路径中黑格个数比白格个数多11,而棋盘中黑白格子个数相同,所以必然有一个白格不会被经过,所以选择白格中权值最小的不经过. 构造方法是这样,首先RRRRDLLLLD这样的路径走到

Connections between cities(LCA)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874 题目: Problem Description 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

【HDU 4940】Destroy Transportation system(数据水/无源无汇带上下界可行流)

Description Tom is a commander, his task is destroying his enemy’s transportation system. Let’s represent his enemy’s transportation system as a simple directed graph G with n nodes and m edges. Each node is a city and each directed edge is a directe

【HDU 5647】DZY Loves Connecting(树DP)

pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 332    Accepted Submission(s): 112 Problem Description DZY has an unroote

【HDU 5828】Rikka with Sequence(线段树)

[HDU 5828]Rikka with Sequence(线段树) Rikka with Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2311    Accepted Submission(s): 391 Problem Description As we know, Rikka is poor at math.

【HDU 4352】 XHXJ&#39;s LIS (数位DP+状态压缩+LIS)

XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2422    Accepted Submission(s): 990 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then careful

【HDU 5145】 NPY and girls(组合+莫队)

pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 593    Accepted Submission(s): 179 Problem Description NPY's girlfriend blew him out!H