Codeforces1362 D. Johnny and Contribution(模拟)

链接:http://codeforces.com/contest/1362/problem/D

解法:https://blog.csdn.net/weixin_44178736/article/details/106578025?fps=1&locationNum=2

#include

using namespace std;

const int N = 5e5+5;

template <typename T>inline void read(T& t){
    char c=getchar();t=0;
    int f=1;
    while(!isdigit(c)){
        if(c=='-')f=-1;
        c=getchar();
    }
    while(isdigit(c))t=t*10+c-48,c=getchar();
    t=f*t;
}

template <typename T,typename... Args> inline void read(T& t,Args&... args){
    read(t);read(args...);
}

struct Point{
    int id,w;
}p[N];

int mex[N],num[N];
vector<int>e[N];

bool cmp(const Point &a,const Point &b){
    if(a.w==b.w){
        return a.id<b.id;
    }
    return a.w<b.w;
}

int main(){
    int n,m,a,b;
    read(n,m);
    for(int i=1;i<=m;i++){
        read(a,b);
        e[a].push_back(b);
        e[b].push_back(a);
    }
    for(int i=1;i<=n;i++){
        read(p[i].w);
        p[i].id=i;
    }
    sort(p+1,p+1+n,cmp);
    for(int i=1;i<=n;i++){
        int tmp=p[i].id;
        int w=p[i].w;
        if(mex[tmp]!=w-1){
            puts("-1");
            return 0;
        }
        num[i]=tmp;
        for(auto v:e[tmp]){
            if(mex[v]==w-1){//排除已经排好的点
                mex[v]=w;
            }
        }
    }
    for(int i=1;i<=n;i++){
        printf("%d%c",num[i],i==n?'\n':' ');
    }
    return 0;
}

你可能感兴趣的:(codeforce)