当前位置:网站首页>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;
}
边栏推荐
- FreeRTOS startup process, coding style and debugging method
- Alibaba interview question [Hangzhou multi tester] [Hangzhou multi tester _ Wang Sir]
- Leetcode 15. sum of three numbers
- RT_ Use of thread message queue
- Analysis of the reason why easycvr service can't be started and tips for dealing with easy disk space filling
- 01 node express system framework construction (express generator)
- Machine learning and deep learning -- normalization processing
- Look at the experience of n-year software testing summarized by people who came over the test
- Program life | how to switch to software testing? (software testing learning roadmap attached)
- Wang Shuang assembly language detailed learning notes 3: registers (memory access)
猜你喜欢

The go zero singleton service uses generics to simplify the registration of handler routes

App test process and test points

Method of converting UI file to py file

Design and development of C language ATM system project

(克隆虚拟机步骤)

Rendering process, how the code becomes a page (2)

How to quickly locate bugs? How to write test cases?

linux下安装mysql

Dynamic SQL and paging
![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]
随机推荐
Redis type
Use and expansion of fault tolerance and fusing
Introduction to testcafe
Look at the experience of n-year software testing summarized by people who came over the test
Basic knowledge of network security - password (I)
吉利AI面试题【杭州多测师】【杭州多测师_王sir】
Explain initialization list
Jupyter notebook installation code prompt function
[Hongke technology] Application of network Multimeter in data center
What SaaS architecture design do you need to know?
Program life | how to switch to software testing? (software testing learning roadmap attached)
linux下安装mysql
scipy.stats.chi2
[Sylar] framework -chapter15 stream module
Destructor of member function
set与list性能对比
[II. Mobile web page development] 2D & 3D conversion and animation, mobile terminal layout, responsive layout
[Oracle] 083 wrong question set
王爽汇编语言详细学习笔记三:寄存器(内存访问)
欧拉路/欧拉回路