博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ——T3417 Network
阅读量:4597 次
发布时间:2019-06-09

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

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 5294   Accepted: 1517

Description

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

Source

, Yang Mu
 
 
n-1条边构成的树,m条边中每加一条比构成环,假设新边为(u,v),那么环为u->LCA(u,v)->v->u,我们给这个环上的边计数1,表示这些边被一个环覆盖了一次。
可以发现

1.覆盖0次,说明这条边不在任何一个环上,这样的边最脆弱,单单是毁掉它就已经可以使树断裂了,这时候只要任意选一条新边去毁,树还是断裂的,所以这样的树边,就产生m种方案(m为新边条数)

2.覆盖1次,说明这条边在一个环上,且,仅在一个环上,那么要使树断裂,就毁掉这条树边,并且毁掉和它对应的那条新边(毁其他的新边无效),就一定能使树断裂,这种树边能产生的方案数为1,一条这样的树边只有唯一解

3.覆盖2次或以上,无论怎么样都不能使树断裂,产生的方案数为0

树上查分维护覆盖次数,树剖求得lca

1 #include 
2 3 #define swap(a,b) {int tmp=a;a=b;b=tmp;} 4 inline void read(int &x) 5 { 6 x=0; register char ch=getchar(); 7 for(; ch>'9'||ch<'0'; ) ch=getchar(); 8 for(; ch>='0'&&ch<='9'; ch=getchar()) x=x*10+ch-'0'; 9 }10 11 const int N(100005);12 int head[N],sumedge;13 struct Edge {14 int v,next;15 Edge(int v=0,int next=0):v(v),next(next){}16 }edge[N<<2];17 inline void ins(int u,int v)18 {19 edge[++sumedge]=Edge(v,head[u]); head[u]=sumedge;20 edge[++sumedge]=Edge(u,head[v]); head[v]=sumedge;21 }22 23 int top[N],size[N],dep[N],son[N],dad[N];24 void DFS(int u,int depth)25 {26 size[u]=1,dep[u]=depth;27 for(int v,i=head[u]; i; i=edge[i].next)28 {29 v=edge[i].v;30 if(v==dad[u]) continue;31 dad[v]=u; DFS(v,depth+1); size[u]+=size[v];32 if(size[son[u]]

 

转载于:https://www.cnblogs.com/Shy-key/p/7599161.html

你可能感兴趣的文章
6 tr
查看>>
同开三本DJANGO,需要提升一下本职工作的能力啦
查看>>
这样就算会了PHP么?-2
查看>>
线段树 (区间查询最大 区间求和 区间加)带lazy
查看>>
三十而立,从零开始学ios开发(十二):Table Views(上)
查看>>
MySQL中的decimal
查看>>
gitlab+jenkins持续集成(一)
查看>>
4.signed/unsigned char
查看>>
iOS,UIImage有个contentmodel属性
查看>>
Debian 7 amd64 + fbterm + ucimf
查看>>
数据结构之【排序】复习题
查看>>
spring boot 首次请求Controller慢
查看>>
事件绑定
查看>>
grep命令详解
查看>>
iterm2快捷键
查看>>
asp.net 生成PDF方法
查看>>
EntityFramework 7 Join Count LongCount 奇怪问题(已修复)
查看>>
设计模式---组件协作模式之模板方法模式(Tempalte Method)
查看>>
程序员心理看WEB开发框架
查看>>
@Data 注解在实体类的使用可省去生成GET,SET方法
查看>>