当前位置:网站首页>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;
}
边栏推荐
- 音乐播放(Toggle && PlayerPrefs)
- Mysql database reports an error: row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT=DY
- [algorithm] sword finger offer2 golang interview question 1: integer division
- Fabrication of fairygui simple Backpack
- Containers and Devops: container based Devops delivery pipeline
- [算法] 劍指offer2 golang 面試題2:二進制加法
- 基本Dos命令
- Matlab读取GNSS 观测值o文件代码示例
- Acwing-116 pilot brother
- PRIDE-PPPAR源码解析
猜你喜欢
FairyGUI人物状态弹窗
The port is occupied because the service is not shut down normally
[algorithme] swordfinger offer2 golang question d'entrevue 2: addition binaire
Mixed use of fairygui button dynamics
[algorithm] sword finger offer2 golang interview question 9: subarray with product less than k
Combination of fairygui check box and progress bar
Detailed explanation of balanced binary tree is easy to understand
[algorithm] sword finger offer2 golang interview question 10: subarray with sum K
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
[算法] 剑指offer2 golang 面试题1:整数除法
随机推荐
最短Hamilton路径 (状压DP)
Office prompts that your license is not genuine pop-up box solution
Basic DOS commands
Solution to the problem of automatic login in Yanshan University Campus Network
Usage differences between isempty and isblank
FairyGUI摇杆
(课设第一套)1-4 消息传递接口 (100 分)(模拟:线程)
Liste des boucles de l'interface graphique de défaillance
FairyGUI按钮动效的混用
Unity3d camera, the keyboard controls the front and rear left and right up and down movement, and the mouse controls the rotation, zoom in and out
GNSS定位精度指标计算
[Chongqing Guangdong education] Shandong University College Physics reference materials
Halcon knowledge: gray_ Tophat transform and bottom cap transform
PRIDE-PPPAR源码解析
2022国赛Re1 baby_tree
服务未正常关闭导致端口被占用
FairyGUI复选框与进度条的组合使用
[算法] 剑指offer2 golang 面试题5:单词长度的最大乘积
[算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等
Implementation of Excel import and export functions