-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathG.cpp
More file actions
133 lines (112 loc) · 3.24 KB
/
G.cpp
File metadata and controls
133 lines (112 loc) · 3.24 KB
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
// Jai Mata Di..
//
// @ author : Sanjit Anand (sanjit_15)
//
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T>
using order_set = tree<T, null_type, less<T>, rb_tree_tag , tree_order_statistics_node_update>;
template<typename T>
using order_multiset = tree<T,null_type,less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
#define PI 3.1415926535897932384626
#define gc getchar_unlocked
#define fo(i,n) for(i=0;i<n;i++)
#define rep(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define pf pop_front
#define ppb pop_back
#define ppf pop_front
#define F first
#define S second
#define trav(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define ppc __builtin_popcount
#define ppcll __builtin_popcountll
#define ctzll __builtin_ctzll
#define clzll __builtin_clzll
#define all(x) x.begin(),x.end()
template<typename T1,typename T2>istream& operator>>(istream& in,pair<T1,T2> &a){in>>a.fr>>a.sc;return in;}
template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.fr<<" "<<a.sc;return out;}
template<typename T,typename T1>T amax(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T amin(T &a,T1 b){if(b<a)a=b;return a;}
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef set<int> si;
typedef list<int> li;
typedef tuple<int> tpi;
const long long INF = 1e18;
const long long MOD = 1000000007;
const long long MM = 998244353;
const int N = 100005;
int mpow(int base, int exp, int m);
int gcd(int a, int b);
vector<int> graph[N]; // graph
int dp[N]; // length of the longest path from (i)th node as source
int helper(int src){
if(graph[src].size() == 0){
dp[src] = 0;
return 0;
}
if(dp[src] != -1){
return dp[src];
}
int ans = 0;
for(auto child : graph[src]){
ans = max(ans,1 + helper(child));
}
dp[src] = ans;
return dp[src];
}
void solve() {
int i, j, n, m;
cin >> n >> m;
int a, b;
for(int i=1; i<=m; i++){
cin >> a >> b;
graph[a].pb(b);
}
memset(dp, -1, sizeof(dp));
int ans = 0;
for(int i=1; i<=n; i++){
ans = max(ans, helper(i));
}
cout << ans << "\n";
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#ifdef NCR
pmain();
#endif
#ifdef SIEVE
sieve();
#endif
int t = 1;
// cin >> t;
while(t--) {
solve();
}
return 0;
}
int gcd(int a, int b){
return b==0 ? a : gcd(b,a%b);
}
int mpow(int base, int exp, int m=MOD) {
base %= m;
int result = 1;
while (exp > 0) {
if (exp & 1) result = ((long long)result * base) % m;
base = ((long long)base * base) % m;
exp >>= 1;
}
return result;
}