hdu 5927 Auxiliary Set
分析:感觉这道题有点意思。就写一篇mark一下吧。
现场比赛的时候去枚举了儿子用了线段树+dfs序,和预想的一样T了。
可以换一个想法,从儿子对父亲的贡献来思考。
在unimportant点中先假设一个节点的每一个儿子对父亲节点都有important的贡献,再考虑每个儿子
如果当前儿子有两个及以上的important点,那么这个点也在集合中,计数加一;如果当前儿子有一个important节点,那么虽然它不在集合中,但还是对它的父亲有important的贡献;如果当前儿子没有important节点,那么它既不在集合中,又对父亲没有贡献,它父亲中的贡献需要减一(之前假设了每个儿子对父亲都有贡献)。于是只要对所有的unimportant点按照深度大小排序,深度大的排在前面,逐一筛选就行了。深度和儿子数都可以在建好树的时候一边dfs就可以求得,而important标记在每次询问的时候都要更新。复杂度O(N+Mlog(M))
代码:
/*****************************************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <map>
#include <set>
#include <ctime>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define offcin ios::sync_with_stdio(false)
#define sigma_size 26
#define lson l,m,v<<1
#define rson m+1,r,v<<1|1
#define slch v<<1
#define srch v<<1|1
#define sgetmid int m = (l+r)>>1
#define LL long long
#define ull unsigned long long
#define mem(x,v) memset(x,v,sizeof(x))
#define lowbit(x) (x&-x)
#define bits(a) __builtin_popcount(a)
#define mk make_pair
#define pb push_back
#define fi first
#define se second
const int INF = 0x3f3f3f3f;
const LL INFF = 1e18;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-9;
const LL mod = 1e9+7;
const int maxmat = 10;
const ull BASE = 31;
/*****************************************************/
const int maxn = 1e5 + 5;
std::vector<int> G[maxn];
int son[maxn], dep[maxn], fat[maxn];
int tson[maxn];
int N, M;
struct Node {
int id, dep;
bool operator <(const Node &rhs) const {
return dep > rhs.dep;
}
};
void dfs(int u, int fa, int d) {
dep[u] = d; fat[u] = fa;
for (unsigned i = 0; i < G[u].size(); i ++) {
int v = G[u][i];
if (v == fa) continue;
son[u] ++;
dfs(v, u, d + 1);
}
}
int main(int argc, char const *argv[]) {
int T;
cin>>T;
for (int kase = 1; kase <= T; kase ++) {
printf("Case #%d:\n", kase);
scanf("%d%d", &N, &M);
mem(son, 0);
for (int i = 1; i <= N; i ++) G[i].clear();
for (int i = 0; i < N - 1; i ++) {
int u, v; scanf("%d%d", &u, &v);
G[u].pb(v); G[v].pb(u);
}
dfs(1, -1, 1);
while (M --) {
int n; scanf("%d", &n);
int ans = N - n;
std::vector<Node> tmp;
for (int i = 0; i < n; i ++) {
int k; scanf("%d", &k);
tson[k] = son[k];
tmp.pb((Node){k, dep[k]});
}
sort(tmp.begin(), tmp.end());
for (unsigned i = 0; i < tmp.size(); i ++) {
int id = tmp[i].id;
if (tson[id] >= 2) ans ++;
else if (tson[id] == 0) tson[fat[id]] --;
}
printf("%d\n", ans);
}
}
return 0;
}
hdu 5927 Auxiliary Set的更多相关文章
- HDU 5927 Auxiliary Set 【DFS+树】(2016CCPC东北地区大学生程序设计竞赛)
Auxiliary Set Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- HDU 5927 Auxiliary Set (dfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5927 题意: 给你一棵树,其中有一些'不重要'的点,要是这些'不重要'的点的子树中有两个重要的点的LC ...
- hdu 5927 Auxiliary Set 贪心
Auxiliary Set Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Pr ...
- F - Auxiliary Set HDU - 5927 (dfs判断lca)
题目链接: F - Auxiliary Set HDU - 5927 学习网址:https://blog.csdn.net/yiqzq/article/details/81952369题目大意一棵节点 ...
- [hdoj5927][dfs]
http://acm.hdu.edu.cn/showproblem.php?pid=5927 Auxiliary Set Time Limit: 9000/4500 MS (Java/Others) ...
- HDU 4010 Query on The Trees (动态树)(Link-Cut-Tree)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4010 题意; 先给你一棵树,有 \(4\) 种操作: 1.如果 \(x\) 和 \(y\) 不在同一 ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
随机推荐
- CAS示例环境部署及配置
http://wenku.baidu.com/link?url=d6JjWqOtuUediSkV18XagtVG9lVC2hTiaIEimfIgv1PIW8RMA1sXeIqqtJkW90lleYPQ ...
- Dom编程
Dom编程 Dom是一种用于HTML和XML文档的编程接口,是HTML页面的模型,将每个标签都做为一个对象,JavaScript通过调用DOM中的属性.方法就可以对网页中的文本框.层等元素进行编程控制 ...
- IOS 截取图片 部分 并生成新图片
/** * 从图片中按指定的位置大小截取图片的一部分 * * @param image UIImage image 原始的图片 * @param rect CGRect rect 要截取的区域 * * ...
- 判断checkbox是否选中
一种是通过jquery A. $("[name='selectUserId']:checked").each(function () { // $(this).attr(" ...
- 2016ACM/ICPC亚洲区沈阳站-重现赛
C.Recursive sequence 求ans(x),ans(1)=a,ans(2)=b,ans(n)=ans(n-2)*2+ans(n-1)+n^4 如果直接就去解...很难,毕竟不是那种可以直 ...
- install intel c/c++ compiler
通过在Intel官网上申请试用版本Intel® Parallel Studio XE Cluster Edition for Linux,会让你提交邮箱等信息,完成后会很快回复邮件,邮件会给出下载地址 ...
- CentOS 7.2.1511编译安装Nginx1.10.1+MySQL5.7.14+PHP7.0.11
准备篇 一.防火墙配置 CentOS 7.x默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl stop firewalld.se ...
- 用户识别APP项目开发计划书
用户识别APP项目开发计划书 项目介绍: 用户识别APP,通过在有限时间内引导用户A交互,提取用户的行为特征,然后将APP交给用户X(可能是A也可能是陌生人),在1分钟内引导X交互,判断 ...
- UIScrollView(滚动视图)
(1)常用属性: 1)@property(nonatomic)CGPointcontentOffset; 这个属性⽤用来表⽰示UIScrollView滚动的位置 2)@property(nonatom ...
- Ember.js入门教程、博文汇总
第一章 对象模型 Ember.js 入门指南——类的定义.初始化.继承 Ember.js 入门指南——类的扩展(reopen) Ember.js 入门指南——计算属性(compute properti ...