hdu2876 Connections between cities(LCA倍增)
图不一定联通,所以用并查集找各个联通块的祖先分别建图,之后就和LCA的步骤差不多了
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
inline int read(){
int sum=0,x=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')
x=0;
ch=getchar();
}
while(ch>='0'&&ch<='9')
sum=(sum<<1)+(sum<<3)+(ch^48),ch=getchar();
return x?sum:-sum;;
}
inline void write(int x){
if(x<0)
putchar('-'),x=-x;
if(x>9)
write(x/10);
putchar(x%10+'0');
}
const int M=1e4+4;
const int maxlog=14;
struct node{
int v,nextt,w;
}e[M<<2];
int head[M],deep[M],grand[M][maxlog],dis[M][maxlog],f[M],s,n,tot,root,sign;
void addedge(int u,int v,int w){
e[tot].v=v;
e[tot].w=w;
e[tot].nextt=head[u];
head[u]=tot++;
}
int find(int x){
return x==f[x]?x:f[x]=find(f[x]);
}
void dfs(int x){
for(int i=1;i<=s;i++){
grand[x][i]=grand[grand[x][i-1]][i-1];
dis[x][i]=dis[x][i-1]+dis[grand[x][i-1]][i-1];
if(!grand[x][i])
break;
}
for(int i=head[x];~i;i=e[i].nextt){
int v=e[i].v;
// cout<<"exit"<<endl;
if(v!=grand[x][0]){
grand[v][0]=x;
deep[v]=deep[x]+1;
dis[v][0]=e[i].w;
dfs(v);
}
}
}
void init(){
s=floor(log(n+0.0)/log(2.0));
deep[0]=-1;
dfs(root);
}
int LCA(int a,int b){
if(deep[a]>deep[b])
swap(a,b);
int ans=0;
for(int i=s;i>=0;i--)
if(deep[a]<deep[b]&&deep[a]<=deep[grand[b][i]])
ans+=dis[b][i],b=grand[b][i];
for(int i=s;i>=0;i--)
if(grand[a][i]!=grand[b][i])
ans+=dis[a][i],a=grand[a][i],ans+=dis[b][i],b=grand[b][i];
if(a!=b)
ans+=dis[a][0],ans+=dis[b][0];
return ans;
}
int main(){
int m,k;
while(~scanf("%d%d%d",&n,&m,&k)){
for(int i=0;i<=n;i++)
f[i]=i,head[i]=-1,deep[i]=0;
memset(grand,0,sizeof(grand));
memset(dis,0,sizeof(dis));
tot=0;
while(m--){
int x=read(),y=read(),w=read();
addedge(x,y,w);
addedge(y,x,w);
int p=find(x);
int q=find(y);
if(p!=q)
f[q]=p;
}
for(int i=1;i<=n;i++){
if(f[i]==i){
root=i;
init();
}
// cout<<"!!!"<<f[i]<<endl;
}
/* for(int i=1;i<=n;i++)
cout<<deep[i]<<" ";
cout<<endl;*/
while(k--){
int u=read(),v=read();
int x=find(u);
int y=find(v);
if(x==y){
write(LCA(u,v));
putchar('\n');
}
else
puts("Not connected");
}
}
return 0;
}
hdu2876 Connections between cities(LCA倍增)的更多相关文章
- hdu 2874 Connections between cities [LCA] (lca->rmq)
Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- HDU 2874 Connections between cities(LCA Tarjan)
Connections between cities [题目链接]Connections between cities [题目类型]LCA Tarjan &题意: 输入一个森林,总节点不超过N ...
- hdu-2874 Connections between cities(lca+tarjan+并查集)
题目链接: Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/327 ...
- HDU 2874 Connections between cities (LCA)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874 题意是给你n个点,m条边(无向),q个询问.接下来m行,每行两个点一个边权,而且这个图不能有环路 ...
- Connections between cities LCA
Problem Description After World War X, a lot of cities have been seriously damaged, and we need to r ...
- [hdu2874]Connections between cities(LCA+并查集)
题意:n棵树,求任意两点的最短距离. 解题关键:并查集判断两点是否位于一棵树上,然后求最短距离即可.此题可以直接对全部区间直接进行st表,因为first数组会将连接的两点的区间表示出来. //#pra ...
- HDU 2874 Connections between cities(LCA)
题目链接 Connections between cities LCA的模板题啦. #include <bits/stdc++.h> using namespace std; #defin ...
- hdu 2874 Connections between cities 带权lca判是否联通
Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- hdu 2874 Connections between cities(st&rmq LCA)
Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
随机推荐
- POJ 1006:Biorhythms 中国剩余定理
Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 121194 Accepted: 38157 Des ...
- C++ CreateInstance("ADODB.Connection");创建接口失败的解决方法
数据库对象mssql2005sp3专业版: 一般数据引用该路径文件#import "c:\\program files\\common files\\system\\ado\\msado15 ...
- caffe中运行mnist
mnist样本字库嘚图片转换:# coding=utf-8import numpy as npimport struct import matplotlib.pyplot as plt from PI ...
- git登录账号密码错误remote: Incorrect username or password (access token)
git提交时弹框让输入用户和密码,不小心输入错误了 再次提交 一直就提示 remote: Incorrect username or password 错误了,也不弹框要重新输入 解决方法 win1 ...
- 数据可视化BI平台——CBoard的部署与使用(笔记整理)
CBoard作为国内自主开发的数据可视化平台,因其方便好用而受到广大用户的使用和好评.现今CBoard有社区版和企业版两个版本,本文所述为社区版的0.4.2版本.注意:所需的一切资源以及相关参考链接都 ...
- OpenCV2基础操作----直线、矩形、圆、椭圆函数的使用
opencv2几个画图函数的调用 要用到几个随机变量: int fr = rand()%frame.rows; int fc = rand()%frame.cols; int b = rand()%2 ...
- 毕设问题 ---链接Dreamweaver和eclipse
在eclipse里面新建站点 https://blog.csdn.net/Slash_youth 我是一个搬运工 哈哈哈
- 【每日Scrum】第六天冲刺
一.计划会议内容 数据库仍然有问题,决定先绕过数据库,进行软件内容设计与界面ui美化. 二.任务看板 三.scrum讨论照片 四.产品的状态 无 五.任务燃尽图
- iOS部分页面横屏显示
在iOS系统支持横屏顺序默认读取plist里面设置的方向(优先级最高)等同于Xcode Geneal设置里面勾选application window设置的级别次之 然后是UINavigationcon ...
- ArrayList扩容原理分析
1:代码解读和分析 1.1:构造方法分析 1: public ArrayList(int initialCapacity) { ) { this.elementData = new Object[in ...