hdu-4857-逃生-拓撲排序,hdu-4857-逃生拓撲

來源:互聯網
上載者:User

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;
......餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.