题目:http://poj.org/problem?id=3417

根据一条边被几个环覆盖来判断能不能删、有几种情况等;

用树上差分,终点 s++,LCA s-=2,统计时计算子树s值的和即可;

用ST表做LCA,不知为何WA了:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int const MAXN=1e5+;
int n,m,head[MAXN],ct,dep[MAXN],pre[MAXN][],s[MAXN];
long long ans;
struct N{
int to,next;
N(int t=,int n=):to(t),next(n) {}
}edge[MAXN<<];
void dfs(int x,int f)
{
dep[x]=dep[f]+;
pre[x][]=f;
for(int i=head[x];i;i=edge[i].next)
if(edge[i].to!=f)dfs(edge[i].to,x);
}
void init()
{
dfs(,);
for(int k=;k<=;k++)
for(int i=;i<=n;i++)
pre[i][k]=pre[pre[i][k-]][k-];
}
int lca(int x,int y)
{
if(dep[x]>dep[y])swap(x,y);
int d=dep[y]-dep[x];
for(int i=;i<=;i++)
if(/*d&(1<<i)*/(d>>i)&)y=pre[y][i];
for(int i=;i>=;i--)
if(pre[x][i]!=pre[y][i])
{
x=pre[x][i];
y=pre[y][i];
}
return pre[x][];
}
long long dfs2(int x,int f)
{
long long sum=s[x];
for(int i=head[x],u;i;i=edge[i].next)
{
if((u=edge[i].to)==f)continue;
long long k=dfs2(u,x);
if(k==)ans+=m;
if(k==)ans++;
sum+=k;
}
return sum;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=,x,y;i<n;i++)
{
scanf("%d%d",&x,&y);
edge[++ct]=N(y,head[x]);head[x]=ct;
edge[++ct]=N(x,head[y]);head[y]=ct;
}
init();
for(int i=,x,y;i<=m;i++)
{
scanf("%d%d",&x,&y);
if(x==y)continue;
s[x]++;s[y]++;
s[lca(x,y)]-=;
}
dfs2(,);
printf("%lld",ans);
return ;
}

ST表为什么WA

于是改成了tarjan,过程中求答案;

注意非树边加边时判掉x=y的情况。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int const MAXN=1e5+;
int n,m,head[MAXN],ct,s[MAXN],fa[MAXN],ct2,head2[MAXN];
long long ans;
bool vis[MAXN];
struct N{
int to,next;
N(int t=,int n=):to(t),next(n) {}
}edge[MAXN<<],ed[MAXN<<];
int find(int x)
{
if(fa[x]==x)return x;
return fa[x]=find(fa[x]);
}
void tarjan(int x)
{
fa[x]=x;vis[x]=;
for(int i=head2[x],u;i;i=ed[i].next)
if(vis[u=ed[i].to])s[find(u)]-=;
for(int i=head[x],u;i;i=edge[i].next)
{
if(vis[u=edge[i].to])continue;//fa
tarjan(u);fa[u]=x;s[x]+=s[u];
if(s[u]==)ans+=m;
if(s[u]==)ans++;
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=,x,y;i<n;i++)
{
scanf("%d%d",&x,&y);
edge[++ct]=N(y,head[x]);head[x]=ct;
edge[++ct]=N(x,head[y]);head[y]=ct;
}
for(int i=,x,y;i<=m;i++)
{
scanf("%d%d",&x,&y);
if(x==y)continue;//!
s[x]++;s[y]++;
ed[++ct2]=N(y,head2[x]);head2[x]=ct2;
ed[++ct2]=N(x,head2[y]);head2[y]=ct2;
}
tarjan();
printf("%d",ans);
return ;
}

