//基于AOV(activity on vertex)网络的拓扑排序
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#define VN 20
vector<int> p[VN];
int de[VN];
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int main()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int i,j,s,e;
int n;
int re[VN];
memset(de,0,sizeof(de));
freopen("in.txt","r",stdin);
scanf("%d",&n);
while(scanf("%d%d",&s,&e))
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(s==0&&e==0) break;
p[s].push_back(e);
de[e]++;
}
int k=0;
int flag;
for(i=1;i<=n;i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
flag=0;
for(j=1;j<=n;j++)
if(de[j]==0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
re[k++]=j;
int len=p[j].size();
for(int t=0;t<len;t++)
de[p[j][t]]--;
de[j]=-1;
flag=1;
break;
}
if(!flag) break;
}
if(k<n) printf("Can't do it\n");
else
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for(i=0;i<n;i++)
printf("%d ",re[i]);
printf("\n");
}
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
测试结果:
8
2 1
3 2
2 4
5 4
7 8
1 8
8 3
3 6
4 8
4 6
0 0
Can't do it
8
2 1
2 3
2 4
5 4
7 8
1 8
3 8
3 6
4 8
4 6
0 0
2 1 3 5 4 6 7 8