Pagini recente » Cod sursa (job #1946877) | Cod sursa (job #2828051) | Cod sursa (job #2600890) | Cod sursa (job #2921464) | Cod sursa (job #150757)
Cod sursa(job #150757)
#include<fstream.h>
using namespace std;
#define nmax 50100
#define mmax 100100
ifstream f("sortaret.in");
ofstream g("sortaret.out");
int n, m, a[mmax], st[nmax], poz[nmax], viz[nmax], nr;
int cmp(int x, int y)
{
if( viz[poz[x]] > viz[poz[y]] ) return 0;
else return 1;
}
void citire()
{
int i, x, y, sum;
f>>n>>m;
for(i=1; i<=n; i++){
f>>x>>y;
st[x]++;
}
f.close();
sum=0;
for(i=1; i<=n; i++){
st[i-1]=sum;
sum=sum+st[i];
}
ifstream f("sortaret.in");
f>>n>>m;
for(i=1; i<=n; i++){
f>>x>>y;
a[st[x]]=y;
st[x]--;
}
f.close();
}
void DFS(int x)
{
int i;
viz[x]=++nr;
for(i=st[x]+1; i<=st[x+1]; i++) if(!viz[a[i]]) DFS(a[i]);
}
int main()
{
int i;
citire();
for(i=1; i<=n; i++) if(!viz[i]){
nr=0;
DFS(i);
}
for(i=1; i<=n; i++) poz[i]=i;
sort( poz+1, poz+n+1, cmp);
for(i=1; i<=n; i++) g<<poz[i]<<' ';
g.close();
return 0;
}