【BZOJ4010】【HNOI2015】菜肴製作,bzoj4010hnoi2015

來源:互聯網
上載者:User

【BZOJ4010】【HNOI2015】菜肴製作,bzoj4010hnoi2015
連結:

#include <stdio.h>int main(){    puts("轉載請註明出處[vmurder]謝謝");    puts("網址:blog.csdn.net/vmurder/article/details/45365831");}
題解:

把所有入度為0的點入優先隊列,每次取出標號最大的,並將此點取走後入度為0的點入優先隊列,最後反序輸出。

代碼:
#include <queue>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define N 101000#define M 101000using namespace std;struct Eli{    int v,next;}e[M];int head[N],cnt,d[N];inline void add(int u,int v){    d[v]++;    e[++cnt].v=v;    e[cnt].next=head[u];    head[u]=cnt;}priority_queue<int>q;int ans[N],n,m;int main(){    freopen("test.in","r",stdin);    int i,j,k;    int a,b,c;    int g;for(scanf("%d",&g);g--;)    {        memset(head,0,sizeof head);        memset(d,0,sizeof d);        scanf("%d%d",&n,&m);        cnt=0;        while(m--)        {            scanf("%d%d",&a,&b);            add(b,a);        }        m=0;        for(i=1;i<=n;i++)if(!d[i])q.push(i);        while(!q.empty())        {            ans[++m]=q.top(),q.pop();            for(i=head[ans[m]];i;i=e[i].next)                if(!--d[e[i].v])q.push(e[i].v);        }        if(m!=n)puts("Impossible!");        else {while(m)printf("%d ",ans[m--]);puts("");}    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.