• 7-9 红色警报 (25分)


    7-9 红色警报 (25分)

    #include<iostream>
    #include<cstdio>
    #include<vector>
    #include<algorithm>
    #include<cstring>
    #include<string>
    #include<map>
    #include<set>
    #include<queue>
    #include<iomanip>
    #include<stack>
    using namespace std;
    #define STDIN freopen("in.in", "r", stdin);freopen("out.out", "w", stdout);
    
    
    const int N = 505;
    int n, m;
    int fa[N];
    
    void init()
    {
        for (int i = 0; i < n; i++) fa[i] = i;
    }
    
    int find(int x)
    {
        if (x == fa[x]) return x;
        return fa[x] = find(fa[x]);
    }
    void unite(int a, int b)
    {
        int f1 = find(a);
        int f2 = find(b);
        fa[f1] = f2;
    }
    vector<pair<int,int> > edge;
    set<int> city;
    vector<string> ans;
    vector<int> city2;
    map<int, vector<int> > ma;
    string change(int j)
    {
        if (j == 0) return "0";
        string t = "";
        while (j)
        {
            t += j%10 + '0';
            j/=10;
        }
        reverse(t.begin(), t.end());
        return t;
    }
    
    int main()
    {
    //     STDIN
        cin >> n >> m;
        init();
        for (int i = 1; i <= m; i++) {
            int a, b;
            cin >> a >> b;
            edge.push_back({a, b});
            ma[a].push_back(b);
            ma[b].push_back(a);
        }
        int k;
        cin >> k;
        for (int i = 1; i <= k; i++)
        {
            int t;
            cin >> t;
            city.insert(t);
            city2.push_back(t);
        }
        if (k == n) ans.push_back("Game Over.");
        for (int i = 0; i < m; i++)
        {
            if (city.find(edge[i].first) == city.end() && city.find(edge[i].second) == city.end())
            {
                unite(edge[i].first, edge[i].second);
            }
        }
        reverse(city2.begin(), city2.end());
        for (int i = 0; i < k; i++)
        {
            city.erase(city2[i]);
            int cnt = 0;
            for (auto &j : ma[city2[i]])
            {
                if (city.find(j) == city.end())
                {
                    if (find(j) != find(city2[i]))
                    {
                        cnt++;        
                        unite(j, city2[i]);
                        
                    }
                }
            }
            if (cnt > 1){
                string s = "Red Alert: City ";
                string send = " is lost!";
                string smid = change(city2[i]);
                s += smid + send;
                ans.push_back(s);
            }
            else{
                string s = "City ";
                string send = " is lost.";
                string smid = change(city2[i]);
                s += smid + send;
                ans.push_back(s);
            }
        }
        reverse(ans.begin(), ans.end());
        for (auto &i : ans) cout << i << endl;
        
    }
  • 相关阅读:
    3变量
    2python的第一个程序
    1python语言的基本介绍
    6文字的剪切,复制,移动
    讨厌的Permission denied:adb访问手机目录时,怎么处理Permission denied问题
    adb命令整理(持续整理)
    loadrunner12下载、安装、认证、汉化
    操作系统中堆和栈的区别
    mos管选型
    Office 转 PDF
  • 原文地址:https://www.cnblogs.com/hulian425/p/14042417.html
Copyright © 2020-2023  润新知