F1. Tree Cutting (Easy Version)

 http://codeforces.com/contest/1118/problem/F1

题意:给一棵树,有些节点是红色或蓝色,或无色,切掉一条边后分成两棵树且红蓝在不同的树;

有多少条边可以切;

思路:一个节点的一个子树点有么包含红的全部且不包含蓝的,或包含蓝的全部而不包含红的,那么这条边可以切;dfs是计算子树红蓝个数是否满足条件;

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

#define sfi(i) scanf("%d",&i)
#define pri(i) printf("%d\n",i)
#define sff(i) scanf("%lf",&i)
#define ll long long
#define mem(x,y) memset(x,y,sizeof(x))
#define INF 0x3f3f3f3f
#define eps 1e-6
#define PI acos(-1)
#define lowbit(x) ((x)&(-x))
#define zero(x) (((x)>0?(x):-(x))
inline void sc(T &ret)
{
    char c;
    ret = 0;
    while ((c = getchar()) < '0' || c > '9');
    while (c >= '0' && c <= '9')
    {
        ret = ret * 10 + (c - '0'), c = getchar();
    }
}

vectorg[maxn];
int a[maxn];
int ans=0,red=0,blue=0;
pair dfs(int u,int fa)
{
    int r=0;
    int b=0;
    if(a[u]==1) r++;
    if(a[u]==2) b++;
    for(int i=0;i tmp=dfs(v,u);
        if(tmp.first==red&&tmp.second==0) ans++;
        if(tmp.first==0&&tmp.second==blue) ans++;
        r+=tmp.first;
        b+=tmp.second;
    }
    return make_pair(r,b);
}
int main()
{
    int n;
    cin>>n;
    for(int i=0;i>a[i];
        if(a[i]==1) red++;
        if(a[i]==2) blue++;
    }
    for(int i=0;i>u>>v;
        u--;
        v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    ans=0;
    dfs(0,-1);
    cout<

 

你可能感兴趣的:(dfs)