1759: Interview Program

Time limit: 2000 ms Memory Limit: 131072 KB

[title] Description

year 2044, mankind will enter cosmic era. State L n planets, numbered 1 to n, there is a long ball on each planet.

Because the long-term accumulation of history, on the i-th planet there is a number of respected elders i, because the elderly respected, so the i-th planet long ball must have been under the jurisdiction of the i-elderly people and elderly people governed themselves.

Each of the hands of an elderly person has a list of Bi, the top record number of some of the elderly.

For some reason wonders heavy, but there may be some number from 1 to i-1 in the list and guaranteed not to repeat the bit i elders.

Since the elderly are respected, the i-th bit elderly Territories Sufficient Conditions elderly j-th bit is: for each k belonging Bi, k of the j-th bit Territories elderly people elderly.

At this point, a reporter wanted to interview some of the long ball, in order to ensure a smooth interview, he decided to improve relations with some of the elderly, in order to interview the ball long been under the jurisdiction of the elderly.

In order to more long balls laughing, the reporter will give you a raise m asking.

Reporters asked the i-th number will give you a fi elder, you need to answer how many planets are at least long ball jurisdiction directly or indirectly, an elder. m≤2000000.

[Input]

The first line of a number n, the number of the planet.

Subsequently n rows, each row describes a Bi: Firstly, the size of the Bi szi (possibly zero), the number szi Next, a description of each element Bi. To ensure that the number of Bi is not repeated.

The next line, a given number m, represents the number of inquiry.

Subsequently m rows, each row describes a query: format set for supra format of Bi.

[Output]

a total of m rows, row i i th output answer to the query.

[Enter] Sample

7
0
. 1. 1
. 1. 1
. 1 2
2 2. 3
0
2 2. 6
. 3
2 2. 3
2. 5. 3
2. 4. 5

[] sample output

. 3
. 3
. 4

[Note]

[Sample] explained

for the first query, 2,3 elderly are under the jurisdiction of the elderly No. 1, so a total of three long balls can be interviewed, numbered 1, 2, respectively.

For the second inquiry, the number of elderly people under the jurisdiction of the elderly 3,5 No. 1, so a total of three long balls can be interviewed, numbered 1,3,5 respectively.

For the third inquiry, No. 4 No. 1, 2 elderly elderly jurisdiction, so a total of four long balls can be interviewed, numbered 1,2,4,5, respectively.

Special note: no jurisdiction elderly No. 5 2 elderly, because 3∈B5 2 but not B3. But the elderly elderly 2 4 jurisdiction because the jurisdiction of the elderly themselves. Description: FIG omitted ball long, indicating the number represents the elderly side Bv u → v u in the [data size] and conventions for 30% of data, n, m≤100.
    








To 100% of the data, n, m≤200000, Σ | Bi | ≤2000000, interrogation of Σ | szi | ≤2000000.

 

【answer】

 

The key question is how these achievements, if found to make a point of all his ancestors were under the jurisdiction of the i-th node should be connected to the next node in the list lca it, because of the need to be governed both these points.

 

Side edge achievements multiplication, contribution complexity nlogn.

 

Consider how to answer questions.

 

We found that each inquiry is seeking to give all points to the root and the number of nodes. Consider lca all nodes sorted by dfn, then each node and its neighboring nodes together is the answer.

 

code show as below:

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int n,last[N],size,f[N][20],m,dfn[N],hu[N],dep[N],cnt;
struct pigu
{
    int dao,ne;
}a[N<<1];
inline void lingjiebiao(int x,int y)
{
    a[++size].dao=y;
    a[size].ne=last[x];
    last[x]=size;
}
inline int read()
{
    int x=0,f=1;
    char c=getchar();
    while(!isdigit(c)) {if(c=='-') f=-1;c=getchar();} 
    while(isdigit(c)) {x=(x<<3)+(x<<1)+c-'0';c=getchar();}
    return x*f;
}
inline void lian(int x,int y)
{
    f[x][0]=y;dep[x]=dep[y]+1;lingjiebiao(y,x);
    for(int i=1;f[f[x][i-1]][i-1];i++) f[x][i]=f[f[x][i-1]][i-1];
}
inline int get_lca(int x,int y)
{
    if(dep[x]<dep[y])
        swap(x,y);
    for(int i=19;i>=0;i--)
        if(dep[f[x][i]]>=dep[y]) 
            x=f[x][i];
    if(x==y) return x;
    for(int i=19;i>=0;i--)
        if(f[x][i]!=f[y][i])
            x=f[x][i],y=f[y][i];
    return f[x][0];
}
inline void dfs(int now)
{
    dfn[now]=++cnt;
    for(int i=last[now];i;i=a[i].ne) dfs(a[i].dao);
}
inline bool cmp(int x,int y)
{
    return dfn[x]<dfn[y];
}
int main()
{
    n=read();
    for(int i=1,x;i<=n;i++)
    {
        x=read();
        int lca=0;
        if(x) lca=read();
        for(int j=1,y;j<=x-1;j++)
        {
            y=read();
            lca=get_lca(lca,y);
        }
        lian(i,lca);
    }
    m=read();
    dfs(0);
    for(int i=1,x;i<=m;i++)
    {
        x=read();
        for(int j=1;j<=x;j++) hu[j]=read();
        sort(hu+1,hu+x+1,cmp);
        int ans=dep[hu[1]];
        for(int j=2;j<=x;j++) ans+=dep[hu[j]]-dep[get_lca(hu[j],hu[j-1])];
        cout<<ans<<"\n";
    }
}
View Code

Guess you like

Origin www.cnblogs.com/betablewaloot/p/12207941.html