任意门:http://codeforces.com/contest/1118/problem/F1
2 seconds
256 megabytes
standard input
standard output
You are given an undirected tree of nn 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?
The first line contains a single integer nn (2≤n≤3⋅1052≤n≤3⋅105) — the number of vertices in the tree.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤20≤ai≤2) — the colors of the vertices. ai=1ai=1 means that vertex ii is colored red, ai=2ai=2 means that vertex ii is colored blue and ai=0ai=0 means that vertex ii is uncolored.
The ii-th of the next n−1n−1 lines contains two integers vivi and uiui (1≤vi,ui≤n1≤vi,ui≤n, vi≠uivi≠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.
Print a single integer — the number of nice edges in the given tree.
5
2 0 0 1 2
1 2
2 3
2 4
2 5
1
5
1 0 0 0 2
1 2
2 3
3 4
4 5
4
3
1 1 2
2 3
1 3
0
Here is the tree from the first example:
The only nice edge is edge (2,4)(2,4). Removing it makes the tree fall apart into components {4}{4} and {1,2,3,5}{1,2,3,5}. The first component only includes a red vertex and the second component includes blue vertices and uncolored vertices.
Here is the tree from the second example:
Every edge is nice in it.
Here is the tree from the third example:
Edge (1,3)(1,3) splits the into components {1}{1} and {3,2}{3,2}, the latter one includes both red and blue vertex, thus the edge isn't nice. Edge (2,3)(2,3) splits the into components {1,3}{1,3} and {2}{2}, the former one includes both red and blue vertex, thus the edge also isn't nice. So the answer is 0.
题意概括:
给出一棵无向树,每个结点有一种颜色(1红 2蓝 0无色),如果删掉某条边可以把这棵树分成两部分刚好各部分只具有一种颜色,则这种边为nice边,求最多有多少条。
解题思路:
哈哈哈,一开始试着BFS暴力了一波,果然水不过。
正解DFS,很明显先预处理出 每个结点为跟的子树所具有的两种颜色的结点个数 dd[ x ][ 1 ] && dd[ x ][ 2 ];
DFS一遍整棵树,判断当前边所分成的两部分是否满足条件,一部分就是预处理的子树部分,另一部分就是用树的根节点部分减去子树部分。
AC code:
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 3e5+;
int color[MAXN];
int u[MAXN], v[MAXN];
int dd[MAXN][];
bool vis[MAXN];
int N, ans;
struct Edge
{
int v, nxt;
}edge[MAXN<<];
int head[MAXN], cnt; void init()
{
memset(head, -, sizeof(head));
cnt = ;
} void add(int from, int to)
{
edge[cnt].v = to;
edge[cnt].nxt = head[from];
head[from] = cnt++;
} void dfs1(int x, int fa)
{
int v;
if(color[x] == ) dd[x][]+=;
else if(color[x] == ) dd[x][]+=;
for(int i = head[x]; i != -; i = edge[i].nxt){
v = edge[i].v;
if(v == x || v == fa) continue;
dfs1(v, x);
dd[x][]+=dd[v][];
dd[x][]+=dd[v][];
}
} void dfs2(int x, int fa)
{
int v;
for(int i = head[x]; i != -; i = edge[i].nxt){
v = edge[i].v;
if(v == fa) continue;
if(dd[v][] == && (dd[][]-dd[v][]) == ){
// printf("u:%d v:%d\n", x, v);
ans++;
}
else if(dd[v][] == && (dd[][]-dd[v][]) == ){
// printf("u:%d v:%d\n", x, v);
ans++;
}
dfs2(v, x);
}
} int main()
{
init();
scanf("%d", &N);
for(int i = ; i <= N; i++){
scanf("%d", &color[i]);
}
for(int i = ; i < N; i++){
scanf("%d %d", &u[i], &v[i]);
add(u[i], v[i]);
add(v[i], u[i]);
} dfs1(, );
ans = ;
dfs2(, ); printf("%d\n", ans); return ;
}