博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj3417 network ( LCA)
阅读量:5160 次
发布时间:2019-06-13

本文共 2671 字,大约阅读时间需要 8 分钟。

Yixght is a manager of the company called SzqNetwork(SN). Now she's very worried because she has just received a bad news which denotes that DxtNetwork(DN), the SN's business rival, intents to attack the network of SN. More unfortunately, the original network of SN is so weak that we can just treat it as a tree. Formally, there are N nodes in SN's network, N-1 bidirectional channels to connect the nodes, and there always exists a route from any node to another. In order to protect the network from the attack, Yixght builds M new bidirectional channels between some of the nodes.

As the DN's best hacker, you can exactly destory two channels, one in the original network and the other among the M new channels. Now your higher-up wants to know how many ways you can divide the network of SN into at least two parts.

Input

The first line of the input file contains two integers: N (1 ≤ N ≤ 100 000), M (1 ≤ M ≤ 100 000) — the number of the nodes and the number of the new channels.

Following N-1 lines represent the channels in the original network of SN, each pair (a,b) denote that there is a channel between node a and node b.

Following M lines represent the new channels in the network, each pair (a,b) denote that a new channel between node a and node b is added to the network of SN.

Output

Output a single integer — the number of ways to divide the network into at least two parts.

Sample Input

4 11 22 31 43 4

Sample Output

3

题意:

n个点,成一棵树,m条新边,问在原先的树中和m条新边中去掉一条边使正好分成两部分的方案

 

题解:

对于一组新边(u,v),就相当于 对u->v的所有原来的边进行一次覆盖,

若(u1,v1)正好有一次覆盖则这条边会对答案有1的影响。

若覆盖次数>1 则无法成立。对于那些覆盖次数为0的则对答案有m的贡献。

这样我们就要求覆盖的次数,可用LCA覆盖,dfs根节点算答案。

注意细节!!!!!

 

 

 

#include
#include
#include
using namespace std;const int MAXN=110000;const int MAXM=21;struct E{int u,v,next;}mm[MAXN<<1];int n,m,len,dep[MAXN],fa[MAXN][MAXM],cnt[MAXN],h[MAXN],ans;inline void ins(int u,int v){ ++len; mm[len].u=u;mm[len].v=v;mm[len].next=h[u];h[u]=len;}void dfs(int u,int f){ dep[u]=dep[f]+1;fa[u][0]=f; for(int k=h[u] ; k ; k=mm[k].next){ int v=mm[k].v; if(v!=f){ dfs(v,u); } }}int LCA(int x,int y){ if(dep[x] < dep[y]) swap(x,y); for(int j=MAXM-1;j>=0;j--) if(dep[fa[x][j]] >= dep[y]) x=fa[x][j]; if(x==y) return x; for(int j=MAXM-1;j>=0;j--) if(fa[x][j] != fa[y][j]) x=fa[x][j],y=fa[y][j]; return fa[x][0];} void dfs1(int x,int f){ for(int k=h[x] ; k ; k=mm[k].next){ int y=mm[k].v; if(y!=f){ dfs1(y,x); cnt[x]+=cnt[y]; } } if(x==1) return ; if(cnt[x]==1) ans+=1; if(cnt[x]==0) ans+=m; } int main(){// freopen("D.in","r",stdin); scanf("%d%d",&n,&m); len=0;memset(h,0,sizeof h); for(int i=1,u,v;i

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/Exception2017/p/10252111.html

你可能感兴趣的文章
JS一些概念知识及参考链接
查看>>
关于JS中&&和||用法技巧
查看>>
TCP/IP协议原理与应用笔记24:网际协议(IP)之 IP协议的简介
查看>>
SAP HANA开发中常见问题- 基于SAP HANA平台的多团队产品研发
查看>>
内部元素一一相应的集合的算法优化,从list到hashmap
查看>>
游戏中的心理学(一):认知失调有前提条件
查看>>
SpringMVC-处理AJAX
查看>>
WHAT I READ FOR DEEP-LEARNING
查看>>
【Ruby】Ruby在Windows上的安装
查看>>
Objective C 总结(十一):KVC
查看>>
BZOJ 3747 洛谷 3582 [POI2015]Kinoman
查看>>
vue实战(7):完整开发登录页面(一)
查看>>
[转载]mysql的left,right,substr,instr截取字符串,截取
查看>>
Visual Studio自定义模板(二)
查看>>
【Mood-20】滴滤咖啡做法 IT工程师加班必备 更健康的coffee 项目经理加班密鉴
查看>>
摘抄详细的VUE生命周期
查看>>
javascript高级程序设计---js事件思维导图
查看>>
sprint计划会议
查看>>
读《构建之法-软件工程》第四章有感
查看>>
使用 Printf via SWO/SWV 输出调试信息
查看>>