当前位置: 首页 > news >正文

Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) 【DFS】

任意门: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 (2n31052≤n≤3⋅105) — the number of vertices in the tree.

The second line contains nn integers a1,a2,,ana1,a2,…,an (0ai20≤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 n1n−1 lines contains two integers vivi and uiui (1vi,uin1≤vi,ui≤n, viuivi≠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
5
2 0 0 1 2
1 2
2 3
2 4
2 5
output
Copy
1
input
Copy
5
1 0 0 0 2
1 2
2 3
3 4
4 5
output
Copy
4
input
Copy
3
1 1 2
2 3
1 3
output
Copy
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+10;
 6 int color[MAXN];
 7 int u[MAXN], v[MAXN];
 8 int dd[MAXN][3];
 9 bool vis[MAXN];
10 int N, ans;
11 struct Edge
12 {
13     int v, nxt;
14 }edge[MAXN<<1];
15 int head[MAXN], cnt;
16 
17 void init()
18 {
19     memset(head, -1, sizeof(head));
20     cnt = 0;
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] == 1) dd[x][1]+=1;
34     else if(color[x] == 2) dd[x][2]+=1;
35     for(int i = head[x]; i != -1; i = edge[i].nxt){
36         v = edge[i].v;
37         if(v == x || v == fa) continue;
38         dfs1(v, x);
39         dd[x][1]+=dd[v][1];
40         dd[x][2]+=dd[v][2];
41     }
42 }
43 
44 
45 void dfs2(int x, int fa)
46 {
47     int v;
48     for(int i = head[x]; i != -1; i = edge[i].nxt){
49         v = edge[i].v;
50         if(v == fa) continue;
51         if(dd[v][1] == 0 && (dd[1][2]-dd[v][2]) == 0){
52 //            printf("u:%d v:%d\n", x, v);
53             ans++;
54         }
55         else if(dd[v][2] == 0 && (dd[1][1]-dd[v][1]) == 0){
56 //            printf("u:%d v:%d\n", x, v);
57             ans++;
58         }
59         dfs2(v, x);
60     }
61 }
62 
63 int main()
64 {
65     init();
66     scanf("%d", &N);
67     for(int i = 1; i <= N; i++){
68         scanf("%d", &color[i]);
69     }
70     for(int i = 1; i < N; i++){
71         scanf("%d %d", &u[i], &v[i]);
72         add(u[i], v[i]);
73         add(v[i], u[i]);
74     }
75 
76     dfs1(1, 0);
77     ans = 0;
78     dfs2(1, 0);
79 
80     printf("%d\n", ans);
81 
82     return 0;
83 }
View Code

 

转载于:https://www.cnblogs.com/ymzjj/p/10409894.html

相关文章:

  • volatilesynchronizeddiff
  • canvas字体样式
  • 5-发音规则(略读)
  • [洛谷P1709] [USACO5.5]隐藏口令Hidden Password
  • 树·二叉查找树ADT(二叉搜索树/排序树)
  • 两种经典电商CSS布局
  • 微信小程序 - 自定义swiper dots样式(非组件)
  • django基础 第四章 模板标签
  • C++重载赋值运算符
  • golang学习之interface与其它类型转换
  • windows系统和IE的兼容性问题
  • PHP TP5 文章评论+积分+签到
  • YAML 基础
  • Cordova 打包签名
  • JMeter学习(四)参数化
  • hexo+github搭建个人博客
  • 自己简单写的 事件订阅机制
  • Android开发 - 掌握ConstraintLayout(四)创建基本约束
  • Bootstrap JS插件Alert源码分析
  • HashMap ConcurrentHashMap
  • Javascript设计模式学习之Observer(观察者)模式
  • jdbc就是这么简单
  • JS变量作用域
  • JS专题之继承
  • Octave 入门
  • Ruby 2.x 源代码分析:扩展 概述
  • Stream流与Lambda表达式(三) 静态工厂类Collectors
  • 大型网站性能监测、分析与优化常见问题QA
  • 得到一个数组中任意X个元素的所有组合 即C(n,m)
  • 基于游标的分页接口实现
  • 悄悄地说一个bug
  • 深度学习入门:10门免费线上课程推荐
  • 思考 CSS 架构
  • !! 2.对十份论文和报告中的关于OpenCV和Android NDK开发的总结
  • # Swust 12th acm 邀请赛# [ K ] 三角形判定 [题解]
  • # 透过事物看本质的能力怎么培养?
  • #pragma once
  • #使用清华镜像源 安装/更新 指定版本tensorflow
  • (11)MSP430F5529 定时器B
  • (17)Hive ——MR任务的map与reduce个数由什么决定?
  • (react踩过的坑)Antd Select(设置了labelInValue)在FormItem中initialValue的问题
  • (附源码)spring boot校园拼车微信小程序 毕业设计 091617
  • (五) 一起学 Unix 环境高级编程 (APUE) 之 进程环境
  • (五)IO流之ByteArrayInput/OutputStream
  • (转)JVM内存分配 -Xms128m -Xmx512m -XX:PermSize=128m -XX:MaxPermSize=512m
  • (转载)PyTorch代码规范最佳实践和样式指南
  • .bat批处理(十):从路径字符串中截取盘符、文件名、后缀名等信息
  • .cfg\.dat\.mak(持续补充)
  • .NET Core中Emit的使用
  • .net oracle 连接超时_Mysql连接数据库异常汇总【必收藏】
  • .Net Remoting常用部署结构
  • .NET 使用 ILMerge 合并多个程序集,避免引入额外的依赖
  • .NET国产化改造探索(三)、银河麒麟安装.NET 8环境
  • /proc/vmstat 详解
  • [ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname