zoukankan      html  css  js  c++  java
  • 【bzoj4010】 HNOI2015—菜肴制作

    http://www.lydsy.com/JudgeOnline/problem.php?id=4010 (题目链接)

    题意

      给出一张无向图要求出一个拓扑序列满足1的位置最靠前 ,在保证上面的条件下使2的位置最靠前 ,在保证上面的条件下使3的位置最靠前 ……

    Solution 

      这个问题就等价于是倒过来的字典序最大。构造逆拓扑序,套个堆每次选出编号最大的加入答案,输出答案时从后往前输出。 

      有点难想,但是事后诸葛亮一下还是OK的。

    细节

      多组数据,清空数组。

    代码

    // bzoj4010
    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdlib>
    #include<cstdio>
    #include<cmath>
    #include<queue>
    #define MOD 1000000007
    #define inf 2147483640
    #define LL long long
    #define free(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout);
    using namespace std;
    
    const int maxn=100010;
    struct edge {int to,next;}e[maxn<<1];
    priority_queue<int> q;
    int head[maxn],ans[maxn],d[maxn],n,m,top,cnt;
    
    void link(int u,int v) {
        e[++cnt].to=v;e[cnt].next=head[u];head[u]=cnt;
    }
    int main() {
        int T;scanf("%d",&T);
        while (T--) {
            scanf("%d%d",&n,&m);cnt=0;
            memset(head,0,sizeof(head));memset(d,0,sizeof(d));
            for (int u,v,i=1;i<=m;i++) {
                scanf("%d%d",&u,&v);
                link(v,u);d[u]++;
            }
            top=0;
            while (q.size()) q.pop();
            for (int i=1;i<=n;i++) if (!d[i]) q.push(i);
            while (q.size()) {
                int x=q.top();q.pop();
                ans[++top]=x;
                for (int i=head[x];i;i=e[i].next) {
                    d[e[i].to]--;
                    if (d[e[i].to]==0)
                        q.push(e[i].to);
                }
            }
            if (top!=n) printf("Impossible!
    ");
            else {
                for (int i=n;i>=1;i--) printf("%d ",ans[i]);
                printf("
    ");
            }
        }
        return 0;
    }
    

      

    This passage is made by MashiroSky.
  • 相关阅读:
    codeforces 814B An express train to reveries
    codeforces 814A An abandoned sentiment from past
    codeforces 785D D. Anton and School
    codeforces 785C Anton and Fairy Tale
    codeforces 791C Bear and Different Names
    AOP详解
    Spring集成JUnit测试
    Spring整合web开发
    IOC装配Bean(注解方式)
    IOC装配Bean(XML方式)
  • 原文地址:https://www.cnblogs.com/MashiroSky/p/5913640.html
Copyright © 2011-2022 走看看