2100分 E. Graph Coloring 二分图染色+背包+记录路径+贪心构造

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define mem(a, b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define p_queue priority_queue
#define DBG printf("this is a input\n")
#define fi first
#define se second
#define mk(a, b) make_pair(a,b)
#define pb(a) push_back(a)
#define CLOSE ios::sync_with_stdio(0); cin.tie(0)
int n , m;
int n1 , n2 , n3;
vector <int> edge[5005];
vector <pair<int,int> > a;
vector <int> vcc[5005];
int sec[5005];
int color[5005], ok , cntx , cnty, tot = -1;
int dp[5005][5005];
void dfs(int root)
{
    vcc[tot].pb(root);
    if(ok)
        return ;
    for (int i = 0 ; i < edge[root].size() ; i ++)
    {
        int v = edge[root][i];
        if(color[v] == 0)
        {
            color[v] = -color[root];
            dfs(v);
            if(color[v] == 1)
                cntx ++;
            else
                cnty ++;
        }
        else
        {
            if(color[v] == color[root])
            {
                ok = 1;
                return ;
            }
        }
    }
}
void Deal()
{
    for (int i = 1 ; i <= n ; i ++)
    {
        if(color[i] == 0)
        {
            ++ tot;
            ok = cnty = 0;
            cntx = 1;
            color[i] = 1;
            dfs(i);
            a.pb(mk(cntx,cnty));
            if(ok == 1)
            {
                cout << "NO" << endl;
                exit(0);
            }
        }
    }
}
int pre[5005][5005];
int ans[5005];
int main(void)
{
    CLOSE;
    cin >> n >> m;
    cin >> n1 >> n2 >> n3;
    for (int i = 1 ; i <= m ; i ++)
    {
        int u , v ;
        cin >> u >> v;
        edge[u].pb(v) , edge[v].pb(u);
    }
    dp[0][0] = 1;
    Deal();
    for (int i = 1 ; i <= a.size();  i ++)
    {
        for (int j = a[i-1].fi ; j <= n2 ; j ++)
        {
            dp[i][j] |= dp[i - 1][j - a[i - 1].fi];
            if (dp[i][j])
                pre[i][j] = 1;
        }
        for (int j = a[i-1].se ; j <= n2 ; j ++)
        {
            dp[i][j] |= dp[i - 1][j - a[i - 1].se];
            if (dp[i][j] && !pre[i][j])
                pre[i][j] = 2;
        }
    }
    if(dp[a.size()][n2])
    {
        cout << "YES" << endl;
        int x = a.size() , y = n2;
        while (x && y)
        {
            while(!pre[x][y]) x -= 1;
            sec[x-1] = pre[x][y];
            if(pre[x][y] == 1)
                y -= a[x-1].fi;
            else
                y -= a[x-1].se;
            x -= 1;
        }
        for (int i = 0 ; i < a.size() ; i ++)
        {
            if(sec[i] == 1)
            {
                for (int j = 0 ; j < vcc[i].size() ; j ++)
                {
                    if(color[vcc[i][j]] == 1)
                        ans[vcc[i][j]] = 2;
                    else
                    {
                        if(n1)
                            ans[vcc[i][j]] = 1, n1 -= 1;
                        else
                            ans[vcc[i][j]] = 3, n3 -= 1;
                    }
                }
            }
            else if (sec[i] == 2)
            {
                for (int j = 0 ; j < vcc[i].size() ; j ++)
                {
                    if(color[vcc[i][j]] == -1)
                        ans[vcc[i][j]] = 2;
                    else
                    {
                        if(n1)
                            ans[vcc[i][j]] = 1, n1 -= 1;
                        else
                            ans[vcc[i][j]] = 3, n3 -= 1;
                    }
                }
            }
            else
            {
                for (int j = 0 ; j < vcc[i].size() ; j ++)
                {
                    if(n1)
                        ans[vcc[i][j]] = 1, n1 -= 1;
                    else
                        ans[vcc[i][j]] = 3, n3 -= 1;
                }
            }
        }
        for (int i = 1 ; i <= n ; i ++)
            cout << ans[i];
        cout << endl;
    }
    else
        cout << "NO" << endl;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值