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. 视频推流模式HLS,HTTP,RTSP,RTMP协议的区别

    HTTP: 先通过服务器将FLV下载到本地缓存,然后再通过NetConnection的本地连接来播放这个FLV,这种方法是播放本地的视频,并不是播放服务器的视频.因此在本地缓存里可以找到这个FLV.其 ...

  2. Java visualvm

    简介 VisualVM是一个集成多个JDK命令行工具的可视化工具.可以作为Java应用程序性能分析和运行监控的工具.开发人员可以利用它来监控.分 析线程信息,浏览内存堆数据.系统管理员可以利用它来监测 ...

  3. python 工厂方法

    工厂方法模式(FACTORY METHOD)是一种常用创建型设计模式,此模式的核心精神是封装类中变化的部分,提取其中个性化善变的部分为独立类, 通过依赖注入以达到解耦.复用和方便后期维护拓展的目的. ...

  4. 2.熟悉LINUX的基本操作

    cd命令:切换目录 (1)切换到目录 /usr/local cd /usr/local (2)去到目前的上层目录 cd .. (3)回到自己的主文件夹 cd ~ ls命令:查看文件与目录 (4)查看目 ...

  5. jQuery判断当前页面是APP内打开还是浏览器打开

    一.代码如下: function check_useragent() { var browser = { versions: function() { var u = navigator.userAg ...

  6. java实现在线预览--poi实现word、excel、ppt转html

    java实现在线预览 - -之poi实现word.excel.ppt转html 简介 java实现在线预览功能是一个大家在工作中也许会遇到的需求,如果公司有钱,直接使用付费的第三方软件或者云在线预览服 ...

  7. Solr字段类型

    一.一般属性 1.name fieldType的名称.该值用于字段定义中的类型属性,强烈建议名称仅包含字母数字和下划线,不能以数字开头[非强制]. 2.class 用于存储和索引此类型数据的类名.可以 ...

  8. 为什么在定义hashcode时要使用31这个数呢?

    散列计算就是计算元素应该放在数组的哪个元素里.准确的说是放到哪个链表里面.按照Java的规则,如果你要想将一个对象放入HashMap中,你的对象的类必须提供hashcode方法,返回一个整数值. ht ...

  9. Ubuntu16.04 网络配置

    Ubuntu 网络配置 安装Ubuntu操作系统之后,为了通过Xshell连接主机,或者连接其他主机.需要进行如下网络配置和ssh服务配置. 1 网络配置 1.1 修改网络配置信息 sudo vi / ...

  10. docker启动报错解决及分析(Cannot create container for service *******: cannot mount volume over existing file, file exists /var/lib/docker/overlay2/)

    现象: Cannot create container for service *******: cannot mount volume over existing file, file exists ...