-
Notifications
You must be signed in to change notification settings - Fork 30
/
The Nagging React newbie.cpp
159 lines (113 loc) · 3.2 KB
/
The Nagging React newbie.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
Solution by Rahul Surana
***********************************************************
JS newbie “A” wants to learn React from “B” and wants to know in his network who can introduce him to B in the shortest time period.
INPUT FORMAT
Total Members in UI Friend Network = N
MemberId1 = N1
MemberId2 = N2
MemberId3 = N3
MemberIdN = Nn
Total Possible Edges = E
<Follower 1> <Following 1> <Time taken to send the message> = p1,q1,t1
<Follower 2> <Following 2> <Time taken to send the message> = p2,q2,t2
<Follower 3> <Following 3> <Time taken to send the message> = p3,q3,t3
<Follower N> <Following N> <Time taken to send the message> = pn,qn,tn
Follower (Ninja A) = A
Following (JS expert B) = B
OUTPUT FORMAT
Shortest Time A takes to reach B
***********************************************************
*/
#include <bits/stdc++.h>
#define ll long long
#define vl vector<ll>
#define vi vector<int>
#define pi pair<int,int>
#define pl pair<ll,ll>
#define all(a) a.begin(),a.end()
#define mem(a,x) memset(a,x,sizeof(a))
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define FOR(i,a) for(int i = 0; i < a; i++)
#define trace(x) cerr<<#x<<" : "<<x<<endl;
#define trace2(x,y) cerr<<#x<<" : "<<x<<" | "<<#y<<" : "<<y<<endl;
#define trace3(x,y,z) cerr<<#x<<" : "<<x<<" | "<<#y<<" : "<<y<<" | "<<#z<<" : "<<z<<endl;
#define fast_io std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
using namespace std;
map<int,vector<int>> adj;
bool bfs(int s, int t)
{
map<int,int> p;
for(auto x: adj) p[x.F] = -1;
queue<int> q;
q.push(s);
p[s] = s;
map<int,bool> v;
while (!q.empty())
{
int u = q.front();
q.pop();
if(v[u]) continue;
v[u] = 1;
if(u == t) { return true; }
for (auto x: adj[u])
{
if(!v[x])
{
q.push(x);
p[x] = u;
}
}
}
return false;
}
bool dfs(int s, int d, int p){
if(s == d) return true;
else{
bool f = false;
for(auto m : adj[s]){
if(m!=p) f = dfs(m,d,s);
if(f) break;
}
return f;
}
}
// int min_cut(int source, int sink){
// cap = adj;
// int ans = 0;
// while(bfs(source, sink)){
// ans++;
// int u = sink;
// while(u != p[u]){
// // cout << p[u] <<" - "<< u <<"\n";
// cap[p[u]].erase(find(cap[p[u]].begin(),cap[p[u]].end(),u));
// u = p[u];
// }
// }
// return ans;
// }
int main()
{
fast_io;
int e,n;
cin >> e;
FOR(i,e) { int f; cin >>f; adj[f] = vector<int>(); }
cin >> n;
FOR(i,n) {
int a,b;
cin >> a >> b;
adj[b-1].pb(a-1);
}
int x,y;
cin >> x >> y;
vector<int> ans;
for(auto z : adj[y-1]){
if(bfs(z,x-1)){
ans.pb(z+1);
}
}
for(auto z: ans) cout << z <<" ";
}