当前位置:网站首页>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;
}
边栏推荐
- could only be written to 0 of the 1 minReplication nodes. There are 0 datanode(s) running and 0 node
- Printf() print char* str
- MySQL partition table transformation
- Analyze the emotional elements contained in intelligent sweeping robot
- How to quickly turn function test to automatic test
- Summary and review of puppeter
- Can plastics comply with gb/t 2408 - Determination of flammability
- [Sylar] framework -chapter20- daemon module
- Interview fraud: there are companies that make money from interviews
- [daily one] visual studio2015 installation in ancient times
猜你喜欢

RT_ Use of thread message queue
![[每日一氵]上古年代的 Visual Studio2015 安装](/img/b1/066ed0b9e93b8f378c89ee974163e5.png)
[每日一氵]上古年代的 Visual Studio2015 安装

Wang Shuang assembly language detailed learning notes 3: registers (memory access)

你必需要了解的saas架构设计?

Interview fraud: there are companies that make money from interviews

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

The first artificial intelligence security competition starts. Three competition questions are waiting for you to fight
![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]

App test process and test points

RT based_ Distributed wireless temperature monitoring system based on thread
随机推荐
Redis configuration file explanation / parameter explanation and elimination strategy
[Sylar] practical part - redis based parameter query service
网络安全基本知识——密码(一)
Gerrit operation - rollback a patch_ set
[Sylar] framework -chapter7-io coordination scheduling module
[idea] check out master invalid path problem
王爽汇编语言详细学习笔记三:寄存器(内存访问)
Rendering process, how the code becomes a page (2)
Machine learning and deep learning -- normalization processing
Leetcode 454. Adding four numbers II
[Sylar] framework Chapter 22 auxiliary module
Automated test tool playwright (quick start)
Depth traversal and breadth traversal of tree structure in JS
RT_ Use of thread message queue
机器人教育在STEM课程中的设计研究
linux下安装mysql
App test process and test points
If mongoose exists, it will be updated; if it does not exist, it will be added
Testcafe's positioning, operation of page elements, and verification of execution results
提升学生群体中的STEAM教育核心素养