CF549B Looksery Party 题解

给定一张\(n\)个点的有向图,不一定联通,每个点都有一个自环.对于每个点\(i\)给出\(a_i\),试构造一种选点方案使得每个点的入度都不等于\(a_i\).

容易发现,对于\(a_i=0\)的点,我们必须将它们选进去.选入之后我们将该点所指向的点\(a_i\)减一,实际上相当于比较方便地统计当前选点产生的对其他点的入度.每次选出\(a_i=0\)的点,重复上述操作,正确性显然.同时也说明并不存在无解的情况.

#include <iostream>
#include <cstdio>
#include <assert.h>
using namespace std;
const int N = 105;
struct edge
{
    int to, next;
} e[N * N];
int cnt, head[N];
void add(int u, int v)
{
    e[++cnt].to = v;
    e[cnt].next = head[u];
    head[u] = cnt;
}
int n;
int a[N], p[N], ans;
int main()
{
    ios::sync_with_stdio(false);
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        string s;
        cin >> s;
        for (int j = 0; j < s.length(); j++)
            if (s[j] == '1')
                add(i, j + 1);
    }
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    while (1)
    {
        bool flag = false;
        for (int i = 1; i <= n; i++)
            if (a[i] == 0 && !p[i])
            {
                ans++;
                p[i] = 1;
                flag = true;
                for (int j = head[i]; j; j = e[j].next)
                    a[e[j].to]--;
                break;
            }
        if (!flag)
            break;
    }
    cout << ans << endl;
    for (int i = 1; i <= n; i++)
        if (p[i])
            cout << i << " ";
    cout << endl;
}

posted @ 2021-11-07 15:32  Kinuhata  阅读(27)  评论(0)    收藏  举报