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 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
随机推荐
- HTML5的Video标签的属性,方法和事件汇总
<video>标签的属性 src :视频的属性 poster:视频封面,没有播放时显示的图片 preload:预加载 autoplay:自动播放 loop:循环播放 controls:浏览 ...
- c#DataGridView数据绑定示例——格式化单元格的内容(转)
转自http://blog.csdn.net/testcs_dn/article/details/37834063 c#DataGridView数据绑定示例 格式化单元格的内容 在使用DataGrid ...
- 小结一下前段时间做的rpgdemo
虽然说已经是彻底放弃继续做那个demo了(代码结构混乱,想增加新功能非常的不方便),不过还是花了一点心血在里面的,毕竟这是我开始学习unity游戏制作的初衷,不过果然是学的越多越发现自己的不足... ...
- Program.cs
Program.cs using System; namespace HelloWorld { class Program { [STAThread] static void Main(string[ ...
- 在Ubuntu14.04安装torch7笔记
http://www.linuxdiyf.com/linux/18979.html 附编译例子: https://github.com/vic-w/torch-practice
- jQuery 弹出层脚本
直接贴代码: $.extend({ closeBox: function (o, x, fn) { $('#' + o).fadeOut(500, function () { if (fn & ...
- .NET蓝牙开源库:32feet.NET
在用C#调用蓝牙编程一文中我留个小悬念就是:InTheHand.Net.Personal.dll是怎么来的?这篇文章来解答这个问题,InTheHand.Net.Personal.dll就是来源于今天要 ...
- jqury 右击事件插件
在有些时候,网页中需要给一些标签对象加入右击的事件,在网上看了一些小的插件,但是不能根据this获取到当前的标签.所以相对他们进行改进一下.自己写了一个小的js右击事件.废话不多说了,看代码. $(f ...
- c# winform DataGridView单击选中一整行,只能单选,不能选择多行,只能选择一行
设置DataGridView的属性SelectionMode为FullRowSelect 这样就使DataGridView不是选择一个字段,而是选择一整行了 设置DataGridView的属性Mult ...
- C#返回时间格式转换成 js 字符串
在.net 中,调用 post 或者 get和后台通信时,如果有时间返回信息,后台返回的时间信息一般是这样格式:Thu Jul 9 23:14:53 UTC+0800 2015,那么要在前台显示就会有 ...