当前位置:网站首页>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 面试题6:排序数组中的两个数字之和
- Fabrication d'un sac à dos simple fairygui
- (课设第一套)1-4 消息传递接口 (100 分)(模拟:线程)
- FairyGUI簡單背包的制作
- Idea problem record
- 【rtklib】在rtk下使用抗差自适应卡尔曼滤波初步实践
- [Chongqing Guangdong education] reference materials for regional analysis and planning of Pingdingshan University
- Pride-pppar source code analysis
- Unity场景跳转及退出
- [algorithm] sword finger offer2 golang interview question 4: numbers that appear only once
猜你喜欢
Problems and solutions of robust estimation in rtklib single point location spp
微信小程序开发心得
Basic DOS commands
服务未正常关闭导致端口被占用
[算法] 剑指offer2 golang 面试题5:单词长度的最大乘积
C code implementation of robust estimation in rtklib's pntpos function (standard single point positioning spp)
FairyGUI循環列錶
Mysql database index
Guided package method in idea
341. Flatten nested list iterator
随机推荐
1041 Be Unique (20 point(s))(哈希:找第一个出现一次的数)
Game 280 weekly
Introduction to the daily practice column of the Blue Bridge Cup
FairyGUI循環列錶
错误:排序与角标越界
Special palindromes of daily practice of Blue Bridge Cup
FairyGUI摇杆
地球围绕太阳转
[rtklib] preliminary practice of using robust adaptive Kalman filter under RTK
Basic DOS commands
(课设第一套)1-4 消息传递接口 (100 分)(模拟:线程)
FairyGUI条子家族(滚动条,滑动条,进度条)
In 2020, the average salary of IT industry exceeded 170000, ranking first
[算法] 剑指offer2 golang 面试题4:只出现一次的数字
[algorithm] sword finger offer2 golang interview question 2: binary addition
第一人称视角的角色移动
Combination of fairygui check box and progress bar
Unity3d, Alibaba cloud server, platform configuration
Guided package method in idea
[algorithm] sword finger offer2 golang interview question 8: the shortest subarray with a sum greater than or equal to K