当前位置:网站首页>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;
}
边栏推荐
- [算法] 剑指offer2 golang 面试题1:整数除法
- 【rtklib】在rtk下使用抗差自适应卡尔曼滤波初步实践
- 地球围绕太阳转
- Liste des boucles de l'interface graphique de défaillance
- [algorithm] sword finger offer2 golang interview question 9: subarray with product less than k
- How to improve the deletion speed of sequential class containers?
- 1041 Be Unique (20 point(s))(哈希:找第一个出现一次的数)
- [algorithm] sword finger offer2 golang interview question 10: subarray with sum K
- Mysql database reports an error: row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT=DY
- FairyGUI按钮动效的混用
猜你喜欢
![[GNSS data processing] Helmert variance component estimation analysis and code implementation](/img/4e/ff0334cf9a2a37096778a8c5719a4e.jpg)
[GNSS data processing] Helmert variance component estimation analysis and code implementation

FGUI工程打包发布&导入Unity&将UI显示出来的方式

rtklib单点定位spp使用抗差估计遇到的问题及解决

Code example of MATLAB reading GNSS observation value o file

Combination of fairygui check box and progress bar

Compilation principle: preprocessing of source program and design and implementation of lexical analysis program (including code)

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

Unity scene jump and exit

Unity3D,阿里云服务器,平台配置

NovAtel 板卡OEM617D配置步骤记录
随机推荐
C programming exercise
异常:IOException:Stream Closed
The port is occupied because the service is not shut down normally
基于rtklib源码进行片上移植的思路分享
记录:初次cmd启动MySQL拒接访问之解决
[算法] 剑指offer2 golang 面试题7:数组中和为0的3个数字
VLSM variable length subnet mask partition tips
【RTKLIB 2.4.3 b34 】版本更新简介一
The service robots that have been hyped by capital and the Winter Olympics are not just a flash in the pan
微信小程序开发心得
Wechat applet development experience
音乐播放(Toggle && PlayerPrefs)
错误:排序与角标越界
Teach you to release a DeNO module hand in hand
Lean product development - Lean Software Development & lean product development
FairyGUI循环列表
FairyGUI循環列錶
【rtklib】在rtk下使用抗差自适应卡尔曼滤波初步实践
FairyGUI条子家族(滚动条,滑动条,进度条)
(课设第一套)1-5 317号子任务 (100 分)(Dijkstra:重边自环)