您的位置:首页 > 其它

URAL - 1329. Galactic History(树的遍历)

2017-04-13 20:33 253 查看


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 fr
4000
om 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
10
234 -1
12 234
13 234
14 234
15 234
16 234
17 234
18 234
19 234
233 19
5
234 233
233 12
233 13
233 15
233 19

1
0
0
0
2

Problem Author: Evgeny Krokhalev
Problem Source: The 10th Collegiate Programming Contest of the High School Pupils of the Sverdlovsk Region (October 16, 2004)
读错题意了,看了下大佬的思路,判断区间是否重叠,如果前一个点的区间包括后一个点的区间,那么前一个点一定是后一个点的父节点或更接近根的节点,其余同理,关键点在于如何计算一个节点包括的区间,搜索实现,具体看代码
#include <iostream>
#include <iomanip>
#include<stdio.h>
#include<string.h>
#include<stack>
#include<stdlib.h>
#include<queue>
#include<map>
#include<math.h>
#include<algorithm>
#include<vector>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define N 40010
typedef  long long LL;
using namespace std;
int l
,r
,vis
,num;
vector<int>v
;
void dfs(int x)
{
vis[x]=1;
l[x]=num++;
int len=v.size();
//遍历每一个节点的子节点,计算其区间
for(int i=0;i<v.size();i++)
{
if(!vis[x][i])
dfs(i);
}
r[x]=num-1;

}
int main()
{
int n,q,a,b,rank1;
scanf("%d",&n);
mem(l,0);
mem(r,0);
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;
dfs(rank1);
scanf("%d",&q);
for(int i=1;i<=q;i++)
{
scanf("%d%d",&a,&b);
//判断区间是否重叠
if(l[a]<=l[b]&&r[a]>=l[b])
printf("1\n");
else if(l[a]>=l[b]&&r[a]<=r[b])
printf("2\n");
else
printf("0\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: