zoukankan      html  css  js  c++  java
  • BZOJ4010 [HNOI2015]菜肴制作

    首先是贪心的思想。。。我们从小到大确定每个最前的位置

    建出反图,然后拓扑排序,每次找$deg=0$的点中最大的那个,于是就可以保证编号小的尽可能的在后面

    最后把顺序倒过来输出就好了。。。

      1 /**************************************************************
      2     Problem: 4010
      3     User: rausen
      4     Language: C++
      5     Result: Accepted
      6     Time:900 ms
      7     Memory:3936 kb
      8 ****************************************************************/
      9  
     10 #include <cstdio>
     11 #include <cstring>
     12 #include <queue>
     13  
     14 using namespace std;
     15 const int N = 1e5 + 5;
     16  
     17 struct edge {
     18     int next, to;
     19     edge(int _n = 0, int _t = 0) : next(_n), to(_t) {}
     20 } e[N];
     21  
     22 int n, m;
     23 int first[N], tot;
     24 int deg[N], q[N];
     25 int t[N], NOW, v[N];
     26 int ans[N];
     27 priority_queue <int> h;
     28  
     29 inline int read();
     30  
     31 inline void add_edge(int x, int y) {
     32     e[++tot] = edge(first[x], y), first[x] = tot;
     33     ++deg[y];
     34 }
     35  
     36 #define p q[l]
     37 #define y e[x].to
     38 bool check_circle() {
     39     static int i, l, r, x;
     40     for (r = 0, i = 1; i <= n; ++i) if (deg[i] == 0) q[++r] = i;
     41     for (l = 1; l <= r; ++l)
     42         for (x = first[p]; x; x = e[x].next)
     43             if (--deg[y] == 0) q[++r] = y;
     44     return r != n;
     45 }
     46  
     47 inline int bfs(int s) {
     48     static int l, r, x;
     49     ++NOW;
     50     q[l = r = 1] = s;
     51     for (; l <= r; ++l)
     52         for (x = first[p]; x; x = e[x].next) if (!v[y]) {
     53             ++deg[y];
     54             if (t[y] != NOW)
     55                 q[++r] = y, t[y] = NOW;
     56         }
     57 }
     58 #undef p
     59  
     60 inline void get(int s) {
     61     static int p, x;
     62     while (!h.empty()) h.pop();
     63     h.push(s), v[s] = 1;
     64     while (!h.empty()) {
     65         p = h.top(), h.pop();
     66         ans[++ans[0]] = p;
     67         for (x = first[p]; x; x = e[x].next) if (!v[y])
     68             if (--deg[y] == 0) h.push(y), v[y] = 1;
     69     }
     70 }
     71 #undef y
     72  
     73 int main() {
     74     int T, i, x, y;
     75     T = read();
     76     while (T--) {
     77         n = read(), m = read(), tot = NOW = 0;
     78         memset(first, 0, sizeof(first));
     79         memset(v, 0, sizeof(v));
     80         memset(deg, 0, sizeof(deg));
     81         memset(t, 0, sizeof(t));
     82         for (i = 1; i <= m; ++i) {
     83             x = read(), y = read();
     84             add_edge(y, x);
     85         }
     86         if (check_circle()) {
     87             puts("Impossible!");
     88             continue;
     89         }
     90         for (i = 1; i <= n; ++i) if (!v[i]) {
     91             bfs(i);
     92             get(i);
     93             for (; ans[0]; --ans[0]) printf("%d ", ans[ans[0]]);
     94         }
     95         puts("");
     96     }
     97     return 0;
     98 }
     99  
    100 inline int read() {
    101     static int x;
    102     static char ch;
    103     x = 0, ch = getchar();
    104     while (ch < '0' || '9' < ch)
    105         ch = getchar();
    106     while ('0' <= ch && ch <= '9') {
    107         x = x * 10 + ch - '0';
    108         ch = getchar();
    109     }
    110     return x;
    111 }
    View Code
    By Xs酱~ 转载请说明 博客地址:http://www.cnblogs.com/rausen
  • 相关阅读:
    微信助力活动表结构设计
    mysql的in查询分析
    PHP从数组中找到指定元素的位置
    Java程序使用Alpine Linux报错java.lang.NoClassDefFoundError: Could not initialize class org.xerial.snappy.Snappy解决
    Eclipse/Idea 代码格式化部分忽略
    修改Linux桌面高分屏下QT程序界面的缩放
    折腾linux随笔 之 关闭Budgie默认自动隐藏应用的菜单栏 与 Gnome系桌面应用菜单无内容解决
    Debian Buster 使用Lxde在界面中打开url提示错误解决
    Portainer容器可视化管理工具使用文档
    Lxde添加触摸板双击功能、防误触
  • 原文地址:https://www.cnblogs.com/rausen/p/4454588.html
Copyright © 2011-2022 走看看