Interesting Vertices
Interesting Vertices(前向星+思维+dfs回溯)
参考博客:https://blog.csdn.net/I_believe_CWJ/article/details/102472012
题目大意:给你一课有n个节点的树,其中有k个节点被染色,求有多少个节点满足自身没有被染色
并且它的每棵子树中都至少有一个节点被染色。
解题思路:dfs回溯类似求树的重心的方式求解,dfsdfs回溯可以得到每个节点的它的子树中是否都有染色的点
并统计所有子树中染色点的个数sum,然后走向父亲的那棵子树中被染色的点数就为k−sum,
这样即可判断此节点是否满足条件。
AC_Code
#include <iostream>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <cmath>
#include <vector>
#include <map>
#include <vector>
#include <algorithm>
#define bug printf("*********\n");
#define mem0(a) memset(a, 0, sizeof(a));
#define mem1(a) memset(a, -1, sizeof(a));
#define ios ios::sync_with_stdio(false);
#define pb(x) push_back(x)
using namespace std;
typedef long long LL;
const LL mod = 1e9 + ;
const int inf = 1e9 + ;
const LL INF = 1e18 + ;
const double eps = acos(-1.0); int n, k, cnt;
int c[];///标记是否是染色点
int head[];///前向星使用
int vis[];///标记是否访问过
int sz[];///sz[i]表示第i个节点的子树中一共有多少个被染色 struct edge {
int to, nxt;
}e[*]; void add(int u, int v) {
e[cnt].to = v;
e[cnt].nxt = head[u];
head[u] = cnt ++;
} vector<int> ans; void dfs(int u) {
vis[u] = ;
sz[u] = c[u];
int flag = ;
for(int i = head[u]; ~i; i = e[i].nxt) {
int en = e[i].to;
if(vis[en]) continue;
dfs(en);
if(!sz[en]) flag = ;///此子树没有被染色
sz[u] += sz[en];
}
///k-sz[u]>0 是指u的父亲节点所带领的那棵子树有被染色
///!c[u]是指满足改点没有被染色
///flag表示点的所有子树都有被染色
///u==1是因为1节点没有父亲节点了
if(!c[u] && flag && (k-sz[u]> || u == )) ans.pb(u);
} int main() {
int x, y;
while(~scanf("%d%d", &n, &k)) {
mem0(vis);mem0(c);mem1(head);
cnt = ;
ans.clear();
for(int i = ; i < k; i ++) {
scanf("%d", &x);
c[x] = ;
}
for(int i = ; i < n-; i ++) {
scanf("%d%d", &x, &y);
add(x, y), add(y, x);
}
dfs();///假装以1为树的总根
sort(ans.begin(), ans.end());
printf("%d\n", ans.size());
for(auto i : ans) {
printf("%d ", i);
}
printf("\n");
}
return ;
}
Interesting Vertices的更多相关文章
- Codeforces Round #143 (Div. 2) E. Cactus 无向图缩环+LCA
E. Cactus A connected undirected graph is called a vertex cactus, if each vertex of this graph bel ...
- 用GraphX分析伴生网络(二)
8. 过滤噪声边 在当前的伴生关系中,边的权重是基于一对概念同时出现在一篇论文中的频率来计算的.这种简单的权重机制的问题在于:它并没有对一对概念同时出现的原因加以区分,有时一对概念同时出现是由于它们具 ...
- Codeforces Beta Round #9 (Div. 2 Only) E. Interesting Graph and Apples 构造题
E. Interesting Graph and Apples 题目连接: http://www.codeforces.com/contest/9/problem/E Description Hexa ...
- An interesting experiment on China’s censorship
This paper presented a very interesting topic. Censorship in China has always drawn people's attenti ...
- 2015年辽宁省赛Interesting Tree
题目描述 Recently, Miss Huang want to receive a Tree as her birthday gift! (What a interesting person!) ...
- HDU5785 Interesting(Manacher + 延迟标记)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5785 Description Alice get a string S. She think ...
- ural 2070. Interesting Numbers
2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...
- 多校赛3- Solve this interesting problem 分类: 比赛 2015-07-29 21:01 8人阅读 评论(0) 收藏
H - Solve this interesting problem Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I ...
- hdu Interesting Fibonacci
Interesting Fibonacci Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
随机推荐
- redis监控工具汇总
redis-stat redis-stat是一个比较有名的redis指标可视化的监控工具,采用ruby开发,基于redis的info命令来统计,不影响redis性能. docker运行 docker ...
- 使用NODEJS实现JSONP的实例
JSONP与JSON只有一字之差,我们在使用Jquery的Ajax调用的时候也是使用相同的方法来调用,两者的区别几乎只在于使用的dataType这个属性的不同.但是实际上JSON和JSONP是完全不同 ...
- linux 压力测试工具之ab
简介 Apache Benchmark(简称ab) 是Apache安装包中自带的压力测试工具 ,简单易用 在此提供 ab 在 centOS7 下的安装和使用方法注:个人发现,之前安装的centos6. ...
- Dockerfile构建jar镜像
dockerDockerfilejar包docker-compose 一.安装docker和compose 二.准备jar包 三.编写配置文件 1. Dockerfile 2. docker-comp ...
- 2019 三七互娱java面试笔试题 (含面试题解析)
本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.三七互娱等公司offer,岗位是Java后端开发,因为发展原因最终选择去了三七互娱,入职一年时间了,也成为了面 ...
- selenium自学笔记---下拉框定位元素select
下拉框1.先定位select 然后在定位option city = driver.find_element_by_id("selCities_0") city.find_eleme ...
- css,对包含有子元素的元素进行flex后,会影响原有的布局。如何后续处理
对包含有子元素的元素进行flex后,会影响原有的布局. 例如设置两个div,第一个div包含一个img(图片),第二个div包含多个p元素(对前面的img的说明).如下图 1:当对着两个两个div进行 ...
- javascript中attribute和property的区别详解
DOM元素的attribute和property很容易混倄在一起,分不清楚,两者是不同的东西,但是两者又联系紧密.很多新手朋友,也包括以前的我,经常会搞不清楚. attribute翻译成中文术语为“特 ...
- rabbitmq:新建用户登陆rabbitmq management失败
参考文档:https://www.rabbitmq.com/management.html#permissions 安装好rabbitmq之后,遇到一个问题,新建的账号无法在rabbitmq的UI界面 ...
- Hybris服务器启动日志分析
build文件检测,使用b2c_acc recipit启动服务器:/home/jerrywang/Hybris/installer/recipes/b2c_acc/build.gradle The T ...