hdu 5927 Auxiliary Set 贪心

Auxiliary Set

Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

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

Hint

For 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

Source

2016CCPC东北地区大学生程序设计竞赛 - 重现赛

思路:记录儿子个数,子树的全删掉就是删掉一儿子;

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=2e5+10,M=4e6+10,inf=1e9+10,mod=1e9+7;
const ll INF=1e18+10;
vector<int>v[N];
int si[N],fa[N],deep[N],a[N],change[N];
int n,m,q;
void init()
{
    for(int i=0;i<=n;i++)
    {
        change[i]=0;
        fa[i]=0;
        si[i]=0;
        deep[i]=0;
        v[i].clear();
    }
}
void dfs(int u,int pre,int dep)
{
    deep[u]=dep;
    for(int i=0;i<v[u].size();i++)
    {
        if(v[u][i]==pre)continue;
        si[u]++;dfs(v[u][i],u,dep+1);
        fa[v[u][i]]=u;
    }
}
int cmp(int a,int b)
{
    return deep[a]>deep[b];
}
int main()
{
    int T,cas=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&q);
        init();
        for(int i=1;i<n;i++)
        {
            int u,w;
            scanf("%d%d",&u,&w);
            v[u].push_back(w);
            v[w].push_back(u);
        }
        dfs(1,0,1);
        printf("Case #%d:\n",cas++);
        while(q--)
        {
            int ans=0;
            scanf("%d",&m);
            for(int i=1;i<=m;i++)
                scanf("%d",&a[i]);
            sort(a+1,a+1+m,cmp);
            for(int i=1;i<=m;i++)
            {
                int FA=fa[a[i]];
                if(si[a[i]]==0)
                {
                    si[FA]--;
                    change[FA]++;
                }
                if(si[a[i]]>=2)
                {
                    ans++;
                }
            }
            printf("%d\n",n-m+ans);
            for(int i=1;i<=m;i++)
            {
                int FA=fa[a[i]];
                if(change[FA])
                {
                    si[FA]+=change[FA];
                    change[FA]=0;
                }
            }
        }
    }
    return 0;
}
时间: 2024-11-02 23:26:53

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)Total Submission(s): 873    Accepted Submission(s): 271 Problem Description Given a rooted tree with n vertices, some of the vertices are important. An a

HDU 5927 Auxiliary Set (dfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5927 题意: 给你一棵树,其中有一些’不重要‘的点,要是这些’不重要‘的点的子树中有两个重要的点的LCA是这个点,那么这个点就是重要的点.每次询问 问你重要的点有多少? 思路: 用不重要的点建图,要是这个不重要点上的相邻子树至少有两个有重要的点不在同一子树上,那么这个不重要的点才变成重要的点. 1 #include <iostream> 2 #include <cstdio> 3 #i

hdu 4296 Buildings(贪心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4296 Buildings Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1822    Accepted Submission(s): 722 Problem Description Have you ever heard the sto

hdu 4864 Task (贪心)

# include <stdio.h> # include <algorithm> # include <string.h> using namespace std; struct node { int t; int v; int yy; }; struct node a[100010],b[100010]; bool cmp(node a1,node a2) { if(a1.t==a2.t)//先按时间从大到小 return a1.v>a2.v;//再按水平从大

HDU 5014 Number Sequence 贪心 2014 ACM/ICPC Asia Regional Xi&#39;an Online

尽可能凑2^x-1 #include <cstdio> #include <cstring> const int N = 100005; int a[N], p[N]; int init(int x) { int cnt = 0; while(x > 1) { x /= 2; cnt ++; } return cnt + 1; } int main() { int n; while(~scanf("%d", &n)){ for(int i = 0;

HDU 4903 (模拟+贪心)

Fighting the Landlords Problem Description Fighting the Landlords is a card game which has been a heat for years in China. The game goes with the 54 poker cards for 3 players, where the “Landlord” has 20 cards and the other two (the “Farmers”) have 1

HDU 1045 Fire Net 贪心

Problem Description Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall. A blockhouse is a small castle that has four openings through wh

HDU 1338 Game Prediction 贪心

Problem Description Suppose there are M people, including you, playing a special card game. At the beginning, each player receives N cards. The pip of a card is a positive integer which is at most N*M. And there are no two cards with the same pip. Du

hdu 1009 FatMouse&#39; Trade(贪心)

题目来源:hdu 1009 FatMouse' Trade FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 54581 Accepted Submission(s): 18299 Problem Description FatMouse prepared M pounds of cat food, ready