zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    python面试
    Python 3.x--使用re模块,实现计算器运算实例
    Python 3.x--模块导入
    Python 3.x--序列化及反序列化
    Python 3.x--装饰器
    Python 3.x--函数的参数问题
    Python 3.x--文件seek、tell的用法
    Python 3.x--字典循环
    Python3.x--文件读写与list
    Python简介
  • 原文地址:https://www.cnblogs.com/Dont-Starve/p/13199660.html
Copyright © 2011-2022 走看看