传送门

分析:感觉这道题有点意思。就写一篇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的更多相关文章

  1. HDU 5927 Auxiliary Set 【DFS+树】(2016CCPC东北地区大学生程序设计竞赛)

    Auxiliary Set Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  2. HDU 5927 Auxiliary Set (dfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5927 题意: 给你一棵树,其中有一些'不重要'的点,要是这些'不重要'的点的子树中有两个重要的点的LC ...

  3. hdu 5927 Auxiliary Set 贪心

    Auxiliary Set Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Pr ...

  4. F - Auxiliary Set HDU - 5927 (dfs判断lca)

    题目链接: F - Auxiliary Set HDU - 5927 学习网址:https://blog.csdn.net/yiqzq/article/details/81952369题目大意一棵节点 ...

  5. [hdoj5927][dfs]

    http://acm.hdu.edu.cn/showproblem.php?pid=5927 Auxiliary Set Time Limit: 9000/4500 MS (Java/Others)  ...

  6. HDU 4010 Query on The Trees (动态树)(Link-Cut-Tree)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4010 题意; 先给你一棵树,有 \(4\) 种操作: 1.如果 \(x\) 和 \(y\) 不在同一 ...

  7. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  8. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  9. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

随机推荐

  1. java.sql.SQLException: ORA-00001: 违反唯一约束条件 (SCOTT.SYS_C0011456)

    我tb_user数据库的主键是id,在这个java程序中: String sql="insert into tb_user(id,USER_NAME,USER_PASSWORD) value ...

  2. 以小时候玩的贪吃蛇为例,对于Java图像界面的学习感悟

    简介 正文 01.JFrame是啥? 02.JPanel 03. KeyListener 04.Runnable 05.游戏Running 06.游戏初始类编写 07.main 简介: 一直以来用代码 ...

  3. Windows光标形状

    ::SetCursor( LoadCursor(NULL, IDC_XXX) ); IDC_ARROW (plain) IDC_HELP (arrow + question mark) IDC_APP ...

  4. NOIP2016普及组复赛解题报告

    提高组萌新,DAY1DAY2加起来骗分不到300,写写普及组的题目聊以自慰. (附:洛谷题目链接 T1:https://www.luogu.org/problem/show?pid=1909 T2:h ...

  5. c# winform DataGridView单击选中一整行,只能单选,不能选择多行,只能选择一行

    设置DataGridView的属性SelectionMode为FullRowSelect 这样就使DataGridView不是选择一个字段,而是选择一整行了 设置DataGridView的属性Mult ...

  6. python之路3:

    class set(object): """ set() -> new empty set object set(iterable) -> new set o ...

  7. VirtualBox虚拟机运行Ubuntu如何不卡

    VirtualBox虚拟机运行Ubuntu如何不卡 转自http://www.xuzefeng.com/post/85.html 上一篇文章<VirtualBox虚拟机安装Ubuntu详细教程& ...

  8. html+js 的一些小问题

    html+js 的一些小问题: 选择器+遍历函数: $("#taskStatusList tr:gt(0)").find("td:eq(3)").find(&q ...

  9. JSON生成c#类代码小工具

    JSON生成c#类代码小工具 为什么写这么个玩意 最近的项目中需要和一个服务端程序通讯,而通讯的协议是基于流行的json,由于是.net,所以很简单的从公司代码库里找到了Newtonsoft.dll( ...

  10. ListView下拉加载一(分页)

    首先创建在主xml里放置一个listview列表,代码如下: <LinearLayout xmlns:android="http://schemas.android.com/apk/r ...