hdu-4857-逃生-拓撲排序,hdu-4857-逃生拓撲
拓撲排序。
反向建邊。
為了序號小的盡量在前面,我們每次都取出入度為0的最大的點。
#include<stdio.h>#include<algorithm>#include<iostream>#include<stdlib.h>#include<vector>#include<queue>#include<string.h>#include<math.h>using namespace std;struct list{ int u,v,w; int next;}edge[110000];int head[33000];int nums;void add(int u,int v,int w){ edge[nums].u=u; edge[nums].v=v; edge[nums].w=w; edge[nums].next=head[u]; head[u]=nums++;}int du[33000];void init(){ memset(head,-1,sizeof(head)); nums=1; memset(du,0,sizeof(du));}priority_queue<int>que;vector<int>vec;int n;void dos(){ vec.clear(); while(!que.empty())que.pop(); for(int i=1;i<=n;i++) { if(du[i]==0)que.push(i); } while(!que.empty()) { int x=que.top(); que.pop(); for(int i=head[x];i!=-1;i=edge[i].next) { int y=edge[i].v; du[y]--; if(du[y]==0) { que.push(y); } } vec.push_back(x); } for(int i=n-1;i>=0;i--) { if(i!=n-1)printf(" "); printf("%d",vec[i]); } cout<<endl;}int main(){ int T,m,a,b; scanf("%d",&T); while(T--) { init(); scanf("%d%d",&n,&m); for(int i=1;i<=m;i++) { scanf("%d%d",&a,&b); add(b,a,1); du[a]++; } dos(); } return 0;}
HDU 3342 拓撲排序
#include<iostream>
using namespace std;
#define maxn 105
int map[maxn][maxn];
int degree[maxn];
int n,m;
int top;
int top2; // 新增top2
int f[maxn];
void toposort()
{
int i,j;
bool p=true;
top=0;
top2=0; // 初始化top2
while(p) {
p=false;
top++;
for(i=0;i<n;i++)
if(degree[i]==0) {
p=true;
f[i]=top;
top2++; // 數一下top2
}
for(i=0;i<n;i++)
if(f[i]==top) {
for(j=0;j<n;j++)
if(map[i][j]) degree[j]--;
degree[i]=-1;
}
}
top--;
}
int main()
{
int i,j,k;
while(cin>>n>>m)
{ if(n==0&&m==0) break;
memset(map,0,sizeof(map));
memset(degree,0,sizeof(degree));
memset(f,0,sizeof(f)); // 初始化f, 這個很關鍵
int a,b;
while(m--)
{ cin>>a>>b;
if(map[a][b]==0)
{ map[a][b]=1;
degree[b]++;
}
}
top=0;
toposort();
if(top2==n) cout<<"YES"<<endl; // 用top2比較
else cout<<"NO"<<endl;
}
return 0;
}...餘下全文>>
幫忙解釋這個拓撲排序的程式
#include <stdio.h>//呵呵,我也學了一回,原來是這麼回事,有機會也要用用,
#include <string.h>
typedef struct node{//邊接點
int adjvex;
struct node *next;
}EdgeNode;
typedef struct vnode{//頂點
int id;
EdgeNode *link;
}vnode,Adjlist[100];
typedef Adjlist LGraph;
typedef struct snode{//棧結點
int data;
struct snode *next;
}Link_Stack;
Link_Stack *top,*s;
void Push(Link_Stack **top,int x)//入棧
{
s=(Link_Stack*)malloc(sizeof(Link_Stack));
s->data=x;
s->next=(*top)->next;
(*top)->next=s;
}
void GetTop(Link_Stack **top,int *x)//出棧,取得棧頂元素
{
s=(*top)->next;
*x=s->data;
(*top)->next=s->next;
free(s);
}
int CreatGraph(LGraph gl)//建立圖
{
int m,n,i=0,c,d;
EdgeNode *p;
printf("input start.\n");
printf("please input how many vertex there had?\n",i);
scanf("\n%d",&c);
for(i=0;i<c;i++){//初始化鄰接表
gl[i].link=NULL;
gl[i].id=0;
}
printf("please input EdgeNodes number?");
scanf("%d",&d);
for(i=0;i<d;i++){
printf("\nplease input Vi&Vj:=");
scanf("\n%d,%d",&m,&n);
if((m>=0)&&(m<c)&&(n>=0)&&(n<c)){//驗證m,n合法則輸入,以建立鄰接表
p=(EdgeNode*)malloc(sizeof(EdgeNode));
p->adjvex=n; //頂點
p->next=gl[m].link; //在相應位置插入鄰接表
gl[m].link=p;
gl[n].id++;//頂點n入度+1
}
}
return c;
}
void TopuSort(LGraph G,int n)//拓撲排序
{
int i,j,m=0;
......餘下全文>>