Pagini recente » Cod sursa (job #3356282) | Cod sursa (job #3319904) | Cod sursa (job #3323604) | Cod sursa (job #3346587) | Cod sursa (job #3349070)
#include <stdio.h>
#define MAXN 100000
#define MAXM 200000
using namespace std;
FILE *fin, *fout;
struct cell{
int v, next;
};
struct node{
int adj;
int time_in;
int low;
bool on_stack;
};
struct my_stack{
int v[MAXN+1];
int pointer=0;
void push(int val){
v[++pointer]=val;
}
void pop(){
pointer--;
}
int top(){
return v[pointer];
}
};
cell my_list[MAXM+1];
node nd[MAXN+1];
my_stack st;
int n, m, time;
int my_min(int a, int b){
return a<b?a:b;
}
void write_ctc_and_pop(int last){
int v;
do{
v=st.top();
st.pop();
fprintf(fout, "%d ", v);
nd[v].on_stack=false;
}while(v!=last);
fprintf(fout, "\n");
}
void dfs(int u){
nd[u].time_in=nd[u].low=++time;
nd[u].on_stack=true;
st.push(u);
for(int ptr=nd[u].adj;ptr;ptr=my_list[ptr].next){
int v=my_list[ptr].v;
if(!nd[v].time_in){
dfs(v);
nd[u].low=my_min(nd[u].low, nd[v].low);
}
else if(nd[v].on_stack){
nd[u].low=my_min(nd[u].low, nd[v].time_in);
}
}
if(nd[u].low==nd[u].time_in){
write_ctc_and_pop(u);
}
}
void dfs_driver(){
for(int u=1;u<=n;u++){
if(!nd[u].time_in){
dfs(u);
}
}
}
void add_edge(int u, int v){
static int pos=1;
my_list[pos]={v, nd[u].adj};
nd[u].adj=pos++;
}
void read_graph(){
int u, v;
fin=fopen("ctc.in", "r");
fscanf(fin, "%d%d", &n, &m);
for(int i=0;i<m;i++){
fscanf(fin, "%d%d", &u, &v);
add_edge(u, v);
}
fclose(fin);
}
int main()
{
read_graph();
fout=fopen("ctc.out", "w");
dfs_driver();
fclose(fout);
return 0;
}