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 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
随机推荐
- [转]struts1.2的action参数配置
转载地址:http://chenfeng0104.iteye.com/blog/796870 <struts-config> <form-beans> ...
- AngularJS控制器
AngularJS 控制器 控制 AngularJS 应用程序的数据,控制器是常规的 JavaScript 对象. 1. angular.module(name, [requires], [confi ...
- No row with the given identifier exists:错误另解
这是一个hibernate常见的问题.搜索出来最多的答案都是如下面这篇文章所述: http://blog.csdn.net/eyejava/article/details/1896492 但我觉得我问 ...
- 关于打印机能PING通但是无法打开\\地址的问题
首先PING地址,看是否能否PING通,如果不通,代表网络不通不能共享, 如果能PING通,但是无法打开\\地址,那么就就检查这几个地方: 1 防火墙是否关闭 2 print splooer 打印 ...
- [原]ComFriendlyWaitForSingleObject
structThreadParam { unsignedint p1;// +00h ebp-24h unsignedint p2;// +04h ebp-20h unsignedint cookie ...
- for循环嵌套的优化
public static void main(String[] args) { int x = 0; for (int i = 0; i < 2; i++) { ...
- 使用Json出现java.lang.NoClassDefFoundError解决方法
前几天在项目使用到Json格式数据,于是把使用Json需要用到的包都引到了工程里面,程序写好后运行时,发现后台报 java.lang.NoClassDefFoundError: net/sf/json ...
- Navigator
Navigator 这是一个简单的例子,用Navigator来跳转页面,页面之间传递参数 (代码是ES6语法写的): import React from 'react'; import { V ...
- alert效果
<!DOCTYPE html> <head> <meta charset="utf-8" /> <title>拉伸效果</ti ...
- Download Oracle Forms 6i
To download Oracle Forms Developer 6i from Oracle click this link http://download.oracle.com/otn/nt/ ...