codeforces1156D 0-1-Tree 换根dp

题目传送门

题意:

  给定一棵n个点的边权为0或1的树,一条合法的路径(x,y)(x≠y)满足,从x走到y,一旦经过边权为1的边,就不能再经过边权为0的边,求有多少边满足条件?

思路:设$f[u]$为以1为根,自下而上到$u$的末节点是1的合法路径数量,$g[u]$代表以1为根,自下而上到$v$末节点是0的合法路径数量,这个可以通过一遍dfs简单求解。

  再设$nf[u]$和$ng[u]$代表以u为根的两种合法路径数量,进行换根dfs,在换根的过程中:

  若某一条边是0边,则:

    $ng[st.to]=ng[u]$,$nf[st.to]=f[st.to]$。这个方程也很好理解,白边的路径是不会变的,所有从父节点自上而下转移过来的黑边到了这里都是非法路径了。

  若某一条边是1边,则:

    $ng[st.to]=g[st.to]$,$nf[st.to]=nf[u]-g[st.to]+ng[u]$,白边只有从下往上过来的了。黑边要减去  到当前位置为白边与父节点的黑边连接形成的边 ,再加上父节点是白边,加上黑边形成的边。

#pragma GCC optimize (2)
#pragma G++ optimize (2)
#pragma comment(linker, "/STACK:102400000,102400000")
#include<bits/stdc++.h>
#include<cstdio>
#include<vector>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,b,a) for(int i=b;i>=a;i--)
#define clr(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define pii pair<int,int >
using namespace std;
typedef long long ll;
const int maxn=200010;
ll rd()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
int T;
struct edge{
    int to,w;
};
vector<edge>ve[maxn];
int f[maxn],g[maxn],nf[maxn],ng[maxn];
int n,m;
ll ans;
void dfs_1(int u,int fa){
    for(auto &st:ve[u]){
        if(st.to==fa)continue;
        dfs_1(st.to,u);
        if(st.w==0){
            g[u]+=g[st.to]+1;
        }else{
            f[u]+=f[st.to]+1+g[st.to];
        }
    }
}
void dfs_2(int u,int fa){
    ans+=nf[u]+ng[u];
    for(auto &st:ve[u]){
        if(st.to==fa)continue;
        if(st.w==0){
            ng[st.to]=ng[u];
            nf[st.to]=f[st.to];
        }else{
            ng[st.to]=g[st.to];
            nf[st.to]=nf[u]-g[st.to]+ng[u];
        }
        dfs_2(st.to,u);
    }
}
int main(){
    cin>>n;
    rep(i,1,n-1){
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        ve[u].pb({v,w});
        ve[v].pb({u,w});
    }
    dfs_1(1,0);
    nf[1]=f[1],ng[1]=g[1];
    dfs_2(1,0);
    cout<<ans<<endl;
} 

原文地址:https://www.cnblogs.com/mountaink/p/11597906.html

时间: 2024-08-28 17:25:37

codeforces1156D 0-1-Tree 换根dp的相关文章

换根dp「小奇的仓库&#183;randomwalking&#183;」

把以前考试换根题集中写一下 随便选一个点做根一遍$dfs$求子树内贡献,再通过特殊手段算$ans[1]$,最后$dfs$求其他$ans$ 拆成子树内,子树外分别算贡献差,得儿子是很常见套路了 小奇的仓库 $M<=15$ 题解 很久之前做的换根dp,当时觉得真是神仙,现在看还是觉得很神仙 不同于一般换根dp,这个题$n^2$并不好写 所以$n^2$算法就省略了 考虑$M$非常小,可以计算$M$对答案影响 一个直接的想法是先算出来原答案,再减去现在答案 //本来为j现在异或M,变化了j-delta

换根DP

换根dp的通法:1.第一次扫描时,任选一个点为根,在"有根树"上执行一次树形DP,也就在回溯时发生的,自底向上的状态转移. 2.第二次扫描时,从刚才选出的根出发,对整棵树执行一次dfs,在每次递归前进行自上向下的推导,计算出换根后的解. 1.POJ3585 Accumulation Degree dp[i]以i为根的子树中,把i作为源点的最大流量 转移\(dp[x]=\sum_{y\epsilon son(x)}^{}\left\{\begin{matrix} min(dp[y],le

poj3585 Accumulation Degree(换根dp)

传送门 换根dp板子题(板子型选手 题意: 一棵树确定源点和汇点找到最大的流量(拿出一整套最大瘤板子orz const int maxn=2e5+10; int head[maxn],tot; struct node { int nt,to;long long w; }q[2*maxn]; long long dp[maxn];int cnt[maxn]; void insert(int u,int v,long long w) { q[tot].nt=head[u];q[tot].w=w;q[

【换根dp】9.22小偷

换根都不会了 题目大意 给定一棵$n$个点的树和树上一撮关键点,求到所有$m$个关键点距离的最大值$dis_{max}\le LIM$的点的个数. $n,m\le 30000,LIM\le 30000$ 题目分析 考虑在求出一个点的情况下如何转移到其子节点. 对点$u$最直接关心的状态是$mx[u]$:所有关键点到$u$的最大距离. 对点$u$的子节点$v$来说,$u$能带给它的只是“外面的世界”——$v$子树的补集这块贡献,也就是对于$u$的除了$v$子树的$mx[u]$. 因为$mx[u]$

HDU 2196 Computer 二次扫描与换根DP

题意:给定一棵树,求树上所有点到其最远点的距离. 数据范围: 1 <= N <= 100000 ------------------------------------------我是分割线------------------------------------------ 题解:对于每个节点u来说,其可能到达的最长距离为max{其子树内的最长距离,其父节点不经过u的子树内的最长距离}.于是,我们便可以在第一遍dfs中预处理节点x到其子树内的最长距离,顺带求一下次长距离,方便转移. // f[

CodeForce - 1187 E. Tree Painting (换根dp)

You are given a tree (an undirected connected acyclic graph) consisting of nn vertices. You are playing a game on this tree. Initially all vertices are white. On the first turn of the game you choose one vertex and paint it black. Then on each turn y

换根dp+暴力+预处理+记忆化搜索——cf1292C好题!

/** 给定一棵树,要求给树边赋值[0,n-2],每个值只能使用一次 S = mex(u,v), mex(u,v)是u-v路径上没有出现过的编号最小的值 问使得S最大的赋值方式 由于很难直接统计答案,所以考虑统计每条边的贡献 包含(0)路径的贡献tot1是其左右子树size的乘积 包含(0,1)的路径的贡献tot2是其左右子树的size乘积 ...依次类推 显然:只包含(1,2)这样的路径是没有贡献的 那么原问题转化为如何分配[0,n-2],使得最后的乘积和最大 dp[u][v]表示路径(u,v

newcoder 79F 小H和圣诞树 换根 DP + 根号分治

Code: #include <cstdio> #include <algorithm> #include <vector> #include <cmath> #include <map> #define N 100003 #define ll long long #define setIO(s) freopen(s".in", "r" , stdin) , freopen(s".out"

经典换根dp——hdu2196

给定一棵边权树,求距离每个点最远的点,输出这个距离 #include<bits/stdc++.h> using namespace std; #define N 10005 struct Edge{int to,nxt,w;}e[N<<1]; int head[N],tot,n; void init(){memset(head,-1,sizeof head);tot=0;} void add(int u,int v,int w){ e[tot].to=v;e[tot].w=w;e[