zoukankan      html  css  js  c++  java
  • Codeforces Round #647 (Div. 2) D. Johnny and Contribution(BFS)

    题目链接:https://codeforces.com/contest/1362/problem/D

    题意

    有一个 $n$ 点 $m$ 边的图,每个结点有一个从 $1 sim n$ 的指定数字,每个结点染与它相邻的结点中最小的未染过的正整数,问是否存在某种顺序可以将所有结点染为指定数字,如果存在,输出染色顺序,否则输出 $-1$ 。

    题解

    边数最多为 $5 imes 10^5$ 而不是 $n^2$,所以逐点 $bfs$ 的复杂度最大为 $10^6$,模拟即可。

    代码

    #include <bits/stdc++.h>
    using namespace std;
    const int N = 5e5 + 100;
    
    vector<int> G[N];
    int t[N], p[N], vis[N];
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr); cout.tie(nullptr);
        int n, m; cin >> n >> m;
        for (int i = 0; i < m; i++) {
            int u, v; cin >> u >> v;
            G[u].push_back(v);
            G[v].push_back(u);
        }
        for (int i = 1; i <= n; i++)
            cin >> t[i];
        iota(p, p + n, 1);
        sort(p, p + n, [&] (int x, int y) {
            return t[x] < t[y];
        });
        bool ok = true;
        for (int i = 0; i < n; i++) {
            int u = p[i];
            set<int> st;
            for (auto v : G[u])
                if (vis[v])
                    st.insert(t[v]);
            vis[u] = true;
            if (st.size())
                if (*st.rbegin() == t[u] - 1 and st.size() == t[u] - 1) ; //判断 set 内是否为 1 2 3 ... t[u] - 1
                else ok = false;
            else if (t[u] != 1) ok = false;
        }
        if (ok)
            for (int i = 0; i < n; i++)
                cout << p[i] << " 
    "[i == n - 1];
        else
            cout << -1 << "
    ";
    }
  • 相关阅读:
    codeforce 896A
    CQH分治与整体二分
    [CQOI2011]动态逆序对
    codeforce Hello 2018 913F sol
    A*算法[k短路([SDOI2010]魔法猪学院)]
    bzoj3524 [POI2014]Couriers
    整体二分
    bzoj5016 [SNOI2017]一个简单的询问
    CF176E Archaeology
    bzoj4551 [TJOI2016&HEOI2016]树
  • 原文地址:https://www.cnblogs.com/Kanoon/p/13052903.html
Copyright © 2011-2022 走看看