HDU 2874 —— Connections between cities(并查集+LCA)
来源:互联网 发布:c语言简单的图形编程 编辑:程序博客网 时间:2024/06/05 14:38
题目:Connections between cities
题意:N个点的无向图,由于不存在环,所以形成的是森林。给出边的权值信息,询问u, v两个点之间是否连通,以及最短路径。
对于连通的问题,用并查集搞定。
而路径,由于两个点在同一棵树,可以先dfs预处理每个节点i到根节点的距离dist[i],无根树就随便找个结点当根啦。然后对于查询的u, v,找到它们的最近公共祖先p,答案就是dist[u]+dist[v]-2*dist[p]。
#include<cstdio>#include<cstring>#include<vector>#include<algorithm>using namespace std;#define pb push_backconst int N = 10010;const int LOG_SIZE = 16;inline void in(int &x){ x=0; char c=getchar(); while(c<48 || c>57) c=getchar(); while(c>=48 && c<=57){ x = x*10+c-48; c = getchar(); }}vector<int> V[N], G[N];int n, m, q, fa[N];int find(int x){ int y = x; for(; x!=fa[x]; x=fa[x]); return fa[y]=x;}vector<int> P;int parent[LOG_SIZE][N], dist[N];int depth[N];void dfs(int x, int par){ P.pb(x); for(int i=0; i<V[x].size(); i++){ int j = V[x][i]; if(j==par) continue; int v = G[x][i]; depth[j] = depth[x]+1; parent[0][j] = x; dist[j] = dist[x]+v; dfs(j, x); }}void init_lca(int x){ P.clear(); depth[x]=0; dist[x]=0; dfs(x, -1); for(int k=0; k+1<LOG_SIZE; k++){for(int i=0; i<P.size(); i++){int v = P[i];if(parent[k][v]<0)parent[k+1][v] = -1;else{ parent[k+1][v] = parent[k][parent[k][v]];}}}}int lca(int u, int v){ if(depth[u] > depth[v]) swap(u, v);for(int k=0; k<LOG_SIZE; k++){if((depth[v]-depth[u]) >> k&1){v = parent[k][v];}}if(u==v)return u;for(int k=LOG_SIZE-1; k>=0; k--){if(parent[k][u] != parent[k][v]){u = parent[k][u];v = parent[k][v];}}return parent[0][u];}int query(int u, int v){ int p = lca(u,v); return dist[u]+dist[v]-2*dist[p];}int main(){ while(~scanf("%d %d %d", &n, &m, &q)){ for(int i=1; i<=n; i++){ V[i].clear(); G[i].clear(); fa[i] = i; } int a, b, c; while(m--){ in(a); in(b); in(c); V[a].pb(b); G[a].pb(c); V[b].pb(a); G[b].pb(c); a = find(a); b = find(b); if(a!=b) fa[a]=b; } for(int i=1; i<=n; i++){ if(fa[i]==i){ init_lca(i); } } while(q--){ in(a); in(b); if(find(a) != find(b)) puts("Not connected"); else printf("%d\n", query(a, b)); } } return 0;}
0 0
- HDU 2874 —— Connections between cities(并查集+LCA)
- hdu 2874 Connections between cities LCA || dfs+并查集
- |hdu 2874|LCA|并查集|Connections between cities
- 文章标题 HDU 2874 : Connections between cities(LCA+并查集)
- HDU2874——Connections between cities 详解 (LCA,RMQ,数据结构,dfs序,并查集)
- hdu2874 Connections between cities(LCA离线+并查集)
- hdu 2874 Connections between cities (并查集+LCA)hash优化
- Connections between cities (hdu 2874 LCA)
- hdu 2874 Connections between cities(LCA)
- HDU 2874Connections between cities(LCA)
- HDOJ 题目2874 Connections between cities(LCA转RMQ+并查集)
- HDU2874 Connections between cities LCA+DFS或并查集
- hdu2874—Connections between cities(LCA)
- hdu 2874 Connections between cities LCA
- HDU 2874 Connections between cities [LCA]
- hdu 2874 Connections between cities (LCA)
- hdu 2874 Connections between cities(LCA)
- 【HDU】2874 Connections between cities 离线LCA
- za
- OFBIZ 电子商城商品如何 进行多币种结算
- 关于nextLine()和next()的使用
- 制作Android 升级包
- C语言经典例题及答案1
- HDU 2874 —— Connections between cities(并查集+LCA)
- 线性规划与网络流24题の24 骑士共存问题 (二分图最大独立集)
- hdu 3579 (同余方程组,逐一合并大法)
- C语言经典例题及答案2
- Caught exception while loading file struts-default.xml 错误
- The password specified was incorrect. Please enter the correct password for the postgres windows use
- 转 OFBIZ 网站或店铺视觉主题(visual Theme)设计
- iOS APP 图标Icon和启动图片的大小、命名
- 转:全新安装Mac OSX 开发者环境 同时使用homebrew搭建 (LNMP开发环境)