URAL1329 Galactic History(深搜DFS)

来源:互联网 发布:sqlserver列明别名 编辑:程序博客网 时间:2024/06/06 00:42

题目:

1329. Galactic History

Time limit: 1.0 second
Memory limit: 64 MB
It is very hard for one person to learn all galactic history. But, on the other hand, every diplomat who wants to hold a more important post in a galactic empire must know the subject well. For example, letting a spoon fall among high-rankers of the star system Arcturus means offending them awfully. (Didn’t you hear that the last conflict between systems Arcturus and Alpha flamed up because of the only shattered glass?)
Fortunately, the solution was found in the Galactic Academy. For diplomats of the lower rank it is enough to learn just a single branch of history – the one that concerns only the cluster of star systems, in which he is going to work. (Diplomats of the lower rank negotiate only with planets that are located in one star cluster. How come we didn’t guess this earlier?)
Taking this very important observation into consideration, it was decided to replace a single intergalactic course with several separate courses, each covering only the part of history that refers to only one star cluster. Of course, it is necessary to learn history in chronological order, beginning from the origin of humanity. That’s why the history of the Earth needs to be included in all collections of separate histories. Then things become complicated: for example, emigrants from Centaurus system colonized the star system of Herdsman, so the textbook on the history of Herdsman system has to contain the early history of Centaurus system. In order to decide, in which textbooks which phases of history should be included, historians of Galactic Academy divided general intergalactic history into many small milestones. Then all milestones were combined into one big tree (omnipresent biologists helped historians in this work, as they had always been using these trees). The milestone referring to early history of the Earth (before the space colonization) was declared the root. Milestones referring to history of star systems close to solar system appear to be its sons (because these systems were colonized by emigrants from Earth) and so on. That’s all! To determine milestones that have to be included in a particular textbook it is only required to determine quickly, whether the milestone A is located in a subtree with the root in milestone B.

Input

In the first line there is a number N (N ≤ 40000), which defines the total number of milestones. In the next N lines there are descriptions of each milestone.
Each milestone is defined by two numbers: ID – an unique numerical identifier of a milestone and ParentID – identifier of the milestone which is its father in a tree. ParentID for the root equals to -1.
(N+2)th line contains number L (L ≤ 40000) – amount of queries. The next L lines contain descriptions of queries: on each line there are two different numbers A and B. All identifiers lie between 0 and 40000.

Output

For each query it is necessary to write in separate line:
  • 1, if milestone A is a root of subtree which contains milesone B.
  • 2, if milestone B is a root of subtree which contains milesone A.
  • 0, if no one of the first two conditions is true.

Sample

inputoutput
10234 -112 23413 23414 23415 23416 23417 23418 23419 234233 195234 233233 12233 13233 15233 19
10002
Problem Author: Evgeny Krokhalev
Problem Source: The 10th Collegiate Programming Contest of the High School Pupils of the Sverdlovsk Region (October 16, 2004)
Tags: graph theory  (
hide tags for unsolved problems
)
思路:

这道题首先给你了n,然后接下来n行,每行有两个数,分别代表儿子节点和它的父亲节点,如果哪一个的父亲节点是-1,那么他就是根,然后接下来有L组询问,问前一个点和后一个点有什么关系,如果没有关系输出0,如果前面的这个点辈分大于后面的点输出1,小于就输出2.

这里我们用深搜的思想来做,我们用一个区间来标记每一个点以及它的儿子的区间范围,比如第一个样例234这个点它的开始区间是0,结束区间是9,那么范围就是(0,9).

l[12]=1,r[12]=1,num=2
l[13]=2,r[13]=2,num=3
l[14]=3,r[14]=3,num=4
l[15]=4,r[15]=4,num=5
l[16]=5,r[16]=5,num=6
l[17]=6,r[17]=6,num=7
l[18]=7,r[18]=7,num=8
l[233]=9,r[233]=9,num=10
l[19]=8,r[19]=9,num=10
l[234]=0,r[234]=9,num=10

以上是他们的区间范围,然后我们只需要判断l[a]<=l[b]&&r[a]>=r[b],那么就证明前面点的辈分大所以输出1,l[a]>=l[b]&&r[a]<=r[b]这种情况输出2,其他情况输出0,,具体看代码


代码:

#include<stdio.h>#include<iostream>#include<string.h>#include<stdlib.h>#include<stack>#include<map>#include<vector>#include<set>#include<queue>#include<math.h>#include<ctype.h>#define mem(a,b) memset(a,b,sizeof(a))#include<algorithm>using namespace std;const int N=40006;typedef long long LL;int l[N],r[N],vis[N],num;vector<int>v[N];void dfs(int x){    vis[x]=1;    l[x]=num++;    int len=v[x].size();    for(int i=0; i<len; i++)    {        int k=v[x][i];        if(!vis[k])            dfs(k);    }    r[x]=num-1;   // printf("l[%d]=%d,r[%d]=%d,num=%d\n",x,l[x],x,r[x],num);}int main(){    int n,q,a,b,rank1;    scanf("%d",&n);    for(int i=1; i<=n; i++)    {        scanf("%d%d",&a,&b);        if(b==-1)            rank1=a;        else            v[b].push_back(a);    }    num=0;    mem(l,0);    mem(r,0);    dfs(rank1);    scanf("%d",&q);    for(int i=1; i<=q; i++)    {        scanf("%d%d",&a,&b);        if(l[a]<=l[b]&&r[a]>=r[b])            puts("1");        else if(l[a]>=l[b]&&r[a]<=r[b])            puts("2");        else            puts("0");    }    return 0;}


0 0
原创粉丝点击