bzoj4010[HNOI2015]菜肴制作

http://www.lydsy.com/JudgeOnline/problem.php?id=401

这道题和NOI2010 航空管制非常像。

我们先建立拓扑图,对于如果菜a必须在菜b前,那么连有向边b->a,并求出点的入度。

将所有入度为0的点放在一个优先队列里,按菜的编号从大到小排序。

我们从后往前考虑菜的顺序。

取出优先队列中编号最大的点,作为最后一个菜,并删去拓扑图中与他相连的边,如果有新的点的入度变成0,继续加入优先队列里。

重复操作次即可。

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<utility>
#include<set>
#include<bitset>
#include<vector>
#include<functional>
#include<deque>
#include<cctype>
#include<climits>
#include<complex>
//#include<bits/stdc++.h>适用于CF,UOJ,但不适用于poj
 
using namespace std;

typedef long long LL;
typedef double DB;
typedef pair<int,int> PII;
typedef complex<DB> CP;

#define mmst(a,v) memset(a,v,sizeof(a))
#define mmcy(a,b) memcpy(a,b,sizeof(a))
#define fill(a,l,r,v) fill(a+l,a+r+1,v)
#define re(i,a,b)  for(i=(a);i<=(b);i++)
#define red(i,a,b) for(i=(a);i>=(b);i--)
#define ire(i,x) for(typedef(x.begin()) i=x.begin();i!=x.end();i++)
#define fi first
#define se second
#define m_p(a,b) make_pair(a,b)
#define p_b(a) push_back(a)
#define SF scanf
#define PF printf
#define two(k) (1<<(k))

template<class T>inline T sqr(T x){return x*x;}
template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;}
template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;}

inline int sgn(DB x){if(abs(x)<1e-9)return 0;return(x>0)?1:-1;}
const DB Pi=acos(-1.0);

int gint()
  {
        int res=0;bool neg=0;char z;
        for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
        if(z==EOF)return 0;
        if(z=='-'){neg=1;z=getchar();}
        for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
        return (neg)?-res:res; 
    }
LL gll()
  {
      LL res=0;bool neg=0;char z;
        for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
        if(z==EOF)return 0;
        if(z=='-'){neg=1;z=getchar();}
        for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
        return (neg)?-res:res; 
  }

const int maxn=100000;

int n,m;
int now,first[maxn+100];
struct Tedge{int v,next;}edge[maxn+100];
int du[maxn+100];

void addedge(int u,int v){now++;edge[now].v=v;edge[now].next=first[u];first[u]=now;}

int cnt,out[maxn+100];
priority_queue<int>Q;

int main()
  {
      freopen("bzoj4010.in","r",stdin);
      freopen("bzoj4010.out","w",stdout);
      for(int Case=gint();Case;Case--)
      {
      int i;
      n=gint();m=gint();
      now=-1;mmst(first,-1);mmst(du,0);
      re(i,1,m){int u=gint(),v=gint();addedge(v,u);du[u]++;}
      while(!Q.empty())Q.pop();
      cnt=0;
      re(i,1,n)if(du[i]==0)Q.push(i);
      while(!Q.empty())
        {
            int u=Q.top(),v;Q.pop();
            out[++cnt]=u;
            for(i=first[u],v=edge[i].v;i!=-1;i=edge[i].next,v=edge[i].v)
              {
                  du[v]--;
                  if(du[v]==0)Q.push(v);
              }
        }
      if(cnt<n)
        PF("Impossible!\n");
      else
        {
            red(i,cnt,1)PF("%d ",out[i]);
            PF("\n");
        }
      }
      return 0;
  }
View Code

 

posted @ 2015-11-11 09:29  maijing  阅读(194)  评论(0编辑  收藏  举报