zoukankan      html  css  js  c++  java
  • 20200725T1 【NOIP2015模拟10.27】挑竹签

    Description

    挑竹签

    挑竹签——小时候的游戏
    夏夜,早苗和诹访子在月光下玩起了挑竹签这一经典的游戏。
    挑竹签,就是在桌上摆上一把竹签,每次从最上层挑走一根竹签。如果动了其他的竹签,就要换对手来挑。在所有的竹签都被挑走之后,谁挑走的竹签总数多,谁就胜了。
    身为神明的诹访子自然会让早苗先手。为了获胜,早苗现在的问题是,在诹访子出手之前最多能挑走多少竹签呢?
    为了简化问题,我们假设当且仅当挑最上层的竹签不会动到其他竹签。

    输入文件mikado.in。
    第一行输入两个整数n,m, 表示竹签的根数和竹签之间相压关系数。
    第二行到m+1 行每行两个整数u,v,表示第u 根竹签压住了第v 根竹签。

    输出文件mikado.out。
    一共一行,一个整数sum,表示最多能拿走sum 根竹签。

    6 6
    1 2
    2 3
    3 1
    4 3
    4 5
    6 5

    3
    样例解释:
    一共有6 根竹签,其中1 压住2,2 压住3,3 压住1,4 压住3 和5,6 压住5。最优方案中,我们可以依次挑走4、6、5 三根竹签。而剩下的三根相互压住,都无法挑走。所以最多能挑走3 根竹签。

    对于20% 的数据,有1<= n,m<= 20。
    对于40% 的数据,有1 <= n,m <= 1 000。
    对于100% 的数据,有1 <= n,m <= 1 000 000。

    solution

    显然拓扑

    code

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<queue>
    #include<vector>
    #include<stack>
    #include<set>
    #include<deque>
    #include<map>
    using namespace std;
    
    template <typename T> void read(T &x) {
        x = 0; int f = 1; char c;
        for (c = getchar(); c < '0' || c > '9'; c = getchar()) if (c == '-') f = -f;
        for (; c >= '0' && c <= '9'; c = getchar()) x = 10 * x + c - '0' ;
        x *= f;
    }
    template <typename T> void write(T x){
        if (x < 0) putchar('-'), x = -x;
        if (x > 9) write(x / 10);
        putchar(x % 10 + '0');
    }
    template <typename T> void writeln(T x) { write(x); putchar('
    '); }
    template <typename T> void writesn(T x) { write(x); putchar(' '); }
    
    #define ll long long
    #define inf 1234567890
    #define next net
    #define P 2147483647
    #define N 1000010
    #define mid ((l+r)>>1)
    #define lson (o<<1)
    #define rson (o<<1|1)
    #define R register
    #define debug puts("zxt")
    
    int n , m , ans;
    int cut, head[N ], next[N ], ver[N ], ru[N ];
    
    inline void add(int x, int y)
    {
        ver[++cut] = y; next[cut] = head[x]; head[x] = cut; ru[y]++;
    }
    inline void topu()
    {
        queue<int>q;
        for(R int i = 1; i <= n; i++) if(!ru[i]) q.push(i), ans++;
        while(!q.empty())
        {
            int x = q.front();
            q.pop();
            for(R int i = head[x]; i; i = next[i])
            {
                int y = ver[i];
                ru[y]--;
                if(!ru[y]) q.push(y), ans++; 
            }
        }
        writeln(ans);
    }
    signed main()
    {
        //freopen("mikado.in","r",stdin);
        //freopen("mikado.out","w",stdout);
        read(n); read(m );
        for(R int i = 1, x, y; i <= m ; i++)
        {
            read(x); read(y);
            add(x, y);
        } 
        topu();
        return 0;
    }
  • 相关阅读:
    uvm_misc——杂货铺(miscellaneous)
    23种设计模式及其应用场景
    Hadoop TextInputFormat源码分析
    java字符串替换函数高效实现
    hadoop HA 之 QJM
    中文分词词性对照表
    计算两个字符串编辑距离
    java 图片处理
    zookeeper数据迁移
    正则表达式
  • 原文地址:https://www.cnblogs.com/e-e-thinker/p/13376050.html
Copyright © 2011-2022 走看看