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 1
1 2
2 3
1 4
3 4

Sample Output

3

题意:

一棵树,后来加了m条新边,形成了一个有环无向图,问删去原树上一条边和m条新边中的一条,使得图不连通。这样的方案有多少。

思路:

以前看到过,完全没有思路,放弃了。今天又看到了,还是没有思路。。。GG

把原图dfs建立成有根树,假设1是根节点,再想,有方向可能就有思路了,oh,还是没有。。。GG

  • 试着手动在两点之间加一条新边,我们看到形成了一个环。
  • 对于环上加的一条新边,两点间每一条原树边都多进入了一个环。
  • 1,      如果一条边进入了两个环,则不用再考虑它。
  • 2,      如果只进入一个环,则它与对应的新边是一组答案。
  • 3,      如果一条表没有进入任何环,那它与任何一条新边是一组答案。
  • 对每一条新边e,其端点为u,v,LCA(u,v)=a,则把路径u-a-v上每一条边加1,最后处理即可。
  • 显然可以用线段树+lazy做。数据上感觉可以过。但是学到了树上差分。。。像前缀和一样。感觉很简单,但是没想到。。。

:sum[u]+1;sum[v]+1;sum[a]-2; 由叶子向根root累加即可。

注意:对象是边,而不是顶点的sum[]。

我写的是树剖(因为在练习树剖),注意找LCA的时候注意 if(dpt[u]<dpt[v]) 是刷新。

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<algorithm>
#define swap(a,b) {a^=b;b^=a;a^=b;}
using namespace std;
const int maxn=;
int Laxt[maxn],Next[maxn],To[maxn],cnt;
int fa[maxn],son[maxn],size[maxn],dpt[maxn],top[maxn];
int n,m,ans,sum[maxn];
void add(int u,int v)
{
Next[++cnt]=Laxt[u];
Laxt[u]=cnt; To[cnt]=v;
}
void dfs1(int u,int pre)
{
fa[u]=pre;dpt[u]=dpt[pre]+;size[u]=;son[u]=;
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i]; if(v==pre) continue;
dfs1(v,u);size[u]+=size[v];
if(!son[u]||size[v]>size[son[u]]) son[u]=v;
}
}
void dfs2(int u,int Top)
{
top[u]=Top; if(!son[u]) return ; dfs2(son[u],Top);
for(int i=Laxt[u];i;i=Next[i])
if(To[i]!=fa[u]&&To[i]!=son[u]) dfs2(To[i],To[i]);
} int dfs3(int u,int pre)
{
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i];
if(v!=pre){
int tmp=dfs3(v,u);
sum[u]+=tmp;
if(tmp==) ans+=m;
if(tmp==) ans+=;
}
}
return sum[u];
}
int find(int u,int v)
{
int a=top[u],b=top[v];
while(a!=b){
if(dpt[a]<dpt[b]) { swap(a,b);swap(u,v);}
u=fa[a]; a=top[u];
}
if(dpt[u]<dpt[v]) return u; return v;
}
int main()
{ while(~scanf("%d%d",&n,&m)){
memset(Laxt,,sizeof(Laxt)); cnt=;
memset(sum,,sizeof(sum)); ans=;
for(int i=;i<n;i++){
int v,u; scanf("%d%d",&u,&v);
add(u,v);add(v,u);
}
dfs1(,);
dfs2(,);
for(int i=;i<=m;i++){
int u,v; scanf("%d%d",&u,&v);
int anc=find(u,v);
sum[u]++;sum[v]++;sum[anc]-=;
}
dfs3(,);
printf("%d\n",ans);
} return ;
}

