任意门:http://codeforces.com/contest/1118/problem/F1

F1. Tree Cutting (Easy Version)
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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?

Input

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.

Output

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

Examples
input

Copy
  1. 5
  2. 2 0 0 1 2
  3. 1 2
  4. 2 3
  5. 2 4
  6. 2 5
output

Copy
  1. 1
input

Copy
  1. 5
  2. 1 0 0 0 2
  3. 1 2
  4. 2 3
  5. 3 4
  6. 4 5
output

Copy
  1. 4
input

Copy
  1. 3
  2. 1 1 2
  3. 2 3
  4. 1 3
output

Copy
  1. 0
Note

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:

  1. #include <bits/stdc++.h>
  2. #define INF 0x3f3f3f3f
  3. #define LL long long
  4. using namespace std;
  5. const int MAXN = 3e5+;
  6. int color[MAXN];
  7. int u[MAXN], v[MAXN];
  8. int dd[MAXN][];
  9. bool vis[MAXN];
  10. int N, ans;
  11. struct Edge
  12. {
  13. int v, nxt;
  14. }edge[MAXN<<];
  15. int head[MAXN], cnt;
  16.  
  17. void init()
  18. {
  19. memset(head, -, sizeof(head));
  20. cnt = ;
  21. }
  22.  
  23. void add(int from, int to)
  24. {
  25. edge[cnt].v = to;
  26. edge[cnt].nxt = head[from];
  27. head[from] = cnt++;
  28. }
  29.  
  30. void dfs1(int x, int fa)
  31. {
  32. int v;
  33. if(color[x] == ) dd[x][]+=;
  34. else if(color[x] == ) dd[x][]+=;
  35. for(int i = head[x]; i != -; i = edge[i].nxt){
  36. v = edge[i].v;
  37. if(v == x || v == fa) continue;
  38. dfs1(v, x);
  39. dd[x][]+=dd[v][];
  40. dd[x][]+=dd[v][];
  41. }
  42. }
  43.  
  44. void dfs2(int x, int fa)
  45. {
  46. int v;
  47. for(int i = head[x]; i != -; i = edge[i].nxt){
  48. v = edge[i].v;
  49. if(v == fa) continue;
  50. if(dd[v][] == && (dd[][]-dd[v][]) == ){
  51. // printf("u:%d v:%d\n", x, v);
  52. ans++;
  53. }
  54. else if(dd[v][] == && (dd[][]-dd[v][]) == ){
  55. // printf("u:%d v:%d\n", x, v);
  56. ans++;
  57. }
  58. dfs2(v, x);
  59. }
  60. }
  61.  
  62. int main()
  63. {
  64. init();
  65. scanf("%d", &N);
  66. for(int i = ; i <= N; i++){
  67. scanf("%d", &color[i]);
  68. }
  69. for(int i = ; i < N; i++){
  70. scanf("%d %d", &u[i], &v[i]);
  71. add(u[i], v[i]);
  72. add(v[i], u[i]);
  73. }
  74.  
  75. dfs1(, );
  76. ans = ;
  77. dfs2(, );
  78.  
  79. printf("%d\n", ans);
  80.  
  81. return ;
  82. }

Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) 【DFS】的更多相关文章

  1. Codeforces Round #540 (Div. 3)--1118F1 - Tree Cutting (Easy Version)

    https://codeforces.com/contest/1118/problem/F1 #include<bits/stdc++.h> using namespace std; in ...

  2. Codeforces Round #527 (Div. 3) F. Tree with Maximum Cost 【DFS换根 || 树形dp】

    传送门:http://codeforces.com/contest/1092/problem/F F. Tree with Maximum Cost time limit per test 2 sec ...

  3. Codeforces Round #650 (Div. 3) F1. Flying Sort (Easy Version) (离散化,贪心)

    题意:有一组数,每次操作可以将某个数移到头部或者尾部,问最少操作多少次使得这组数非递减. 题解:先离散化将每个数映射为排序后所对应的位置,然后贪心,求最长连续子序列的长度,那么最少的操作次数一定为\( ...

  4. Codeforces Round #555 (Div. 3) C2. Increasing Subsequence (hard version)【模拟】

    一 题面 C2. Increasing Subsequence (hard version) 二 分析 需要思考清楚再写的一个题目,不能一看题目就上手,容易写错. 分以下几种情况: 1 左右两端数都小 ...

  5. Codeforces Round #533 (Div. 2) C. Ayoub and Lost Array 【dp】

    传送门:http://codeforces.com/contest/1105/problem/C C. Ayoub and Lost Array time limit per test 1 secon ...

  6. Codeforces Round #680 (Div. 2, based on Moscow Team Olympiad)【ABCD】

    比赛链接:https://codeforces.com/contest/1445 A. Array Rearrangment 题意 给定两个大小均为 \(n\) 的升序数组 \(a\) 和 \(b\) ...

  7. Codeforces Round #599 (Div. 2) B1. Character Swap (Easy Version) 水题

    B1. Character Swap (Easy Version) This problem is different from the hard version. In this version U ...

  8. Codeforces Round #263 (Div. 2) A. Appleman and Easy Task【地图型搜索/判断一个点四周‘o’的个数的奇偶】

    A. Appleman and Easy Task time limit per test 1 second memory limit per test 256 megabytes input sta ...

  9. Codeforces Round #599 (Div. 2) B1. Character Swap (Easy Version)

    This problem is different from the hard version. In this version Ujan makes exactly one exchange. Yo ...

随机推荐

  1. Db - DataAccess

    /* Jonney Create 2013-8-12 */ /*using System.Data.OracleClient;*/ /*using System.Data.SQLite;*/ /*us ...

  2. Magento 2中文文档教程 - Magento 2.1.x 系统需求

    Magento 2.1.x 系统需求 操作系统 (Linux x86-64) Linux发行版如红帽企业Linux(RHEL),CentOS,Ubuntu,Debian,等等 内存需求 升级的应用程序 ...

  3. ASP.NET MVC4 新手入门教程之五 ---5.用控制器访问模型数据

    在本节中,将创建一个新的MoviesController类并编写代码来检索电影数据并将其显示在浏览器中使用一个视图模板. 才走出下一步生成应用程序. 用鼠标右键单击控制器文件夹中并创建一个新的 Mov ...

  4. Java--详解WebService技术

    Java--详解WebService技术 一.什么是 webservice WebService是一种跨编程语言和跨操作系统平台的远程调用技术. 所谓跨编程语言和跨操作平台,就是说服务端程序采用jav ...

  5. 使用QQ第三方登录 并在父页面跳转刷新

    <html> <head> <title>QQ登录跳转</title> <script src="http://lib.sinaapp. ...

  6. csharp:DropDownComboxTreeView

    using System; using System.Collections.Generic; using System.Text; using System.Drawing; using Syste ...

  7. JavaScript常用类

    JS常用类 一.Number 1.常用数字 整数:10 小数:3.14 科学计数法:1e5 | 1e-5 正负无穷:Infinity | -Infinity 2.常用进制 二进制:0b1010 八进制 ...

  8. JavaScript 二维数组排列组合

    <html> <head> <title>二维数组排列组合</title> </head> <body> <div id= ...

  9. element中文件上传

    vue+element 文件操作 作者:一粒尘土 时间:2019-3-17 注:以下操作针对 vue-cli 目录 使用 组件常用参数 组件常用方法 上传文件 上传文件格式限制 回显文件 下载文件 删 ...

  10. 合并excel的多个sheet

    '合并excel的多个sheetSub 合并当前工作簿下的所有工作表()Application.ScreenUpdating = FalseFor j = 1 To Sheets.Count If S ...