当前位置:网站首页>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;
}
边栏推荐
- Unity3d, Alibaba cloud server, platform configuration
- Excel导入,导出功能实现
- [algorithme] swordfinger offer2 golang question d'entrevue 2: addition binaire
- rtklib单点定位spp使用抗差估计遇到的问题及解决
- FairyGUI增益BUFF数值改变的显示
- 异常:IOException:Stream Closed
- Teach you to release a DeNO module hand in hand
- [algorithm] sword finger offer2 golang interview question 9: subarray with product less than k
- Unity3D,阿里云服务器,平台配置
- [Chongqing Guangdong education] reference materials for regional analysis and planning of Pingdingshan University
猜你喜欢
Force buckle 1189 Maximum number of "balloons"
[算法] 剑指offer2 golang 面试题10:和为k的子数组
Lock wait timeout exceeded try restarting transaction
RTKLIB: demo5 b34f. 1 vs b33
Particle system for introduction to unity3d Foundation (attribute introduction + case production of flame particle system)
【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
FairyGUI条子家族(滚动条,滑动条,进度条)
C programming exercise
Derivation of logistic regression theory
FairyGUI摇杆
随机推荐
[GNSS] robust estimation (robust estimation) principle and program implementation
Derivation of logistic regression theory
RTKLIB: demo5 b34f. 1 vs b33
Code example of MATLAB reading GNSS observation value o file
What are the functions and features of helm or terrain
[算法] 劍指offer2 golang 面試題2:二進制加法
Unity3D基础入门之粒子系统(属性介绍+火焰粒子系统案例制作)
VLSM variable length subnet mask partition tips
MySQL error warning: a long semaphore wait
C programming exercise
[算法] 剑指offer2 golang 面试题4:只出现一次的数字
服务未正常关闭导致端口被占用
On March 15, the official version of go 1.18 was released to learn about the latest features and usage
[algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal
[algorithm] sword finger offer2 golang interview question 4: numbers that appear only once
错误:排序与角标越界
Compile GDAL source code with nmake (win10, vs2022)
Matlab读取GNSS 观测值o文件代码示例
Guided package method in idea
Fairygui character status Popup