zoukankan      html  css  js  c++  java
  • bzoj 4010: [HNOI2015]菜肴制作

    这是一个结论题。。

    要求的序=反图的最大字典序

    也就是尽量把大的放在后面。。

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<cmath>
    #include<queue>
    using namespace std;
    
    struct node
    {
        int x,y,next;
    }a[110000];int len,last[110000];
    void ins(int x,int y)
    {
        len++;
        a[len].x=x;a[len].y=y;
        a[len].next=last[x];last[x]=len;
    }
    priority_queue<int>q;
    int du[110000],as[110000];
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            int n,m,x,y;
            scanf("%d%d",&n,&m);
            len=0;memset(last,0,sizeof(last));
            memset(du,0,sizeof(du));
            for(int i=1;i<=m;i++)
            {
                scanf("%d%d",&x,&y);
                ins(y,x);du[x]++;
            }
            for(int i=1;i<=n;i++)
                if(du[i]==0)q.push(i);
            
            bool bk=true;
            for(int i=1;i<=n;i++)
            {
                if(q.empty()){bk=false;break;}
                int x=q.top();q.pop();
                as[i]=x;
                for(int k=last[x];k;k=a[k].next)
                {
                    int y=a[k].y;
                    du[y]--;
                    if(du[y]==0)q.push(y);
                }
            }
            if(bk==false)printf("Impossible!
    ");
            else
            {
                for(int i=n;i>1;i--)printf("%d ",as[i]);
                printf("%d
    ",as[1]);
            }
        }
        return 0;
    }
  • 相关阅读:
    2016CCPC长春
    POJ 3974
    CH 1401
    POJ 1426
    hihocoder 1829
    江南OJ 1151
    POJ 3279
    POJ 3349
    POJ 3278
    ZOJ 3983
  • 原文地址:https://www.cnblogs.com/AKCqhzdy/p/10153620.html
Copyright © 2011-2022 走看看