Mai intai trebuie sa te autentifici.
Cod sursa(job #681617)
Utilizator | Data | 17 februarie 2012 15:44:35 | |
---|---|---|---|
Problema | Componente tare conexe | Scor | 60 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.92 kb |
#include<fstream>
#include<vector>
using namespace std;
ifstream f("ctc.in");
ofstream g("ctc.out");
int m,n,x,y,viz[100001],nr;
vector<int> a[100001],at[100001],b[100001],pord;
void citire()
{
int i;
f>>n>>m;
for(i=1;i<=m;i++)
{
f>>x>>y;
a[x].push_back(y);
at[y].push_back(x);
}
}
void DFS(int x)
{
viz[x]=1;
for(vector<int>::iterator it=a[x].begin();it!=a[x].end();it++)
if(!viz[*it])
DFS(*it);
pord.push_back(x);
}
void DFST(int x)
{
viz[x]=0;
b[nr].push_back(x);
for(vector<int>::iterator it=at[x].begin();it!=at[x].end();it++)
if(viz[*it])
DFST(*it);
}
int main()
{
int i;
citire();
for(i=1;i<=n;i++)
if(!viz[i])
DFS(i);
for(i=n-1;i>=0;i--)
if(viz[pord[i]])
{
nr++;
DFST(pord[i]);
}
g<<nr<<endl;
for(i=1;i<=nr;i++)
{
for(vector<int>::iterator it=b[i].begin(); it!=b[i].end(); it++)
g<<*it<<' ';
g<<endl;
}
return 0;
}