[] Dishes made HNOI2015

Face questions

https://www.luogu.org/problem/P3243

answer

(1) In all the prerequisites of limitation, the number 1 cuisine "try" priority production;
(2) meet all restrictions, No. 1 cuisine "try" premise preferentially produced, No. 2 dishes "try" priority production;
( 3) meet all restrictions, No. 1 and No. 2 dishes "try" lower priority context, No. 3 dishes "try" priority production;
(4) meet all restrictions, No. 1 and No. 2 and No. 3 dishes "try" under the premise of priority, No. 4 dishes "as far as possible" priority production;

We can think of lexicographical, but definitely not the lexicographically smallest.

An opposite example is assumed $ 2 to $ 4 to $ $, $ 3 to $ 1 to $ $,

The minimum order is $ dictionaries 2,3,1,4 $, but the subject of the request is $ 3,1,2,4 $.

So negated sequences lexicographically maximum can be.

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 100500
#define ri register int
using namespace std;

int n,m,ans[N],ru[N];
vector<int> to[N];
priority_queue<int> q;

inline int read() {
  int ret=0,f=0; char ch=getchar();
  while (ch<'0' || ch>'9') f|=(ch=='-'),ch=getchar();
  while (ch>='0' && ch<='9') ret*=10,ret+=(ch-'0'),ch=getchar();
  return f?-ret:ret;
}

int main(){
  int T=read();
  while (T--) {
    n=read(); m=read();
    for (ri i=1;i<=n;i++) to[i].clear(),ru[i]=0;
    for (ri i=1;i<=m;i++) {
      int u=read(),v=read();
      to[v].push_back(u); ru[u]++;
    }
    for (ri i=1;i<=n;i++) if (!ru[i]) q.push(i);
    int cc=0;
    while (!q.empty()) {
      int x=q.top(); q.pop();
      ans[++cc]=x;
      for (ri i=0;i<to[x].size();i++) {
        ru[to[x][i]]--;
        if (!ru[to[x][i]]) q.push(to[x][i]);
      }
    }
    if (cc!=n) {
      puts("Impossible!");
    }
    else {
      for (ri i=n;i>=1;i--) printf("%d ",ans[i]);
      puts("");
    }
  }
  return 0;
}

Guess you like

Origin www.cnblogs.com/shxnb666/p/11297792.html