POJ3417Network(LCA+树上查分||树剖+线段树)
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+树上查分||树剖+线段树)的更多相关文章
- 【bzoj4699】树上的最短路(树剖+线段树优化建图)
题意 给你一棵 $n$ 个点 $n-1$ 条边的树,每条边有一个通过时间.此外有 $m$ 个传送条件 $(x_1,y_1,x_2,y_2,c)$,表示从 $x_1$ 到 $x_2$ 的简单路径上的点可 ...
- [LNOI2014]LCA(树剖+线段树)
\(\%\%\% Fading\) 此题是他第一道黑题(我的第一道黑题是蒲公英) 一直不敢开,后来发现是差分一下,将询问离线,树剖+线段树维护即可 \(Code\ Below:\) #include ...
- LUOGU P1967 货车运输(最大生成树+树剖+线段树)
传送门 解题思路 货车所走的路径一定是最大生成树上的路径,所以先跑一个最大生成树,之后就是求一条路径上的最小值,用树剖+线段树,注意图可能不连通.将边权下放到点权上,但x,y路径上的lca的答案不能算 ...
- BZOJ_2238_Mst_树剖+线段树
BZOJ_2238_Mst_树剖+线段树 Description 给出一个N个点M条边的无向带权图,以及Q个询问,每次询问在图中删掉一条边后图的最小生成树.(各询问间独立,每次询问不对之后的询问产生影 ...
- BZOJ_4551_[Tjoi2016&Heoi2016]树_树剖+线段树
BZOJ_4551_[Tjoi2016&Heoi2016]树_树剖+线段树 Description 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为 ...
- 【BZOJ5210】最大连通子块和 树剖线段树+动态DP
[BZOJ5210]最大连通子块和 Description 给出一棵n个点.以1为根的有根树,点有点权.要求支持如下两种操作: M x y:将点x的点权改为y: Q x:求以x为根的子树的最大连通子块 ...
- [CF1007D]Ants[2-SAT+树剖+线段树优化建图]
题意 我们用路径 \((u, v)\) 表示一棵树上从结点 \(u\) 到结点 \(v\) 的最短路径. 给定一棵由 \(n\) 个结点构成的树.你需要用 \(m\) 种不同的颜色为这棵树的树边染色, ...
- LOJ#3088. 「GXOI / GZOI2019」旧词(树剖+线段树)
题面 传送门 题解 先考虑\(k=1\)的情况,我们可以离线处理,从小到大对于每一个\(i\),令\(1\)到\(i\)的路径上每个节点权值增加\(1\),然后对于所有\(x=i\)的询问查一下\(y ...
- 洛谷P4315 月下“毛景树”(树剖+线段树)
传送门 woc这该死的码农题…… 把每一条边转化为它连接的两点中深度较深的那一个,然后就可以用树剖+线段树对路径进行修改了 然后顺便注意在上面这种转化之后,树剖的时候不能搞$LCA$ 然后是几个注意点 ...
- BZOJ_2157_旅游_树剖+线段树
BZOJ_2157_旅游_树剖+线段树 Description Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但 ...
随机推荐
- JOB Hunting 总结-----2013-11-5
从9月份开始的找工作大战,告一段落:其实早在10月中旬就已搞定,现在回想起这过去的几个月,很充实,很疲惫,很挫败又很有成就感! 开始找工作,对未来有过很多憧憬,也很迷茫,不知道自己的未来会在 ...
- dede内容页调用点击数
<script src="{dede:field name='phpurl'/}/count.php?view=yes&aid={dede:field name='id'/} ...
- 【Java】Spring Web MVC注意事项
本文内容可能是书上没有的,至少是<Java Web整合开发实践>这本书上没有的.这是初学Spring的笔者走过的弯路,谨记以自勉. 这两天学习Spring WebMVC,照着书依葫芦画瓢写 ...
- diy文件系统上创建文件的流程
[0]README 0.1) source code are from orange's implemention of a os , and for complete code , please v ...
- os引导程序boot 在根目录区寻找os加载程序文件loader 对应的根目录条目
[0]README 0.0) source code from orange's implemention of a os and for complete code , please visit h ...
- 基于redis的分布式锁二种应用场景
“分布式锁”是用来解决分布式应用中“并发冲突”的一种常用手段,实现方式一般有基于zookeeper及基于redis二种.具体到业务场景中,我们要考虑二种情况: 一.抢不到锁的请求,允许丢弃(即:忽略) ...
- C#多线程学习(六) 互斥对象
如何控制好多个线程相互之间的联系,不产生冲突和重复,这需要用到互斥对象,即:System.Threading 命名空间中的 Mutex 类. 我们可以把Mutex看作一个出租车,乘客看作线程.乘客首先 ...
- String知识点
- JAVA解析XML之DOM方式
JAVA解析XML之DOM方式 准备工作 创建DocumentBuilderFactory对象; 创建DocumentBuilder对象; 通过DocumentBuilder对象的parse方法 ...
- 在pycharm中误删了Python文件,怎么办,挺急的?
1.今天在操作的时候,误删了文件,怎么办? (1)找到删除的上一级文件=>右键,找到Local History=>Show History (2),打开之后,双击左边的恢复时间内容,在右边 ...