Skip to content

Instantly share code, notes, and snippets.

@luccasiau
Created April 26, 2015 02:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luccasiau/43c399e798996b3e2c60 to your computer and use it in GitHub Desktop.
Save luccasiau/43c399e798996b3e2c60 to your computer and use it in GitHub Desktop.
#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#define MAXN 50010
using namespace std;
int nodes,edges;
int grau[MAXN];
int marc[MAXN];
bool ciclo;
vector<int> myv[MAXN];
vector<int> resp;
priority_queue<int, vector<int>, greater<int> > heap;
void dfs(int x){
marc[x] = 1;
int l = myv[x].size();
for(int i = 0;i<l;i++){
if( marc[myv[x][i]] ) ciclo = 1;
else dfs(myv[x][i]);
}
}
int main(){
scanf("%d %d",&nodes,&edges);
memset(grau,0,sizeof grau);
for(int i = 1;i<=edges;i++){
int a,b;
scanf("%d %d",&a,&b);
a++;
b++;
grau[b]++;
myv[a].push_back(b);
}
for(int i = 1;i<=nodes;i++) if(!grau[i]) heap.push(i);
int cont = 0;
while( !heap.empty() ){
cont++;
int l = myv[heap.top()].size();
int cara = heap.top();
heap.pop();
for(int i = 0; i < l; i++ ){
grau[ myv[cara][i] ]--;
if(!grau[myv[cara][i]]) heap.push(myv[cara][i]);
}
resp.push_back(cara);
}
if(cont < nodes) printf("*\n");
else for(int i = 0;i<nodes;i++) printf("%d\n",resp[i]-1);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment