您的位置:首页 > 其它

poj 2367 拓扑排序

2014-08-11 15:33 274 查看
题意:n行,每行的编号要在给出的数字之前,输出一种情况

解法:拓扑排序~~

/*
----------------------------------

Love is more than a word.
It says so much.
When I see these four letters,
I almost feel your touch.
This is only happened since
I fell in love with you.
Why this word does this,
I haven't got a clue.

To My Goddess
CY
----------------------------------
*/

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>

#define maxn 50000+5

#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define REP(i,a,b) for(i=a;i<=b;i++)
#define rep(i,n) for(i=0;i<n;i++)

#define cle(a) memset(a,0,sizeof(a))
#define clehead(a) rep(i,maxn)a[i]=-1

/*
The time of story :
**  while(1)
{
once upon a time,
there was a mountain,
on top of which there was a temple,
in which there was an old monk and a little monk.
Old monk was telling stories inside the temple.
What was he talking about?
**  }

ÎûÎû
(*^__^*)
*/

#define sci(a) scanf("%d",&a)
#define scd(a) scanf("%lf",&a)
#define pri(a) printf("%d",a)
#define prie(a) printf("%d\n",a)
#define prd(a)  printf("%lf",a)
#define prde(a) printf("%lf\n",a)
#define pre printf("\n")

#define LL(x) x<<1
#define RR(x) x<<|1

#define pb push_back
#define mod 90001
#define PI 3.141592657

const ull INF = 1LL << 61;
const int inf =   int(1e5)+10;
const double eps=1e-5;

using namespace std;
int in[maxn];
struct node
{
int u,v,w;
int next;
}tree[maxn];

bool cmp(int a,int b){
return a>b;
}
int n;
int cnt;
int head[maxn];
void init()
{
cnt=0;
memset(head,-1,sizeof(head));
cle(in);
}
void add(int x,int y)
{
tree[cnt].u=x;
tree[cnt].v=y;
tree[cnt].next=head[x];
in[y]++;
head[x]=cnt++;
}

int main()
{
freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(cin>>n)
{
int i;
init();
reP(i,n){
int m;
while(cin>>m&&m)
{
add(i,m);
}
}
int queue[10000];
int iq=0;
reP(i,n)
{
if(in[i]==0)
{
queue[iq++]=i;
}
}
int k;
rep(i,iq)
{
for(k=head[queue[i]];k!=-1;k=tree[k].next)
{
in[tree[k].v]--;
if(in[tree[k].v]==0)
{
queue[iq++]=tree[k].v;
}
}
}
rep(i,iq){
cout<<queue[i]<<" ";
}
pre;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: