Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define int long long
- using namespace std;
- const int N = 3*1e5+5;
- const int inf = 10000000000005;
- int n,m,cnt=0,lt=0;
- vector<int> a[N];
- vector<int> num,low,res[N];
- stack<int> st;
- void dfs(int u)
- {
- num[u]=low[u]=++cnt;
- st.push(u);
- for(int v : a[u])
- {
- if (num[v]) low[u] = min(low[u], num[v]);
- else
- {
- dfs(v);
- low[u] = min(low[u],low[v]);
- }
- }
- if (num[u]==low[u])
- {
- ++lt;
- int v;
- do
- {
- v = st.top();
- st.pop();
- res[lt].push_back(v);
- low[v] = num[v] = inf;
- } while (v != u);
- }
- }
- signed main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie();cout.tie(0);
- cin >> n >> m;
- num.resize(n+1,0);
- low.resize(n+1,0);
- while (m--)
- {
- int u,v;
- cin >> u >> v;
- a[u].push_back(v);
- }
- for(int i=1; i<=n; ++i)
- {
- if (!num[i]) dfs(i);
- }
- cout << lt << endl;
- for(int i=1; i<=lt; ++i)
- {
- cout << res[i].size() << endl;
- for(int v : res[i]) cout << v << " ";
- cout << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement