当前位置:网站首页>POJ 3417 network (lca+ differential on tree)
POJ 3417 network (lca+ differential on tree)
2022-07-28 04:54:00 【gongyuandaye】
The question :n A spanning tree of points , also m Additional edges , Delete an edge of the spanning tree and an additional edge , Make it become two connected components , There are several options .
Answer key :lca+ The difference in the tree
For each additional edge <x, y>, take x To y Of lca Edge markers on the path , That is, delete any edge on the path , Then delete the additional edge to meet the requirements , But the premise of this method is that the edge on the path is covered only once . This is the first case ,ans++.
The second case is that the edge is not covered , Then delete the edge and it can be divided into two connected blocks , So you can delete any additional edge ,ans += m.
How to cover , Use the difference above the tree .
vector Drawing construction meeting t, Use chain forward star storage , The constant is smaller .
#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;
const int MAXN = 1e5 + 5;
int n, m;
vector<int> v[MAXN];
int fa[MAXN][31], dep[MAXN];
int d[MAXN];
int head[MAXN], k;
struct node {
int v, next;
}edge[MAXN << 1];
void add(int u, int v) {
edge[++k].next = head[u];
edge[k].v = v;
head[u] = k;
}
void init() {
for (int i = 1; i <= n; i++) v[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];
}
for (int i = head[root]; i; i = edge[i].next) {
int v = edge[i].v;
if (v == fno) continue;
dfs(v, root);
}
}
int lca(int x, int y) {
if (dep[x] > dep[y]) swap(x, y);
int tmp = dep[y] - dep[x];
for (int j = 0; tmp; ++j, tmp >>= 1)
if (tmp & 1) y = fa[y][j];
if (y == x) return x;
for (int j = 30; j >= 0 && y != x; --j) {
if (fa[x][j] != fa[y][j]) {
x = fa[x][j];
y = fa[y][j];
}
}
return fa[x][0];
}
void dfs1(int u, int pre) {
for (int i = head[u]; i; i = edge[i].next) {
int v = edge[i].v;
if (v == pre) continue;
dfs1(v, u);
d[u] += d[v];
}
}
int main() {
scanf("%d%d", &n, &m);
int x, y;
for (int i = 1; i < n; i++) {
scanf("%d%d", &x, &y);
add(x, y);
add(y, x);
}
dfs(1, 0);
for (int i = 1; i <= m; i++) {
scanf("%d%d", &x, &y);
++d[x];
++d[y];
d[lca(x, y)] -= 2;
}
dfs1(1, 0);
int ans = 0;
for (int i = 2; i <= n; i++) {
if (d[i] == 0) ans += m;
else if (d[i] == 1) ans++;
}
printf("%d\n", ans);
return 0;
}
边栏推荐
- MySQL partition table transformation
- Is low code the future of development? On low code platform
- 猿辅导技术进化论:助力教与学 构想未来学校
- Rendering process, how the code becomes a page (2)
- Gerrit operation - rollback a patch_ set
- [Sylar] framework -chapter12 bytearray module
- set与list性能对比
- Leetcode 15. sum of three numbers
- 阿里巴巴面试题【杭州多测师】【杭州多测师_王sir】
- 【CPU占用高】software_reporter_tool.exe
猜你喜欢

解析智能扫地机器人中蕴含的情感元素

CPU and memory usage are too high. How to modify RTSP round robin detection parameters to reduce server consumption?

全方位分析STEAM和创客教育的差异化

UI automation test farewell from now on, manual download browser driver, recommended collection

Leetcode 454. Adding four numbers II

The first artificial intelligence security competition starts. Three competition questions are waiting for you to fight

Array or object, date operation

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

动态sql和分页

MySQL数据库————初识数据库
随机推荐
could only be written to 0 of the 1 minReplication nodes. There are 0 datanode(s) running and 0 node
Automated test tool playwright (quick start)
[Sylar] framework chapter -chapter10-address module
Redis类型
(clone virtual machine steps)
Histogram of pyplot module of Matplotlib (hist(): basic parameter, return value)
Internet of things industrial serial port to WiFi module wireless routing WiFi module selection
Gerrit operation - rollback a patch_ set
What is the reason why the easycvr national standard protocol access equipment is online but the channel is not online?
塑料可以执行GB/T 2408 -燃烧性能的测定吗
[daily question 1] 735. Planetary collision
What is the core value of testing?
What should testers know about login security?
Performance comparison between set and list
Is low code the future of development? On low code platform
Cmake usage base summary
After easycvr is connected to the national standard equipment, how to solve the problem that the equipment video cannot be played completely?
MySQL数据库————初识数据库
Redux basic syntax
Introduction to this pointer