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. TCP,UDP 通讯的helper类

    使用Tcp通讯,首先要启动tcp服务端监听客户端,客户端发送消息,服务端收到消息 1.服务端代码如下 public class TcpServerTest { public async Task Be ...

  2. mysql 5.7 非正常安装,无法启动 服务没有报告任何错误

    以前,完整安装mysql5.7程序时,由于程序太大,可以将安装缓存目录中的安装文件(较小)复制出来后,留以后使用. mysql--win32.msi 2 mysql-5.7.17-winx64.msi ...

  3. 英伟达 cuda 开发套件下载

    下载地址 https://developer.nvidia.com/cuda-toolkit 安装比较简单,就不多说了.

  4. 对JS事件机制的深入理解

    一.发生一个事件时,事件及事件处理程序会被放入浏览器的事件队列,事件可归为以下几类: 浏览器事件:window.load.document.DomContentLoaded等 网络请求事件:ajax. ...

  5. redux核心知识

    Provider 作用:把父组件传递进来的store对象放入react 上下文中,这样connect组件就可以从上下文中获取到store对象   Connect 作用: 1.从react上下文中取出s ...

  6. Oracle数据库之操作符及函数

    一.操作符: 1.分类: 算术.比较.逻辑.集合.连接: 2.算术操作符: 执行数值计算: -- 工资加1000 from emp; 3.比较操作符: -- 比较运算符(between and包头不包 ...

  7. 记录axios在IOS上不能发送的问题

    最近 遇到 了axios在IOS上无法发送的问题,测试 了两个 苹果 机,IOS10上不能发送,IOS12可以,百度了下,找到了解决方法.记录下吧 首先引入qs,这个安装axios也已经有了吧:然后在 ...

  8. day 07 作业

    猜年龄游戏 ''' 给定年龄,用户可以猜三次年龄 年龄猜对,让用户选择两次奖励 用户选择两次奖励后可以退出 ''' age_count = 0 age = 20 prize_dict = { '0': ...

  9. myEclipse项目部署点击Finish按钮没反应

    -- 问题描述:myEclipse项目部署点击Finish按钮没反应. -- 问题原因:Tomcat没有不熟JDK. -- 解决办法:window->preferences->servic ...

  10. Linux kernel启动选项(参数)(转)

    Linux kernel启动选项(参数)  转载链接https://www.cnblogs.com/linuxbo/p/4286227.html 在Linux中,给kernel传递参数以控制其行为总共 ...