Pagini recente » Statistici Antonia Tudor (antonia_tudor) | Monitorul de evaluare | Monitorul de evaluare | Atasamentele paginii Profil antonia_tudor | Cod sursa (job #2054821)
#include <fstream>
#include <vector>
#include <stdio.h>
#define NM 100005
using namespace std;
/*ifstream hai("ctc.in");
ofstream pa("ctc.out");*/
FILE *hai=fopen("ctc.in","r");
FILE *pa=fopen("ctc.out","w");
vector<int> G[NM],GT[NM],CTC[NM];
int n, m, viz[NM], st[NM], t = 0, k = 0;
void Dfs(int x);
void Dfst(int x);
int main(){
int x,y,i,j;
//hai >> n >> m;
fscanf(hai,"%d %d", &n,&m);
while(m){
//hai >> x >> y;
fscanf(hai,"%d %d",&x,&y);
G[x].push_back(y);
GT[y].push_back(x);
m--;
}
for(i = 1; i <= n; i++)
if(!viz[i])
Dfs(i);
// free the viz
for(i = 1; i <= n; i++)
viz[i] = 0;
for(i = n; i >= 1; i--)
if(!viz[st[i]]){
k++;
Dfst(st[i]);
}
//pa << k << endl;
fprintf(pa,"%d\n",k);
for(i = 1; i <= k; i++){
for(j = 0; j < CTC[i].size();j++)
//pa << CTC[i][j] << " ";
fprintf(pa,"%d ",CTC[i][j]);
//pa << endl;
fprintf(pa,"\n");
}
}
void Dfs(int x){
int i, w;
viz[x] = 1;
for(i = 0; i < G[x].size(); i++){
w = G[x][i];
if(!viz[w])
Dfs(w);
}
t++;
st[t] = x;
}
void Dfst(int x){
int w;
viz[x] = 1;
CTC[k].push_back(x);
for(int i = 0; i < GT[x].size();i++){
w = GT[x][i];
if(!viz[w])
Dfst(w);
}
}