Sei sulla pagina 1di 5

3/17/2019 Oliver and the Game | Topological Sort & Algorithms Practice Problems | HackerEarth


All Tracks  Algorithms  Graphs   Problem

3

LIVE EVENTS
Oliver and the Game
Attempted by: 734 / Accuracy: 68% / Maximum Score: 30 /      18 Votes

Tag(s): Algorithms, DFS, Data Structures, Medium

PROBLEM EDITORIAL MY SUBMISSIONS

This Problem can be solved by Depth First Search and Topological Sorting.

The idea is to maintain a global timer variable which stores an in time and an out time during DFS calls. See
author's solution for clarity.

In any query we just need to check if one node is fully contained within another or not. In other words, if one
node lies in the sub tree of the other node, then the answer might be YES depending on the Query type 0 or 1.

IS THIS EDITORIAL HELPFUL?


 

Yes, it's helpful No, it's not helpful

50 developer(s) found this editorial helpful.

Author Solution by Arpit Tripathi

1. #include<bits/stdc++.h>
2. using namespace std;
3.
4. int vertex;
5. vector<vector<int> > tree; //used for representing the tree
6. vector<bool> visited;
7.
8. vector<int> starttime; // starttime[i] notes the time at which DFS enters node i
9. vector<int> endtime; // endtime[i] notes the time at which DFS exits node i
10. int timer = 0; // a global variable that stores the timer at that instant
11.
12. void makeTree() // takes the input and creates a directed graph representing the
tree
13. {
14. scanf("%d",&vertex);
15. tree.resize(vertex+1);
16. ?
17. for(int i = 1; i < vertex ; i++)

https://www.hackerearth.com/practice/algorithms/graphs/topological-sort/practice-problems/algorithm/oliver-and-the-game-3/editorial/ 1/5
3/17/2019 Oliver and the Game | Topological Sort & Algorithms Practice Problems | HackerEarth

18. { int x,y;


19. scanf("%d%d",&x,&y);
20.
21. tree[x].push_back(y);
22. } 3

LIVE EVENTS
23. }
24.
25. void measureTime(int v) // Performs Depth First Search
26. {
27. visited[v] = 1;
28.
29. starttime[v] = timer++;
30.
31. for(int i = 0 ; i < tree[v].size() ; i++) // calling measureTime() for adjacent
nodes of node v and performing DFS
32. {
33. if( visited [ tree[ v ][ i ] ] == 0 )
34. measureTime(tree[v][i]);
35. }
36. endtime[v] = timer++;
37. }
38.
39. int check(int x, int y)
40. {
41. if( starttime[x] > starttime[y] && endtime[x] < endtime[y] ) // checks weather
node x lies in the subtree of node y or not
42. return 1;
43. return 0;
44. }
45.
46. int main()
47. {
48. makeTree();
49.
50. visited.resize(vertex+1,0);
51. starttime.resize(vertex+1,0);
52. endtime.resize(vertex+1,0);
53.
54. measureTime(1);
55.
56. int q;
57. scanf("%d",&q);
58.
59. while(q--)
60. {
61. int type,x,y;
62. scanf("%d%d%d",&type,&x,&y);
63.
64. if( !check(x,y) && !check(y,x) )
65. {
66. printf("NO\n");
67. continue;
68. } ?
69.

https://www.hackerearth.com/practice/algorithms/graphs/topological-sort/practice-problems/algorithm/oliver-and-the-game-3/editorial/ 2/5
3/17/2019 Oliver and the Game | Topological Sort & Algorithms Practice Problems | HackerEarth

70. if(type == 0)
71. {
72. if(check(y,x) == 1)
73. printf("YES\n");
74. else 3

LIVE EVENTS
75. printf("NO\n");
76. }
77. else
78. {
79. if(check(x,y) == 1)
80. printf("YES\n");
81. else
82. printf("NO\n");
83. }
84. }
85.
86. return 0;
87. }
88.

Tester Solution by Kuldeep Fouzdar

1. #include <algorithm>
2. #include <bitset>
3. #include <cassert>
4. #include <cstdio>
5. #include <cmath>
6. #include <cstdlib>
7. #include <ctime>
8. #include <cstring>
9. #include <deque>
10. #include <functional>
11. #include <iostream>
12. #include <iomanip>
13. #include <list>
14. #include <math.h>
15. #include <map>
16. #include <numeric>
17. #include <queue>
18. #include <set>
19. #include <sstream>
20. #include <stack>
21. #include <string>
22. #include <utility>
23. #include <vector>
24.
25. #define LL long long
26. #define ULL unsigned long long
27. #define F first
28. #define S second
29. #define pb push_back
30. #define FOR(i,lb,ub) for(i=lb;i<=ub;i++) ?
31. #define RFOR(i,ub,lb) for(i=ub;i>=lb;i--)
https://www.hackerearth.com/practice/algorithms/graphs/topological-sort/practice-problems/algorithm/oliver-and-the-game-3/editorial/ 3/5
3/17/2019 Oliver and the Game | Topological Sort & Algorithms Practice Problems | HackerEarth

32. #define FORS(it,v) for(it=v.begin();it!=v.end();it++)


33. using namespace std;
34. vector<int> G[100005];
35. bool visited[100005];
36. int in[100005], out[100005], ts; 3

LIVE EVENTS
37. int cnt;
38. void dfs(int x)
39. {
40. cnt++;
41. visited[x] = true;
42. in[x] = ++ts;
43. for (int i = 0; i <G[x].size(); ++i)
44. {
45. if (!visited[G[x][i]])
46. dfs(G[x][i]);
47. }
48. out[x] = ++ts;
49. }
50. bool is_subtree(int x, int y)//if y lies in subtree of x
51. {
52. if (in[x] <= in[y] && out[x]>=out[y])
53. return true;
54. return false;
55. }
56. int main()
57. {
58. ios_base::sync_with_stdio(false);
59. cin.tie(NULL);
60. int t,i,j;
61. int n;
62. cin>>n;
63. assert(n>=1 && n<=100000);
64. FOR(i,0,n-2)
65. {
66. int a,b;
67. cin>>a>>b;
68. assert(a>=1 && a<=n);
69. assert(b>=1 && b<=n);
70. G[a].pb(b);
71. G[b].pb(a);
72. }
73. dfs(1);
74. assert(cnt==n);
75. int q;
76. cin>>q;
77. assert(q>=1 && q<=500000);
78. while (q--)
79. {
80. int a,x,y;
81. cin>>a>>x>>y;
82. assert(a==0 || a==1);
83. assert(x>=1 && x<=n);
84. assert(y>=1 && y<=n); ?
85. if ((!a && is_subtree(x,y)) || (a && is_subtree(y,x)))

https://www.hackerearth.com/practice/algorithms/graphs/topological-sort/practice-problems/algorithm/oliver-and-the-game-3/editorial/ 4/5
3/17/2019 Oliver and the Game | Topological Sort & Algorithms Practice Problems | HackerEarth

86. cout<<"YES\n";
87. else
88. cout<<"NO\n";
89. }
90. 3

LIVE EVENTS
91. return 0;
92. }

About Us Innovation Management Technical Recruitment

University Program Developers Wiki Blog

Press Careers Reach Us

Site Language: English | Terms and Conditions | Privacy |© 2019 HackerEarth

https://www.hackerearth.com/practice/algorithms/graphs/topological-sort/practice-problems/algorithm/oliver-and-the-game-3/editorial/ 5/5

Potrebbero piacerti anche