国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

HDU 5927 Auxiliary Set

2019-11-08 18:25:51
字體:
供稿:網(wǎng)友

Auxiliary Set

Time Limit: 9000/4500 MS (java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1171    Accepted Submission(s): 366PRoblem DescriptionGiven 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.InputThe 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. OutputFor 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
16 36 42 55 41 55 33 1 2 31 53 3 1 4 Sample Output
Case #1:363HintFor the query {1,2, 3}:?node 4, 5, 6 are important nodes For the query {5}:?node 1,2, 3, 4, 6 are important nodes?node 5 is the lea of node 4 and node 3 For the query {3, 1,4}:? node 2, 5, 6 are important nodes  

題意:樹中有n個(gè)點(diǎn),從1到n依次標(biāo)記,其中1為根。

樹中的點(diǎn)是否為一個(gè)satisfying的點(diǎn),需要滿足以下條件中的一個(gè):

1、該點(diǎn)是一個(gè)important的點(diǎn)

2、該點(diǎn)有多于2個(gè)important點(diǎn)的子孫

問有多少個(gè)satisfying的點(diǎn)。

思路:dfs跑一遍,統(tǒng)計(jì)好每個(gè)節(jié)點(diǎn)的父節(jié)點(diǎn)、孩子數(shù)以及深度,根據(jù)深度對unimportant的節(jié)點(diǎn)排序,從最深層的unimportant節(jié)點(diǎn)遍歷。具體看代碼吧,代碼有注釋,寫的很全了。

#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;#define SIZE  100000+10int T;//測試數(shù)int Case;int n;//節(jié)點(diǎn)數(shù)int q;//詢問數(shù)int u,v;int un;vector<int> vertice[SIZE];//存放有關(guān)聯(lián)的點(diǎn)int father[SIZE];//存放其父節(jié)點(diǎn)int son[SIZE];//存放兒子節(jié)點(diǎn)的個(gè)數(shù)int deep[SIZE];//存放節(jié)點(diǎn)的深度int unver[SIZE];//存放unimportant節(jié)點(diǎn)int unson[SIZE];//存放unimportant節(jié)點(diǎn)的兒子數(shù)void dfs(int ver, int fver, int d){//ver:當(dāng)前節(jié)點(diǎn) fver:其父節(jié)點(diǎn) d:當(dāng)前深度	father[ver] = fver;	deep[ver] = d;	int temp = vertice[ver].size();//與ver節(jié)點(diǎn)有關(guān)的節(jié)點(diǎn)數(shù)	if(ver == 1)//若節(jié)點(diǎn)為1,其沒有父節(jié)點(diǎn),兒子數(shù)即與其有關(guān)的節(jié)點(diǎn)的個(gè)數(shù);		son[ver] = temp;	else//否則減去父節(jié)點(diǎn)		son[ver] = temp - 1;	for(int i=0; i<temp; i++)	{		int ver_1 = vertice[ver][i];		if(ver_1 == fver)			continue;		dfs(ver_1, ver, d+1);	}}bool cmp(int a, int b){	return deep[a] > deep[b];}void Initial(){	for(int i=0; i<=n; i++)		vertice[i].clear();}int main(){	scanf("%d",&T);	Case = 1;	while(T--)	{		printf("Case #%d:/n",Case++);		scanf("%d %d",&n,&q);		Initial();		for(int i=1;i<n;i++)		{			cin>>u>>v;			vertice[u].push_back(v);			vertice[v].push_back(u);		}		dfs(1,0,0);		for(int i=0;i<q;i++)		{			scanf("%d",&un);			for(int j=0; j<un; j++)			{				scanf("%d",&unver[j]);				unson[unver[j]]=son[unver[j]];			}			sort(unver, unver+un, cmp);//按深度對節(jié)點(diǎn)排序			int num = n-un;			for(int j=0; j<un; j++)			{//節(jié)點(diǎn)是從最深處開始遍歷			 //則一個(gè)節(jié)點(diǎn)的孩子一定都是important節(jié)點(diǎn)				if(unson[unver[j]]>=2)					num++;				else if(unson[unver[j]] == 0)				{//該節(jié)點(diǎn)為葉子節(jié)點(diǎn),那么父節(jié)點(diǎn)相當(dāng)于沒有這個(gè)孩子					unson[father[unver[j]]]--;				}			}			printf("%d/n",num);		}	}	return 0;}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 永宁县| 北安市| 东海县| 双柏县| 榆树市| 宜城市| 万载县| 醴陵市| 庆元县| 鞍山市| 黄骅市| 福泉市| 成都市| 会泽县| 仙游县| 峨眉山市| 普兰店市| 裕民县| 泌阳县| 阿巴嘎旗| 蓬安县| 博乐市| 龙岩市| 英山县| 灵台县| 交城县| 丹阳市| 洪江市| 陈巴尔虎旗| 靖州| 华阴市| 上杭县| 河北区| 古浪县| 济宁市| 清徐县| 孝义市| 佛坪县| 阿尔山市| 靖安县| 文安县|