HTML & JS
#include<bits/stdc++.h>
#define endll "\n";
#define all(x) (x).begin(), (x).end()
#define forr(it,x) for(auto it=x.begin();it!=x.end();it++)
#define for1(i,a,b) for(int i=a;i<b;i++)
#define for2(i,a,b) for(int i=a;i>=b;i--)
#define for3(i,a,b) for(long long int i=a;i<b;i++)
#define for4(i,a,b) for(long long int i=a;i>=b;i--)
#define pb push_back
using namespace std;
typedef pair<int,int> pp;
const int N=5001;
int vis[N];
vector<int> adj[N];
vector<vector<int>> a,b;
int dfs(int i,vector<int> &a1,vector<int> &b1){
    for(int j=0;j<adj[i].size();j++){
        int ii=adj[i][j];
        if(vis[ii]==0){
            if(vis[i]==1){
                vis[ii]=2;b1.pb(ii);
            }
            else{
                vis[ii]=1;a1.pb(ii);
            }
            dfs(ii,a1,b1);
        }
        else{
            if(vis[i]==vis[ii]){
                return 0;
            }
        }
    }
    return 1;
}
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);
        int n,m,n1,n2,n3;cin>>n>>m>>n1>>n2>>n3;
        int u,v;
        for1(i,0,m){
            cin>>u>>v;
            adj[u].pb(v);
            adj[v].pb(u);
        }
        for(int i=1;i<=n;i++){
            if(vis[i]==0){
                vector<int> a1,b1;
                vis[i]=1;a1.pb(i);
                if(dfs(i,a1,b1)==0){
                    cout<<"NO"<<endll;
                    return 0;
                }
                a.pb(a1);b.pb(b1);
            }
        }
        int r=a.size(),c=n2;
        int dp[r+1][c+1];
        dp[0][0]=1;
        for(int i=1;i<=c;i++){
            dp[0][i]=0;
        }
        int k=0;
        while(k<r){
            if(a[k].size()>0&&b[k].size()>0){
                break;
            }
            dp[k+1][0]=1;
            k++;
        }
        while(k<r){
            dp[k+1][0]=0;
            k++;
        }
        for1(i,1,r+1){
            for1(j,1,c+1){
                int j1=j-a[i-1].size();
                int j2=j-b[i-1].size();
                if(j1>=0&&dp[i-1][j1]){
                    dp[i][j]=1;
                }
                else if(j2>=0&&dp[i-1][j2]){
                    dp[i][j]=1;
                }
                else{
                    dp[i][j]=0;
                }
            }
        }
        if(dp[r][c]==0){
            cout<<"NO"<<endll;
            return 0;
        }
        int color[n+1];
        memset(color,0,sizeof(color));
        int i=r,j=c;
        while(i>0){
            if(dp[i-1][j-a[i].size()]==1){
                for(int ii=0;ii<a[i].size();ii++){
                    color[a[i-1][ii]]=2;
                }
                j-=a[i].size();
            }
            else{
                for(int ii=0;ii<b[i].size();ii++){
                    color[b[i-1][ii]]=2;
                }
                j-=b[i].size();
            }
            i--;
        }
        i=0;
        while(n1>0){
            if(color[i]==0){
                color[i]=1;
                n1--;
            }
            i++;
        }
        i=0;
        while(n3>0){
            if(color[i]==0){
                color[i]=3;
                n3--;
            }
            i++;
        }
        cout<<"YES"<<endll;
        string ans="";
        for1(i,0,n){
            ans+=('0'+color[i]);
        }
        cout<<ans<<endll;
    return 0;
}

Generated URL:

Output:

YES
112233
Report Bug