当前位置:网站首页>HDU 2874 connections between cities
HDU 2874 connections between cities
2022-07-28 04:55:00 【gongyuandaye】
The question : Because most roads have been completely destroyed during the war , There may be no path between the two cities , No ring exists . Now? , After telling you the road conditions , We want to know whether there is a path between any two cities . If the answer is yes , Then output the shortest path between them .
Answer key : Union checking set +lca Multiplication method
First use and check the set shrink point , There must be no path if it is not in the same set , then dfs Preprocess each set .
Here we use the multiplication method to find lca.
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<fstream>
#include<set>
#include<map>
#include<sstream>
#include<iomanip>
#define ll long long
#define pii pair<int, int>
using namespace std;
int n, m, q, x, y, k;
const int MAXN = 1e4 + 5;
vector<int> v[MAXN], w[MAXN];
int fa[MAXN][31], cost[MAXN][31], dep[MAXN];
void init() {
for (int i = 1; i <= n; i++) v[i].clear(), w[i].clear();
// memset(fa, 0, sizeof(fa));
// memset(cost, 0, sizeof(cost));
// memset(dep, 0, sizeof(dep));
}
void dfs(int root, int fno) {
//1 0
fa[root][0] = fno;
dep[root] = dep[fa[root][0]] + 1;
for (int i = 1; i < 31; ++i) {
fa[root][i] = fa[fa[root][i - 1]][i - 1];
cost[root][i] = cost[fa[root][i - 1]][i - 1] + cost[root][i - 1];
}
int sz = v[root].size();
for (int i = 0; i < sz; ++i) {
if (v[root][i] == fno) continue;
cost[v[root][i]][0] = w[root][i];
dfs(v[root][i], root);
}
}
int lca(int x, int y) {
if (dep[x] > dep[y]) swap(x, y);
int tmp = dep[y] - dep[x], ans = 0;
for (int j = 0; tmp; ++j, tmp >>= 1)
if (tmp & 1) ans += cost[y][j], y = fa[y][j];
if (y == x) return ans;
for (int j = 30; j >= 0 && y != x; --j) {
if (fa[x][j] != fa[y][j]) {
ans += cost[x][j] + cost[y][j];
x = fa[x][j];
y = fa[y][j];
}
}
ans += cost[x][0] + cost[y][0];
return ans;
}
int pa[MAXN];
int Find(int u) {
return u == pa[u] ? u : pa[u] = Find(pa[u]);
}
bool vis[MAXN];
int main() {
while (~scanf("%d%d%d", &n, &m, &q)) {
init();
for (int i = 1; i <= n; i++) pa[i] = i, vis[i] = 0;
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &x, &y, &k);
int fx = Find(x);
int fy = Find(y);
if (fx != fy) pa[fx] = fy;
v[x].push_back(y);
v[y].push_back(x);
w[x].push_back(k);
w[y].push_back(k);
}
for (int i = 1; i <= n; i++) {
int f = Find(i);
if (vis[f]) continue;
dfs(f, 0);
vis[f] = 1;
}
while (q--) {
scanf("%d%d", &x, &y);
int fx = Find(x);
int fy = Find(y);
if (fx != fy) puts("Not connected");
else printf("%d\n", lca(x, y));
}
}
return 0;
}
边栏推荐
- Analysis of the reason why easycvr service can't be started and tips for dealing with easy disk space filling
- Leetcode 15. sum of three numbers
- 基于MPLS构建虚拟专网的配置实验
- 01 node express system framework construction (express generator)
- 数据安全逐步落地,必须紧盯泄露源头
- (manual) [sqli labs27, 27a] error echo, Boolean blind injection, filtered injection
- excel实战应用案例100讲(十一)-Excel插入图片小技巧
- Cmake usage base summary
- [idea] check out master invalid path problem
- How to quickly locate bugs? How to write test cases?
猜你喜欢
![(manual) [sqli labs27, 27a] error echo, Boolean blind injection, filtered injection](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
(manual) [sqli labs27, 27a] error echo, Boolean blind injection, filtered injection

Artificial intelligence and RPA technology application (I) -rpa Hongji product introduction, designer interface function explanation

FreeRTOS learning (I)

FreeRTOS startup process, coding style and debugging method

App test process and test points

提升学生群体中的STEAM教育核心素养

Automated test tool playwright (quick start)

数据安全逐步落地,必须紧盯泄露源头
![String 0123456789abcdef, what is the number of substrings (not empty and not the same string itself) [Hangzhou multi tester] [Hangzhou multi tester _ Wang Sir]](/img/78/efe3d70a4bfe8ac0c9b58b54d02b00.png)
String 0123456789abcdef, what is the number of substrings (not empty and not the same string itself) [Hangzhou multi tester] [Hangzhou multi tester _ Wang Sir]

What should testers know about login security?
随机推荐
How to quickly turn function test to automatic test
Redis配置文件详解/参数详解及淘汰策略
Basic knowledge of network security - password (I)
Dynamic SQL and paging
Visual studio 2019 new OpenGL project does not need to reconfigure the environment
[Sylar] framework -chapter9-hook module
RT_ Use of thread message queue
字符串0123456789abcdef,子串(非空且非同串本身)的个数是多少【杭州多测师】【杭州多测师_王sir】...
linux下安装mysql
(clone virtual machine steps)
[Sylar] framework Chapter 6 collaborative scheduling module
Attempt method in laravel user authentication
提升学生群体中的STEAM教育核心素养
MySQL(5)
Redis类型
你必需要了解的saas架构设计?
[daily question 1] 735. Planetary collision
Gerrit operation - rollback a patch_ set
Summary and review of puppeter
【CPU占用高】software_reporter_tool.exe