Pagini recente » Cod sursa (job #954783) | Cod sursa (job #1085214) | Cod sursa (job #1224575) | Cod sursa (job #2634771) | Cod sursa (job #2574555)
#include <bits/stdc++.h>
#define nmax 100005
using namespace std;
ifstream fin("ctc.in");
ofstream fout("ctc.out");
int noduri, muchii, componente_tari, a, b;
bool ids[nmax], instack[nmax];
int lowkey[nmax];
vector<int> mat[nmax];
stack<int> s;
void dfs(int node)
{
s.push(node);
instack[node] = 1;
ids[node] = node;
lowkey[node] = node;
for(int i = 0; i < mat[node].size(); ++i)
{
int next_node = mat[node][i];
if(!ids[next_node])
{
dfs(next_node);
}
if(instack[next_node])
{
lowkey[node] = min(lowkey[node], lowkey[next_node]);
}
}
if(ids[node] == lowkey[node])
{
while(!s.empty())
{
int next = s.top();
s.pop();
instack[next] = 0;
lowkey[next] = ids[node];
if(node == next) break;
}
componente_tari++;
}
}
int main()
{
fin >> noduri >> muchii;
for(int i = 1; i <= muchii; ++i)
{
fin >> a >> b;
mat[a].push_back(b);
}
for(int node = 1; node <= noduri; ++node)
{
if(!ids[node])
{
dfs(node);
}
}
fout << componente_tari;
for(int i = 1; i <= noduri; ++i) cout << lowkey[i] << " ";
return 0;
}