当前位置:网站首页>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;
}
边栏推荐
- Alibaba interview question [Hangzhou multi tester] [Hangzhou multi tester _ Wang Sir]
- printf()打印char* str
- 猿辅导技术进化论:助力教与学 构想未来学校
- [Sylar] framework chapter -chapter21- environment variable module
- String 0123456789abcdef, what is the number of substrings (not empty and not the same string itself) [Hangzhou multi tester] [Hangzhou multi tester _ Wang Sir]
- Take out system file upload
- The difference between alter and confirm, prompt
- FreeRTOS learning (I)
- [Sylar] framework chapter -chapter10-address module
- Angr(十一)——官方文档(Part2)
猜你喜欢

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

C语言ATM自动取款机系统项目的设计与开发

Use and expansion of fault tolerance and fusing

Wang Shuang assembly language detailed learning notes 3: registers (memory access)
![[Oracle] 083 wrong question set](/img/10/9a5dae9542a8fed0356843c59f3c2f.png)
[Oracle] 083 wrong question set

Odoo action analysis (action.client, action.act_window, action.server)

After easycvr is connected to the national standard equipment, how to solve the problem that the equipment video cannot be played completely?

Know etcd

Comprehensively analyze the differences between steam and maker Education

Domain name (subdomain name) collection method of Web penetration
随机推荐
(克隆虚拟机步骤)
[Sylar] framework -chapter12 bytearray module
Tiantian AMADA CNC bending machine touch screen maintenance rgm21003 host circuit board maintenance
【CPU占用高】software_reporter_tool.exe
How to quickly turn function test to automatic test
FPGA:使用PWM波控制LED亮度
(clone virtual machine steps)
[Sylar] framework Chapter 6 collaborative scheduling module
Redis configuration file explanation / parameter explanation and elimination strategy
[Hongke technology] Application of network Multimeter in data center
Test report don't step on the pit
Warning: file already exists but should not: c:\users\workmai\appdata\local\temp appears when Python packages exe\_ MEI13
【sylar】框架篇-Chapter7-IO 协程调度模块
printf()打印char* str
驾驭EVM和XCM的强大功能,SubWallet如何赋能波卡和Moonbeam
Automated test tool playwright (quick start)
Observable time series data downsampling practice in Prometheus
(3.1) [Trojan horse synthesis technology]
If mongoose exists, it will be updated; if it does not exist, it will be added
100 lectures on Excel practical application cases (XI) - tips for inserting pictures in Excel