(HDU 5927)Auxiliary Set 思维题 2016CCPC东北地区大学生程序设计竞赛 - 重现赛

本文主要是介绍(HDU 5927)Auxiliary Set 思维题 2016CCPC东北地区大学生程序设计竞赛 - 重现赛,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Auxiliary Set
Problem Description
Given a rooted tree with n vertices, some of the vertices are important.

An auxiliary set is a set containing vertices satisfying at least one of the two conditions:

∙It is an important vertex
∙It is the least common ancestor of two different important vertices.

You are given a tree with n vertices (1 is the root) and q queries.

Each query is a set of nodes which indicates the unimportant vertices in the tree. Answer the size (i.e. number of vertices) of the auxiliary set for each query.

Input
The first line contains only one integer T (T≤1000), which indicates the number of test cases.

For each test case, the first line contains two integers n (1≤n≤100000), q (0≤q≤100000).

In the following n -1 lines, the i-th line contains two integers ui,vi(1≤ui,vi≤n) indicating there is an edge between uii and vi in the tree.

In the next q lines, the i-th line first comes with an integer mi(1≤mi≤100000) indicating the number of vertices in the query set.Then comes with mi different integers, indicating the nodes in the query set.

It is guaranteed that ∑qi=1mi≤100000.

It is also guaranteed that the number of test cases in which n≥1000 or ∑qi=1mi≥1000 is no more than 10.

Output
For each test case, first output one line “Case #x:”, where x is the case number (starting from 1).

Then q lines follow, i-th line contains an integer indicating the size of the auxiliary set for each query.

Sample Input
1
6 3
6 4
2 5
5 4
1 5
5 3
3 1 2 3
1 5
3 3 1 4

Sample Output
Case #1:
3
6
3

这里写图片描述

题意:
给你一棵树,有n个节点,q次查询,每次查询告诉你m个非重要节点(其余都是重要节点),问你每次查询时Auxiliary Set元素有多少个?
满足一下两点中的一点即可为Auxiliary Set元素:
1:是重要节点
2:是两个重要节点的最近公共祖先

分析:
其实这一题就是问有多少个非重要节点可以是Auxiliary Set元素,即是两个重要节点的最近公共祖先。
首先对树进行一次dfs,求出每个节点的父亲和儿子的个数,以及其深度。
然后对于m个不重要节点,按深度从大到小排序,然后依次判断:对于当前点,
如果它的儿子数量大于等于2,说明他有两个重要节点孩子,那么当前点一定是一个重要节点;
如果儿子数量等于1,那么关系到当前点的祖先节点是不是重要节点,不处理;
如果孩子数等于0,那么当前点肯定不是重要节点,同时其父亲节点的儿子数量减1
只要满足孩子数大于等于2的都是Auxiliary Set元素。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;const int maxn = 100010;
struct egde
{int u,v,next;
}edges[2*maxn];
int head[maxn];
int n,m,q,e;
int par[maxn],son[maxn],tson[maxn],dep[maxn],qs[maxn];
bool vis[maxn];void addedge(int u,int v)
{edges[e].u = u;edges[e].v = v;edges[e].next = head[u];head[u] = e++;
}void dfs(int u,int fa,int d)
{vis[u] = true; par[u] = fa; dep[u] = d;for(int i=head[u];i!=-1;i=edges[i].next){if(!vis[edges[i].v]){dfs(edges[i].v,u,d+1);son[u]++;}}
}int cmp(int a,int b)
{return dep[a] > dep[b];
}int main()
{int t,u,v;int kase=1;scanf("%d",&t);while(t--){scanf("%d%d",&n,&q);e = 0;memset(head,-1,sizeof(head));for(int i=1;i<n;i++){scanf("%d%d",&u,&v);addedge(u,v);addedge(v,u);}memset(vis,false,sizeof(vis));memset(son,0,sizeof(son));dfs(1,-1,1);printf("Case #%d:\n", kase++);while(q--){scanf("%d",&m);for(int i=1;i<=m;i++) scanf("%d",&qs[i]);for(int i=1;i<=m;i++) tson[qs[i]] = son[qs[i]];sort(qs+1,qs+m+1,cmp);int ans = n - m;for(int i=1;i<=m;i++){if(tson[qs[i]] >= 2) ans++;else if(tson[qs[i]] == 0) tson[par[qs[i]]]--;}printf("%d\n",ans);}}return 0;
}

这篇关于(HDU 5927)Auxiliary Set 思维题 2016CCPC东北地区大学生程序设计竞赛 - 重现赛的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/679520

相关文章

c++中的set容器介绍及操作大全

《c++中的set容器介绍及操作大全》:本文主要介绍c++中的set容器介绍及操作大全,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录​​一、核心特性​​️ ​​二、基本操作​​​​1. 初始化与赋值​​​​2. 增删查操作​​​​3. 遍历方

shell中set -u、set -x、set -e的使用

《shell中set-u、set-x、set-e的使用》本文主要介绍了shell中set-u、set-x、set-e的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参... 目录✅ 1. set -u:防止使用未定义变量 作用: 示例:❌ 报错示例输出:✅ 推荐使用场景:✅ 2. se

MySQL中FIND_IN_SET函数与INSTR函数用法解析

《MySQL中FIND_IN_SET函数与INSTR函数用法解析》:本文主要介绍MySQL中FIND_IN_SET函数与INSTR函数用法解析,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一... 目录一、功能定义与语法1、FIND_IN_SET函数2、INSTR函数二、本质区别对比三、实际场景案例分

Nginx指令add_header和proxy_set_header的区别及说明

《Nginx指令add_header和proxy_set_header的区别及说明》:本文主要介绍Nginx指令add_header和proxy_set_header的区别及说明,具有很好的参考价... 目录Nginx指令add_header和proxy_set_header区别如何理解反向代理?proxy

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2093 考试排名(sscanf)

模拟题。 直接从教程里拉解析。 因为表格里的数据格式不统一。有时候有"()",有时候又没有。而它也不会给我们提示。 这种情况下,就只能它它们统一看作字符串来处理了。现在就请出我们的主角sscanf()! sscanf 语法: #include int sscanf( const char *buffer, const char *format, ... ); 函数sscanf()和

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while