CodeForces - 1118F1 Tree Cutting (Easy Version) (树上dfs)

本文介绍了一种算法,用于解决在一个由n个顶点和n-1条边组成的树形结构中,找到可以将树分割成两部分的边,使得每部分只包含红色或蓝色顶点的问题。通过深度优先搜索预处理每个顶点的子树中包含的红色和蓝色顶点数量,从而确定好边的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Tree Cutting (Easy Version)

You are given an undirected tree of n vertices.

Some vertices are colored blue, some are colored red and some are uncolored. It is guaranteed that the tree contains at least one red vertex and at least one blue vertex.

You choose an edge and remove it from the tree. Tree falls apart into two connected components. Let’s call an edge nice if neither of the resulting components contain vertices of both red and blue colors.

How many nice edges are there in the given tree?

Input
The first line contains a single integer n (2≤n≤3⋅105) — the number of vertices in the tree.

The second line contains n integers a1,a2,…,an (0≤ai≤2) — the colors of the vertices. ai=1 means that vertex i is colored red, ai=2 means that vertex i is colored blue and ai=0 means that vertex i is uncolored.

The i-th of the next n−1 lines contains two integers vi and ui (1≤vi,ui≤n, vi≠ui) — the edges of the tree. It is guaranteed that the given edges form a tree. It is guaranteed that the tree contains at least one red vertex and at least one blue vertex.

Output
Print a single integer — the number of nice edges in the given tree.

Examples
Input
5
2 0 0 1 2
1 2
2 3
2 4
2 5
Output
1

题意:有n个点和n-1条边,每个点有一个颜色,1表示红色,2表示蓝色,0没有颜色。现在问你是否可以删除一条边,使得分成的两部分中一部分包含全部红色点,另一部分包含全部黑色点,输出共有多少条这样的边。

题解:首先dfs预处理出每个点的子树中包含红色和蓝色点的个数,这样如果一条边的某个端点包含全部蓝点或者包含全部红点,那这条边就可以删。

#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=600000+10;
int s[maxn];
int R=0,B=0;
struct cc{
    int from,to;
}es[maxn];
int first[maxn],nxt[maxn];
int tot=0;
void build(int ff,int tt)
{
    es[++tot]=(cc){ff,tt};
    nxt[tot]=first[ff];
    first[ff]=tot;
}
struct ff{
    int R,B;
}num[maxn];
int ans=0;
void dfs(int x,int fa)
{
    for(int i=first[x];i;i=nxt[i])
    {
        int v=es[i].to;
        if(v==fa) continue;
        dfs(v,x);
        num[x].R+=num[v].R;
        num[x].B+=num[v].B;
    }
}
void dfss(int x,int fa)
{
    for(int i=first[x];i;i=nxt[i])
    {
        int v=es[i].to;
        if(v==fa) continue;
        if((num[v].R==R&&num[v].B==0)||(num[v].R==0&&num[v].B==B))
        {
            ans++;
        }
        dfss(v,x);
    }
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&s[i]);
        if(s[i]==1)
        {
            R++;
            num[i].R++;
        }
        if(s[i]==2)
        {
            B++;
            num[i].B++;
        }
    }
    for(int i=1;i<n;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        build(x,y);
        build(y,x);
    }
    dfs(1,0);
    dfss(1,0);
    printf("%d\n",ans);
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值