-
Notifications
You must be signed in to change notification settings - Fork 66
/
22-Game Routes.cpp
49 lines (48 loc) · 978 Bytes
/
22-Game Routes.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
#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
int gameRoutes(int n, vector<vector<int>> teleporters)
{
long long x,y;
vector<vector<int>>v(n+1);
vector<int>ind(n+1);
vector<long long>dp(n+1);
for(int i=0;i<teleporters.size();i++)
{
x=teleporters[i][0],y=teleporters[i][1];
v[x].push_back(y);
ind[y]++;
}
queue<int>q;
vector<int>topo;
for(int i=1;i<=n;i++)
{
if(ind[i]==0)
{
q.push(i);
}
}
while(q.size())
{
x=q.front();
q.pop();
topo.push_back(x);
for(auto itr:v[x])
{
ind[itr]--;
if(ind[itr]==0)
{
q.push(itr);
}
}
}
dp[1]=1;
for(auto itr:topo)
{
for(auto it:v[itr])
{
dp[it]=(dp[it]+dp[itr])%mod;
}
}
return dp[n];
}