poj3417 Network——LCA+树上差分的更多相关文章

  1. [BZOJ3307]:雨天的尾巴(LCA+树上差分+权值线段树)

    题目传送门 题目描述: N个点,形成一个树状结构.有M次发放,每次选择两个点x,y对于x到y的路径上(含x,y)每个点发一袋Z类型的物品.完成所有发放后,每个点存放最多的是哪种物品. 输入格式: 第一 ...

  2. [luogu P3128][USACO15DEC]Max Flow [LCA][树上差分]

    题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his b ...

  3. bzoj4326 树链剖分 + 线段树 // 二分 lca + 树上差分

    https://www.lydsy.com/JudgeOnline/problem.php?id=4326 题意:N个点的树上给M条树链,问去掉一条边的权值之后所有树链长度和的最大值最小是多少. 首先 ...

  4. 2018.08.22 codves2370 小机房的树(lca+树上差分)

    传送门 一道板子题. 直接树链剖分维护树上lca然后差分就行了. 代码: #include<bits/stdc++.h> #define N 50005 #define lc (p< ...

  5. 【洛谷】【lca+树上差分】P3258 [JLOI2014]松鼠的新家

    [题目描述:] 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n(2 ≤ n ≤ 300000)个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真 ...

  6. [JLOI2014] 松鼠的新家 (lca/树上差分)

    [JLOI2014]松鼠的新家 题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在 ...

  7. LOJ2425 NOIP2015 运输计划 【二分+LCA+树上差分】*

    LOJ2425 NOIP2015 运输计划 LINK 题意:给你一颗树,可以将任意一条边的权值变成0,然后求m条路径的长度的最小值 思路: 先二分最后的距离ans,然后我们把路程大于ans的所有路径拿 ...

  8. [BZOJ3631]:[JLOI2014]松鼠的新家(LCA+树上差分)

    题目传送门 题目描述: 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在“树”上.松鼠想邀 ...

  9. bzoj 3307: 雨天的尾巴【树剖lca+树上差分+线段树合并】

    这居然是我第一次写线段树合并--所以我居然在合并的时候加点结果WAWAWAMLEMLEMLE--!ro的时候居然直接指到la就行-- 树上差分,每个点建一棵动态开点线段树,然后统计答案的时候合并即可 ...

随机推荐

  1. STM32串行通信USART解说笔记

    STM32串行通信USART程序例举链接:http://blog.csdn.net/dragon12345666/article/details/24883111 1.STM32串行通信USART的相 ...

  2. ZOJ 3626 Treasure Hunt I(树形dp)

    Treasure Hunt I Time Limit: 2 Seconds      Memory Limit: 65536 KB Akiba is a dangerous country since ...

  3. 不错.net图片水印类

    using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Draw ...

  4. Android - Activity定制横屏(landscape)显示

    Activity定制横屏(landscape)显示 本文地址: http://blog.csdn.net/caroline_wendy Android横屏(landscape)显示:  android ...

  5. 【CODEFORCES】 B. Dreamoon and Sets

    B. Dreamoon and Sets time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. AVL平衡树的插入例程

    /* **AVL平衡树插入例程 **2014-5-30 11:44:50 */ avlTree insert(elementType X, avlTree T){ if(T == NULL){ T = ...

  7. TFS 解除独占锁定

    cmd 进入Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE tf workspace /delete 工作区名;创建的用户 / ...

  8. 02 svn 文件提交与目录结构

    一:文件操作给svn服务器提交程序文件: ① 在被提交文件的身上点击右键------> tortoiseSVN----->add ② 在被提交文件身上点击右键------> comm ...

  9. 2015最新iherb海淘攻略-图文入门教程-6月免邮

    注:仅仅有首次下单才享有新人优惠10$,大家下单之后千万不要取消后.否则之后则不享有新人优惠. 注:眼下Sino-海淘客国际物流已取消,仅有UCS合众速递. IHerb是美国最热门的海淘海购网站之中的 ...

  10. UVA - 1045 The Great Wall Game(二分图最佳完美匹配)

    题目大意:给出棋盘上的N个点的位置.如今问将这些点排成一行或者一列.或者对角线的最小移动步数(每一个点都仅仅能上下左右移动.一次移动一个) 解题思路:暴力+二分图最佳完美匹配 #include < ...