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. Java ClassLoader 学习理解

    /** * <html> * <body> * <P> Copyright 1994 JsonInternational</p> * <p> ...

  2. Java中new和Class.forName的区别

    首先:New = Class.forName("pacage.A").newInstance(); new是关键字,直接创建对象.Class.forName()是一个方法,要求JV ...

  3. HTML实用文本框样式

    输入框景背景透明: <input style="background:transparent;border:1px solid #ffffff"> 鼠标划过输入框,输入 ...

  4. js的一些较为常见的语句算法题

    下面各题解法可能存在一些时间和空间复杂度问题,有些没有做到最优化,还请谅解!!! 1.用for循环实现10的阶乘. //使用for循环方法解答 var num = 10 var sum = 1; va ...

  5. float与position间的区别

    float与position间的区别:    个人理解为:脱离文档流不一定脱离文本流:但脱离文本流,则也脱离文档流.[如有更好的理解还望评论区一起探讨,共同学习进步]一.float 浮动(脱离文档流, ...

  6. React-Native控件的生命周期

    React-Native控件的生命周期

  7. Centos7搭建Harbor私有仓库(二)

    1 说明 前文Centos7搭建Harbor私有仓库(一)中成功搭建了Harbor,但,是以http方式搭建的,这里我们修改为https方式 以下基于镜像CentOS-7-x86_64-Minimal ...

  8. Load Balancing in gRPC

    背景 基于每次调用的负载均衡 需要注意的是,gRPC的负载均衡发生在每次调用时,而不是每次连接时.换句话说,就算所有的请求来自于同一个客户,我们也希望可以将它们负载均衡到所有的服务器. 负载均衡的方法 ...

  9. CentOS安装MySQL8.x

    MySQL的安装 (4,5,6可省略) 声明:CentOS版本为7.6,安装的MySQL版本为8.0.17 1. 首先要卸载掉本机自带的mysql相关,包括MariaDB. rpm -pa | gre ...

  10. [AWS][GUI][VNC]rhel 7 安装GUI ,配置VNC

    预计阅读时间:15分钟 预计配置时间:30分钟  (前提是已经申请AWS的EC2的rhel7 云主机并且成功运行) 目前AWS 亚马逊云免费试用一年,申请一个学习使用 痛点:没有GUI,无法搭建Jen ...