ACM Hong Kong online game b. Boxes

Source: Internet
Author: User

Original title URL: https://open.kattis.com/problems/boxes

Boxes

There is N boxes, indexed by a number from 1 to N. Each box may (or is not) is put into the other boxes. These boxes together form a tree structure (or a forest structure, to be precise).

Answer a series of queries of the following form:given a list of indices of the boxes, find the total number of boxes that the list of boxes actually contain.

Consider, for example, the following five boxes.

Figure 1: Sample input

    • If the query is the list "1" and then the correct answer are "5", because box 1 contains all boxes.
    • If the query is the list ' 4 5 ', then the correct answer is ' 2 ', for boxes 4 and 5 contain themselves and nothing else.
    • If the query is the list "3 4" and then the correct answer is "2".
    • If the query is the list "2 3 4" and then the correct answer is "4", since box 2 also contains box 5.
    • If the query is the list "2", then the correct answer is "3", because box 2 contains itself and both other boxes.

Input

The first line contains the integer n (1≤n≤200000), the number of boxes.

The second line contains N

Integers. The ith integer is either the index of the box which contains the ith box, or zero if the ith b Ox isn't contained in any other box.

The third line contains an integer q(1≤q≤100000), the number of queries. The following Q lines'll has the following format:on each line, the first integer m (1≤m≤20 ) is the length of the list of boxes in this query, then M integers follow, representing the indices of the boxes .

Output

For each query, output a line which contains an integer representing the total number of boxes.

Sample Input 1               

   Sample Output 1

5

0 1 1) 2 2

5

1 1

2 4 5

2 3 4

3 2 3 4

1 2

5

2

2

4

3

Author (s): Chan Pak Hay

Source: Hong Kong Regional Online Preliminary 2016

Test instructions: N boxes, some of which are packed in other ones. Q Times asked, each time a given M-box, which asked this m-box contained altogether several boxes (including the M-box itself).

, if the first box is packed in box J, J is the parent node of I.

Box number 1~n, the box is a tree or a forest structure, determine a root node 0, so that the forest composed of boxes to synthesize a tree root node 0.

Starting from root node 0, Dfs traverses the entire tree, recording the sequential number of Dfs to each node dfn[i].

Tot[i] Indicates the total number of boxes contained in box I (including box I itself).

The collection of boxes contained in each box is sequential in the order of the DFS traversal.

Range[i] denotes the DFN number range of the box contained in box I, i.e. all chests DFN numbered Range[i].first~range[i].second are included in box I.

For example, a sample Build DFS Traversal :

Among them, range[1].first=1,range[1].second=5; DFN numbered chests are included in box 1.

range[2].first=2,range[2].second=4; The boxes numbered DFN are included in box 4.

range[3].first=5,range[3].second=5; The box with the DFN number 5 is included in Box 3.

range[4].first=3,range[4].second=3; The box with the DFN number 3 is included in box 4.

range[5].first=4,range[5].second=4; The box with the DFN number 4 is included in box 5.

Get the total number of boxes containing each box tot[i], and each box I contain the box number range Range[i],

For each set of queries Q, the violent enumeration once each box Q[i] is contained by another box, if Q[i] is included, delete Q[i], and finally the tot sum of the nodes not deleted.

#include <algorithm>#include<cstring>#include<string.h>#include<iostream>#include<list>#include<map>#include<Set>#include<stack>#include<string>#include<utility>#include<vector>#include<cstdio>#include<cmath>#defineLL Long Long#defineN 200005#defineINF 0X3FFFFFFusing namespacestd;intN;intBelong[n];//belong[i] means box I in box elong[i] insideintQnum;//Number of Queriesintm;intq[ -];vector<int>Vec[n];intDfn[n];//ordinal number of Dfs to each node Dfn[i]pair<int,int>range[N];//Range[i] Indicates the DFN number range of the box contained in box IintTot[n];//Tot[i] Indicates the total number of boxes contained in box I (including box I itselfintPos;voidDfsintU//DFS Traversal tree{Dfn[u]=pos++; Range[u].first=POS; Tot[u]=1;  for(intI=0; I<vec[u].size (); i++){        intv=Vec[u][i];        DFS (v); Tot[u]+=Tot[v]; } Range[u].second=POS; return;}intMain () { while(SCANF ("%d", &n)! =EOF) {POS=0; memset (Tot,0,sizeof(tot));  for(intI=1; i<=n;i++) vec[i].clear ();  for(intI=1; i<=n;i++) {scanf ("%d",&Belong[i]);    Vec[belong[i]].push_back (i); } DFS (0);//traversing from root node 0/*for (int i=1;i<=n;i++) {cout<<tot[i]<<endl;            cout<<dfn[i]<<endl;        cout<<range[i].first<< ' <<range[i].second<<endl; }  */scanf ("%d",&qnum);  while(qnum--) {scanf ("%d",&m);  for(intI=0; i<m;i++) {scanf ("%d", &q[i]);//q Array for the query of the box collection        }        intflag=0;//use flag to record which boxes have been deleted, and if (flag& (1<<i)), the box Q[i] is deleted.          for(intI=0; i<m;i++)//violence enumerate each box q[i] to see if it is contained in another box        {            if(! (flag& (1<<i )) { for(intj=i+1; j<m;j++)                    {                        if(! (flag& (1<<J )) {if(Range[q[i]].first<=range[q[j]].first && Range[q[i]].second>=range[q[j]].second) {//Box Q[j] included in box Q[i]Flag|= (1<<j); }                            Else if(Range[q[j]].first<=range[q[i]].first && Range[q[j]].second>=range[q[i]].second) {//Box Q[i] included in box Q[j]Flag|= (1<<i);  Break; }                        }                    }                }        }        intret=0;  for(intI=0; i<m;i++)        {            if(! (flag& (1<<i)) {//Box Q[i] not covered by other boxesret+=Tot[q[i]]; }} printf ("%d\n", ret); }   }    return 0;}

ACM Hong Kong online game b. Boxes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.