Pagini recente » Cod sursa (job #1660088) | Cod sursa (job #3123182) | Cod sursa (job #2120862) | Cod sursa (job #2188905) | Cod sursa (job #2509955)
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
const int MAXN = 100010;
ifstream fin("ctc.in");
ofstream fout("ctc.out");
int n, m, nr, k, ord[MAXN], ll[MAXN];
bool inSt[MAXN];
vector<int> edges[MAXN], comp[MAXN];
stack<int> st;
void read() {
fin >> n >> m;
for (int i = 0; i < m; ++i) {
int x, y;
fin >> x >> y;
edges[x].pb(y);
}
}
void dfs(int node) {
st.push(node);
inSt[node] = 1;
ll[node] = ord[node] = ++k;
for (vector<int>::iterator it = edges[node].begin(); it != edges[node].end(); ++it)
if (!ord[*it]) {
dfs(*it);
ll[node] = min(ll[node], ll[*it]);
}
else if (inSt[*it])
ll[node] = min(ll[node], ord[*it]);
if (ll[node] == ord[node]) {
while (!st.empty()) {
int top = st.top();
st.pop();
inSt[top] = 0;
comp[nr].pb(top);
if (top == node)
break;
}
++nr;
}
}
void testPrint() {
for (int i = 0; i < n; ++i)
cout << ord[i + 1] << ' ' << ll[i + 1] << '\n';
}
void print() {
fout << nr << '\n';
for (int i = 0; i < nr; ++i) {
for (vector<int>::iterator it = comp[i].begin(); it != comp[i].end(); ++it)
fout << *it << ' ';
fout << '\n';
}
}
int main() {
read();
for (int i = 1; i <= n; ++i)
if (!ord[i])
dfs(i);
print();
testPrint();
return 0;
}