Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <stack>
- using namespace std;
- const int maxn = 1e5 + 10;
- int n, m;
- vector<int> graph[maxn];
- int discoverable_time[maxn];
- int low_link[maxn];
- bool on_stack[maxn];
- int current_time = 0;
- vector<vector<int> > scc;
- void dfs(int node) {
- discoverable_time[node] = current_time;
- low_link[node] = current_time;
- current_time++;
- on_stack[node] = true;
- static stack<int> st;
- st.push(node);
- for(int i = 0; i < (int) graph[node].size(); i++) {
- int neighbour = graph[node][i];
- if(discoverable_time[neighbour] == -1) {
- dfs(neighbour);
- low_link[node] = min(low_link[node], low_link[neighbour]);
- }
- else if(on_stack[neighbour]) {
- low_link[node] = min(low_link[node], discoverable_time[neighbour]);
- }
- }
- if(low_link[node] == discoverable_time[node]) {
- vector<int> v;
- while(!st.empty()) {
- int t = st.top();
- st.pop();
- on_stack[t] = false;
- v.push_back(t);
- if(t == node) {
- break;
- }
- }
- scc.push_back(v);
- }
- }
- void tarjanSCC() {
- for(int i = 0; i < maxn; i++) {
- discoverable_time[i] = -1;
- on_stack[i] = false;
- low_link[i] = -1;
- }
- for(int i = 0; i < n; i++) {
- if(discoverable_time[i] == -1) {
- dfs(i);
- }
- }
- }
- int main()
- {
- cin >> n >> m;
- for(int i = 0; i < m; i++) {
- int a, b;
- cin >> a >> b;
- a--; b--;
- graph[a].push_back(b);
- }
- tarjanSCC();
- cout << scc.size() << endl;
- int result = 0;
- for(int i = 0; i < scc.size(); i++) {
- result = max(result, (int)scc[i].size());
- }
- cout << result << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement