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的更多相关文章

  1. 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 ...

  2. 用GraphX分析伴生网络(二)

    8. 过滤噪声边 在当前的伴生关系中,边的权重是基于一对概念同时出现在一篇论文中的频率来计算的.这种简单的权重机制的问题在于:它并没有对一对概念同时出现的原因加以区分,有时一对概念同时出现是由于它们具 ...

  3. 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 ...

  4. An interesting experiment on China’s censorship

    This paper presented a very interesting topic. Censorship in China has always drawn people's attenti ...

  5. 2015年辽宁省赛Interesting Tree

    题目描述 Recently, Miss Huang want to receive a Tree as her birthday gift! (What a interesting person!)  ...

  6. HDU5785 Interesting(Manacher + 延迟标记)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5785 Description Alice get a string S. She think ...

  7. ural 2070. Interesting Numbers

    2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...

  8. 多校赛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 ...

  9. hdu Interesting Fibonacci

    Interesting Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

随机推荐

  1. SQL Server 2012启动时提示:无效的许可证数据,需要重新安装

    因为手咸,觉得电脑没有VS 2010版本的软件,就把Microsoft Visual C++ 2010某个组件给卸载了. 然后打开Sql Server 2012,就开始报错. 重装之后,也还是报错,将 ...

  2. Linux系统怎么分区

    linux分区方法,不同的人有不同的方法,反正没有统一的方法.在分区方面,我觉得根据自己的实际情况来分是最好的.玩linux也有好几年了,下面说一下,我在分区方面的一些经验. 一,个人用 如果是个人用 ...

  3. jquery实现弹出层完美居中效果

    代码如下: showDiv($("#pop"));function showDiv(obj){ $(obj).show(); center(obj); $(window).scro ...

  4. nginx配置访问xx.com跳转www.xx.com

    二.在nginx里面配置 rewrite 规则.打开 Nginx.conf 文件找到server配置段:[以下是我的server配置段] 禁止IP地址访问 server{ listen 80 defa ...

  5. 在地址栏里输入一个 URL后,按下 Enter 到这个页面呈现出来,中间会发生什么?

    这是一个面试高频的问题 在输入 URL 后,首先需要找到这个 URL 域名的服务器 IP,为了寻找这个 IP,浏览器首先会寻找缓存,查看缓存中是否有记录,缓存的查找记录为:浏览器缓存 ->系统缓 ...

  6. thrift简单示例 (基于C++)

    这个thrift的简单示例, 来源于官网 (http://thrift.apache.org/tutorial/cpp), 因为我觉得官网的例子已经很简单了, 所以没有写新的示例, 关于安装的教程, ...

  7. Django-模型层(多表操作)

    目录 1.创建模型 1.1方式一:自行创建第三张表 1.2方式二:通过ManyToManyField自动创建第三张表 1.3关于db_column和verbose_name 1.4关于on_delet ...

  8. 如何使用Mojave将APFS卷上的MacOS Catalina Beta安装到双引导

    如果你想与macOS Mojave或macOS High Sierra一起运行MacOS Catalina Beta,你可以通过向Mac添加一个新的APFS卷来实现这一点,如果该Macs硬盘被格式化为 ...

  9. Gerrit代码评审流程

    Gerrit代码评审流程 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.代码评审流程(如下图所示) 第一步:贡献者报建一个提交,并通过之前从gerrit下载的commit-ms ...

  10. Django创建管理员账号

    python manage.py createsuperuser 创建一个管理员账号 输入账号:admin 输入邮箱:123456789@qq.com 输入密码:test123456 二次确认 pyt ...