Pagini recente » Istoria paginii runda/cum_sa_fii_manelist | Cod sursa (job #989870) | Monitorul de evaluare | Istoria paginii runda/simulare-cartita-28 | Cod sursa (job #1236161)
#include <fstream>
#include <iostream>
#include <vector>
#include <bitset>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <deque>
using namespace std;
const char infile[] = "ctc.in";
const char outfile[] = "ctc.out";
ifstream fin(infile);
ofstream fout(outfile);
const int MAXN = 100005;
const int oo = 0x3f3f3f3f;
typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;
const inline int min(const int &a, const int &b) { if( a > b ) return b; return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b; return a; }
const inline void Get_min(int &a, const int b) { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b) { if( a < b ) a = b; }
class DirectedGraph {
private:
vector <vector <int> > adj;
vector <bool> used;
int N;
public:
DirectedGraph() {
}
DirectedGraph(int _N) {
adj.resize(_N);
used.resize(_N, 0);
N = _N;
}
void addDirectedEdge(int x, int y) {
adj[x].push_back(y);
}
void dfs(int node, vector <int> &v) {
if(used[node])
return;
used[node] = 1;
for(auto it:adj[node])
if(!used[it])
dfs(it, v);
v.push_back(node);
}
void getTsort(int node, vector <int> &v) {
if(used[node])
return;
used[node] = 1;
for(auto it:adj[node])
if(!used[it])
getTsort(it, v);
v.push_back(node);
}
};
int main() {
cin.sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen(infile, "r", stdin);
freopen(outfile, "w", stdout);
#endif
int N, M;
fin >> N >> M;
DirectedGraph G(N), Gt(N);
for(; M -- ;) {
int x, y;
fin >> x >> y;
--x ; -- y;
G.addDirectedEdge(x, y);
Gt.addDirectedEdge(y, x);
}
vector <int> tsort;
vector <vector <int> > CTC;
for(int i = 0 ; i < N ; ++ i)
G.getTsort(i, tsort);
for(auto it = tsort.rbegin() ; it != tsort.rend(); ++ it) {
vector <int> act;
Gt.dfs(*it, act);
if(!act.empty())
CTC.push_back(act);
}
fout << CTC.size() << '\n';
for(auto it : CTC) {
for(auto el: it)
fout << el + 1 << ' ';
fout << '\n';
}
fin.close();
fout.close();
return 0;
}