• Codeforces-1367-E-DeadLee


    题意:

    Lee有 n 种食物,每种食物有 (w_i) 份,有 m 个朋友,每个人喜欢两种食物(每个人不会喜欢相同的两种食物)。

    如果轮到某个朋友吃了,他会吃他喜欢的两种食物各一份(如果只有一种,那就吃一种)。

    如果他没能吃到任何食物,他就会吃Lee。问Lee能否存活,能存活还要输出朋友的出场顺序

    思路:

    第 i 种食物有 (w_i) 份,假如有 (s_i) 人要吃这种食物,

    • 如果 (w_j le s_j) 就可以把喜欢吃第 i 种食物的这些人放到最后,然后把这些人喜欢吃的另外一种食物 j 不考虑,优先给其他人吃。这样 (s_j) 就会变小,会有新的 (w_j le s_j), 如此循环,直到安排完所有人,Lee活下来,否则Lee 不能存活。
    • 如果不存在 (w_j le s_j) ,则Lee dead.

    AC代码:

    #include<bits/stdc++.h>
    using namespace std;
    const int MAXN = 200005;
    vector<int> ans;
    vector<pair<int, int> > v[MAXN];
    queue<int> q;
    int w[MAXN], in_ans[MAXN], vis[MAXN];
    int main() {
        int n, m; cin >> n >> m;
        for (int i = 1; i <= n; ++i) cin >> w[i];
        for (int i = 1; i <= m; ++i) {
            int x, y; cin >> x >> y;
            v[x].push_back({ y, i }); w[x]--;
            v[y].push_back({ x, i }); w[y]--;
        }
        for (int i = 1; i <= n; ++i) {
            if (w[i] >= 0) {
                q.push(i);vis[i] = 1;
            }
        }
        while (!q.empty()) {
            int food = q.front();q.pop();
            for (auto t : v[food]) {
                if (!in_ans[t.second]) {
                    ans.push_back(t.second);
                    in_ans[t.second] = 1;
                }
                w[t.first]++;
                if (!vis[t.first] && w[t.first] >= 0) {
                    q.push(t.first);vis[t.first] = 1;
                }
            }
        }
        if ((int)ans.size() == m) {
            cout << "ALIVE" << endl;
            for (int i = ans.size() - 1; i >= 0; --i)
                cout << ans[i] << " ";
        }
        else
            cout << "DEAD";
        cout << endl;
        return 0;
    }
    
  • 相关阅读:
    swagger配置
    windows下安装redis
    Redis在windows下安装过程
    jenkins安装部署全过程
    MySQL表名不区分大小写的设置方法
    Linux常用命令
    java 执行redis的部分方法
    把jar包加入本地maven库内
    Java 枚举7常见种用法
    【转】每天一个linux命令(1):ls命令
  • 原文地址:https://www.cnblogs.com/Dont-Starve/p/13199660.html
Copyright © 2020-2023  润新知