【Henu ACM Round#19 F】Dispute

【链接】 我是链接,点我呀:)
【题意】

在这里输入题意

【题解】

这一题和这一题很像 ([链接](http://www.cnblogs.com/AWCXV/p/8377532.html) ) 会发现如果a[i]!=b[i]那么就按下i就好了。 然后改变和他相邻的点。 此后a[i]再也不可能和b[i]相同了。 (其他点无论怎么按b[i]只会变大)

但是这样直接暴力写会超时->O(N^2)。
则写一个队列。
处理和他相邻的点的时候。
如果发现a[y]==b[y]
就重新入队。
因为可以保证每个点最多操作一次。
所以复杂度就是O(n+m)的了。

【代码】

#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int N = 1e5;

int n,m,a[N+10],b[N+10];
vector<int> g[N+10];
queue<int> dl;

int main()
{
    #ifdef LOCAL_DEFINE
        freopen("rush_in.txt","r",stdin);
    #endif
    ios::sync_with_stdio(0),cin.tie(0);
    cin >> n >> m;
    for (int i = 1;i <= m;i++){
        int x,y;
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    for (int i = 1;i <= n;i++) cin>>a[i];
    for (int i = 1;i <= n;i++) b[i] =0;
    vector<int> v;v.clear();
    for (int i = 1;i <= n;i++)
        if (b[i]==a[i]){
            dl.push(i);
        }
    while (!dl.empty()){
        int x = dl.front();
        dl.pop();
        if (b[x]!=a[x]) continue;
        v.push_back(x);
        for (int y:g[x]){
            b[y]++;
            if (b[y]==a[y]){
                dl.push(y);
            }
        }
    }
    cout<<(int)v.size()<<endl;
    for (int x:v){
        cout<<x<<' ';
    }
    return 0;
}

原文地址:https://www.cnblogs.com/AWCXV/p/8398328.html