当前位置:网站首页>Dark chain lock (lca+ difference on tree)
Dark chain lock (lca+ difference on tree)
2022-07-06 12:56:00 【Classmate Chen_】
subject
The legendary dark chain is called D a r k Dark Dark.
D a r k Dark Dark It is the product of the darkness of the human heart , Brave men at all times and in all countries tried to bring it down .
After study , Have you found D a r k Dark Dark Present the structure of an undirected graph , The picture shows N N N Two nodes and two kinds of edges , One kind of edge is called main edge , The other kind is called additional edge .
D a r k Dark Dark Yes N – 1 N–1 N–1 The first major edge , also D a r k Dark Dark There is a path composed of only the main edges between any two nodes of .
in addition , D a r k Dark Dark also M M M Additional edges .
Your task is to bring D a r k Dark Dark Cut into two disconnected parts .
In limine D a r k Dark Dark The additional edges of the are invincible , You can only choose one main edge to cut .
Once you cut off a major side , D a r k Dark Dark Will enter defense mode , The main edge will become invincible and the additional edge can be cut off .
But your ability can only be cut off again D a r k Dark Dark An additional edge of .
Now you want to know , How many options can defeat D a r k Dark Dark.
Be careful , Even if you cut off the main side in the first step, you have already D a r k Dark Dark Cut into two pieces , You also need to cut off an additional edge to defeat D a r k Dark Dark.
Input format
The first line contains two integers N N N and M M M.
after N – 1 N–1 N–1 That's ok , Each line contains two integers A A A and B B B, Express A A A and B B B There is a main side between .
after M M M Lines give additional edges in the same format .
Output format
Output an integer for the answer .
Data range
N ≤ 100000 N≤100000 N≤100000, M ≤ 200000 M≤200000 M≤200000, The data guarantee that the answer is no more than 2 31 − 1 2^{31}−1 231−1
sample input :
4 1
1 2
2 3
1 4
3 4
sample output :
3
LCA+ The difference in the tree
The topic is that let's delete a main edge and an additional edge to change the graph into two parts
1. from On to Out Of chart Yes n individual spot 、 n − 1 strip Lord want edge 、 m strip attach Add edge and And ren It means two spot And between all from Lord want edge phase even , say bright The chart yes from Lord want edge structure become Of One Tree Trees 1. Because the figure given has n A little bit 、n-1 The first major edge 、m There are additional edges and any two points are connected by the main edge , It shows that the graph is a tree composed of main edges 1. from On to Out Of chart Yes n individual spot 、n−1 strip Lord want edge 、m strip attach Add edge and And ren It means two spot And between all from Lord want edge phase even , say bright The chart yes from Lord want edge structure become Of One Tree Trees
2. x To y Of road path in Of attach Add edge < = 1 only can With hold chart change become two Ministry branch ( by What Well ? : because by Lord want edge has the structure become 了 One star Trees , Such as fruit some two spot road path in save stay One strip With On Of attach Add edge , be say bright Delete fall One strip attach Add edge , Its He The earth Fang also yes even through Of , Just No can hold chart change by two Ministry branch ) 2.x To y Additional edges in the path of <=1 Before you can turn the picture into two parts ( Why? ?: Because the main side has formed a tree , If there is more than one additional edge in a two-point path , It means that an additional edge is deleted , Other places are still connected , You can't change the picture into two parts ) 2.x To y Of road path in Of attach Add edge <=1 only can With hold chart change become two Ministry branch ( by What Well ?: because by Lord want edge has the structure become 了 One star Trees , Such as fruit some two spot road path in save stay One strip With On Of attach Add edge , be say bright Delete fall One strip attach Add edge , Its He The earth Fang also yes even through Of , Just No can hold chart change by two Ministry branch )
3. Need to be want system meter The road path in Yes many Less strip attach Add edge . The Such as What Come on It's about The reason is road path in attach Add edge Of individual Count Well ? — > Trees On Bad branch 3. You need to count how many additional edges there are in this path . How to deal with the number of additional edges in the path ?—> The difference in the tree 3. Need to be want system meter The road path in Yes many Less strip attach Add edge . The Such as What Come on It's about The reason is road path in attach Add edge Of individual Count Well ?—> Trees On Bad branch
4. Trees On Bad branch : Such as fruit Want to Give Way x and y And between Of road path On Add On One individual Count c and And No shadow ring Its He road path Of junction fruit : x + = c , y + = c , l c a ( x , y ) − = 2 ∗ c . 【 l c a ( x , y ) yes x and y Of most near Male common Ancestor First 】 4. The difference in the tree : If you want to let x and y Add a number to the path between c And does not affect the results of other paths :x+=c,y+=c,lca(x,y)-=2*c.【lca(x,y) yes x and y The latest public ancestor of 】 4. Trees On Bad branch : Such as fruit Want to Give Way x and y And between Of road path On Add On One individual Count c and And No shadow ring Its He road path Of junction fruit :x+=c,y+=c,lca(x,y)−=2∗c.【lca(x,y) yes x and y Of most near Male common Ancestor First 】
5. 3、 ... and Kind of love condition : ( road path in power value use d Come on surface in 、 a n s generation surface answer case ) 5. Three situations :( The weight in the path is d To express 、ans For the answer ) 5. 3、 ... and Kind of love condition :( road path in power value use d Come on surface in 、ans generation surface answer case )
(1)d=0:
After cutting off the main side , No matter which additional edge is cut , The picture is still in two parts , So there is m Medium scheme —>ans+=m;
(2)d=1:
After cutting off the main side , The additional edge must be cut off to make the graph become two parts , So there is only one solution —>ans++
(3)d>=2:
After cutting off the main side , No matter which additional edge is cut, the graph cannot be changed into two parts , So this plan is not feasible —>ans+=0
AC Code (C++)
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
const int N=100010,M=N<<1;
int depth[N]; // The depth of each point
bool st[N];
int n,m;
int f[N][20]; //f[i][j]:i A little jump j Points reached
int h[N],e[M],ne[M],idx;
int s[N]; // The prefix and
int ans;
void add(int a,int b){
e[idx]=b;
ne[idx]=h[a];
h[a]=idx++;
}
// Preprocess the depth of each point and jump of each point k The next point you can jump to
void bfs(){
queue<int> q;
memset(depth,0x3f,sizeof depth);
depth[0]=0,depth[1]=1;
q.push(1);
st[1]=true;
while(q.size()){
int t=q.front();
q.pop();
st[t]=false;
for(int i=h[t];i!=-1;i=ne[i]){
int j=e[i];
if(depth[j]>depth[t]+1){
depth[j]=depth[t]+1;
f[j][0]=t;
for(int k=1;k<=16;k++){
f[j][k]=f[f[j][k-1]][k-1];
}
if(!st[j]){
st[j]=true;
q.push(j);
}
}
}
}
}
// Recent public ancestor
int lca(int a,int b){
if(depth[a]<depth[b]) swap(a,b);
// Different depths
for(int k=16;k>=0;k--){
if(depth[f[a][k]]>=depth[b]){
a=f[a][k];
}
}
if(a==b) return a;
// If it doesn't return , That explains. a and b Already on the same floor
for(int k=16;k>=0;k--){
if(f[a][k]!=f[b][k]){
a=f[a][k];
b=f[b][k];
}
}
return f[a][0]; // here a and b Not to the nearest public ancestor , Need to jump once
}
int dfs(int u,int fa){
int res=s[u];
for(int i=h[u];i!=-1;i=ne[i]){
int j=e[i];
if(j==fa) continue;
int t=dfs(j,u);
if(t==0) ans+=m;
else if(t==1) ans++;
res+=t;
}
return res;
}
int main(){
scanf("%d%d",&n,&m);
memset(h,-1,sizeof h);
for(int i=1;i<n;i++){
int a,b;
scanf("%d%d",&a,&b);
add(a,b);
add(b,a);
}
bfs();
for(int i=1;i<=m;i++){
int a,b;
scanf("%d%d",&a,&b);
int t=lca(a,b);
s[a]++,s[b]++,s[t]-=2;
}
dfs(1,-1);
printf("%d",ans);
return 0;
}
边栏推荐
- 染色法判定二分图
- WSL common commands
- Office提示您的许可证不是正版弹框解决
- [GNSS data processing] Helmert variance component estimation analysis and code implementation
- Introduction to the daily practice column of the Blue Bridge Cup
- 341. Flatten nested list iterator
- Implementation of Excel import and export functions
- Novatel board oem617d configuration step record
- Fabrication of fairygui simple Backpack
- Agile development helps me
猜你喜欢
Unity3D,阿里云服务器,平台配置
[算法] 剑指offer2 golang 面试题4:只出现一次的数字
Easy to use shortcut keys in idea
微信小程序开发心得
MySQL shutdown is slow
Particle system for introduction to unity3d Foundation (attribute introduction + case production of flame particle system)
FairyGUI人物状态弹窗
Theoretical derivation of support vector machine
堆排序【手写小根堆】
染色法判定二分图
随机推荐
PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
FGUI工程打包发布&导入Unity&将UI显示出来的方式
The master of double non planning left the real estate company and became a programmer with an annual salary of 25W. There are too many life choices at the age of 25
C code implementation of robust estimation in rtklib's pntpos function (standard single point positioning spp)
On March 15, the official version of go 1.18 was released to learn about the latest features and usage
Derivation of logistic regression theory
Database table splitting strategy
Theoretical derivation of support vector machine
MySQL performance tuning - dirty page refresh
How to improve the deletion speed of sequential class containers?
服务未正常关闭导致端口被占用
[algorithm] sword finger offer2 golang interview question 13: sum of numbers of two-dimensional submatrix
Basic DOS commands
[algorithm] sword finger offer2 golang interview question 2: binary addition
Novatel board oem617d configuration step record
Detailed explanation of balanced binary tree is easy to understand
记录:newInstance()过时的代替方法
Unity3D基础入门之粒子系统(属性介绍+火焰粒子系统案例制作)
Mysql database reports an error: row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT=DY
FairyGUI简单背包的制作