【CF-1362】D. Johnny and Contribution

D. Johnny and Contribution

题意

给出n个顶点,m条边的图,现在要为这n个点填数字,规则如下。

顶点u填的数字 是 没有出现在和u相连的顶点中的最小数字。

如果和u相连的数字都没填数字,那么u就填1。

给出n个顶点期望填的数字,问是否存在一种填的顺序,使得n个顶点的数字和期望的数字一样。

题解

按照期望的数字从小往大填,同一个数字,填的顺序无所谓。

如果两个相连的顶点期望的数字一样,肯定不存在。

如果 u 上可以填比 期望数字 更小的值,肯定不存在。

除了这两种情况,都可以,按照期望数字从小到大输出顶点编号即可。

代码

#include<bits/stdc++.h>
#define pb push_back
using namespace std;
const int N=5e5+10;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
typedef unsigned long long ull;
typedef long long ll;

vector<int>vec[N],col[N],ans;
int arr[N],vis[N];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        vec[u].pb(v);
        vec[v].pb(u);
    }
    for(int i=1;i<=n;i++)
    {
        int tmp;
        scanf("%d",&tmp);
        arr[i]=tmp;
        col[tmp].pb(i);
        if(tmp>n)
        {
            puts("-1");
            return 0;
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int u:col[i])
        {
            int num=0;
            for(int v:vec[u])
            {
                if(vis[arr[v]]!=u&&arr[v]<=i)
                {
                    num++;
                    vis[arr[v]]=u;
                }
            }
            if(num==i-1&&vis[i]!=u)
                ans.pb(u);
            else
            {
                puts("-1");
                return 0;
            }
        }
    }
    for(int u:ans) printf("%d ",u);
    printf("
");
    return 0;
}
原文地址:https://www.cnblogs.com/valk3/p/13067606.html