POJ3417Network(LCA+树上查分||树剖+线段树)的更多相关文章

  1. 【bzoj4699】树上的最短路(树剖+线段树优化建图)

    题意 给你一棵 $n$ 个点 $n-1$ 条边的树,每条边有一个通过时间.此外有 $m$ 个传送条件 $(x_1,y_1,x_2,y_2,c)$,表示从 $x_1$ 到 $x_2$ 的简单路径上的点可 ...

  2. [LNOI2014]LCA(树剖+线段树)

    \(\%\%\% Fading\) 此题是他第一道黑题(我的第一道黑题是蒲公英) 一直不敢开,后来发现是差分一下,将询问离线,树剖+线段树维护即可 \(Code\ Below:\) #include ...

  3. LUOGU P1967 货车运输(最大生成树+树剖+线段树)

    传送门 解题思路 货车所走的路径一定是最大生成树上的路径,所以先跑一个最大生成树,之后就是求一条路径上的最小值,用树剖+线段树,注意图可能不连通.将边权下放到点权上,但x,y路径上的lca的答案不能算 ...

  4. BZOJ_2238_Mst_树剖+线段树

    BZOJ_2238_Mst_树剖+线段树 Description 给出一个N个点M条边的无向带权图,以及Q个询问,每次询问在图中删掉一条边后图的最小生成树.(各询问间独立,每次询问不对之后的询问产生影 ...

  5. BZOJ_4551_[Tjoi2016&Heoi2016]树_树剖+线段树

    BZOJ_4551_[Tjoi2016&Heoi2016]树_树剖+线段树 Description 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为 ...

  6. 【BZOJ5210】最大连通子块和 树剖线段树+动态DP

    [BZOJ5210]最大连通子块和 Description 给出一棵n个点.以1为根的有根树,点有点权.要求支持如下两种操作: M x y:将点x的点权改为y: Q x:求以x为根的子树的最大连通子块 ...

  7. [CF1007D]Ants[2-SAT+树剖+线段树优化建图]

    题意 我们用路径 \((u, v)\) 表示一棵树上从结点 \(u\) 到结点 \(v\) 的最短路径. 给定一棵由 \(n\) 个结点构成的树.你需要用 \(m\) 种不同的颜色为这棵树的树边染色, ...

  8. LOJ#3088. 「GXOI / GZOI2019」旧词(树剖+线段树)

    题面 传送门 题解 先考虑\(k=1\)的情况,我们可以离线处理,从小到大对于每一个\(i\),令\(1\)到\(i\)的路径上每个节点权值增加\(1\),然后对于所有\(x=i\)的询问查一下\(y ...

  9. 洛谷P4315 月下“毛景树”(树剖+线段树)

    传送门 woc这该死的码农题…… 把每一条边转化为它连接的两点中深度较深的那一个,然后就可以用树剖+线段树对路径进行修改了 然后顺便注意在上面这种转化之后,树剖的时候不能搞$LCA$ 然后是几个注意点 ...

  10. BZOJ_2157_旅游_树剖+线段树

    BZOJ_2157_旅游_树剖+线段树 Description Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但 ...

随机推荐

  1. VueJS组件之全局组件与局部组件

    全局组件 所有实例都能用全局组件. HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8& ...

  2. swift菜鸟入门视频教程-05-控制流

    本人自己录制的swift菜鸟入门.欢迎大家拍砖.有什么问题能够在这里留言. 主要内容: For 循环 While 循环 条件语句 控制转移语句(Control Transfer Statements) ...

  3. 33:字符统计SumOfCharactors

    题目描述:如果统计的个数相同,则按照ASII码由小到大排序输出 .如果有其他字符,则对这些字符不用进行统计. 实现以下接口: 输入一个字符串,对字符中的各个英文字符,数字,空格进行统计(可反复调用) ...

  4. 栈(C++)

    简介: 限定仅在表尾进行插入或删除操作的线性表 表尾端称为栈顶(top),表头端称为栈底(bottom) 特点: 在栈中,后入栈的元素先出栈 C语言版本 用于测试的文件,以及测试结果可以去作者GitH ...

  5. gulp入门-压缩js/css文件(windows)

    类似于grunt,都是基于Node.js的前端构建工具.不过gulp压缩效率更高. 工具/原料 nodejs/npm 方法/步骤 首先要确保pc上装有node,然后在global环境和项目文件中都in ...

  6. HDFS源码分析心跳汇报之周期性心跳

    HDFS源码分析心跳汇报之周期性心跳,近期推出!

  7. Nginx与Apache的Rewrite规则的区别

    一.Nginx Rewrite规则相关指令 Nginx Rewrite规则相关指令有if.rewrite.set.return.break等,其中rewrite是最关键的指令.一个简单的Nginx R ...

  8. CGI FASTCGI php-fpm

    CGI(Common Gateway Interface) CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工 ...

  9. C#如何遍历数组?

    // 一维数组 int[] arr = { 1, 2, 3, 4, 5 }; foreach (int i in arr) { Console.WriteLine(i.ToString() + &qu ...

  10. 【题解】NOI2015软件包管理器

    [题解][P2146 NOI2015]软件包管理器 实际上就是树链剖分板子题. 对于\(install\)操作,直接查询它到\(0\)节点有多少已经安装了的,再用总数减去它. 对于\(uninstal ...