首页 > 其他分享 >41. CF-Graph Coloring

41. CF-Graph Coloring

时间:2023-03-16 12:56:19浏览次数:40  
标签:cnt Coloring vis int Graph CF color maxn dp

链接

先考虑怎样的图可以染色或者不能染色。容易发现,1 和 3 不能相邻,它们其实是等价的。那么能染色的必要条件就是所有的子图都是二分图。此外,每个二分图都取左部或者右部之一,顶点数量之和需要正好等于 \(n_2\)。

于是这个问题转化成了普通的背包 dp,转移的时候记录一下就好了。

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb push_back
#define endl '\n'
using ll = long long;
using pii = pair<int, int>;
const int maxn = 5e3 + 5;

vector<int> g[maxn];
int vis[maxn], cnt, col[maxn];
vector<pii> color;
int dp[maxn][maxn];
bool ok;
void dfs(int u, int f, bool flag) {
    if (!ok) return;
    if (vis[u] == -1) {
        vis[u] = cnt;
        col[u] = flag;
    } else if (col[u] == flag) {
    	return;
    } else {
        ok = false;
        return;
    }
    if (!flag) color[cnt].first++;
    else color[cnt].second++;
    for (auto v : g[u]) {
        if (v == f) continue;
        dfs(v, u, !flag);
    }
}

void solve() {
    int n, m;
    cin >> n >> m;
    int n1, n2, n3;
    cin >> n1 >> n2 >> n3;
    for (int i = 1, u, v; i <= m; ++i) {
        cin >> u >> v;
        g[u].pb(v), g[v].pb(u);
    }
    memset(vis, -1, sizeof(vis));
    ok = true;
    color.pb({ 0, 0 });
    for (int i = 1; i <= n; ++i) {
        if (vis[i] == -1 && ok) {
        	cnt++;
            color.pb({ 0, 0 });
            dfs(i, 0, 0);
        }
    }
    if (!ok) {
        cout << "NO\n" << endl;
        return;
    }
    dp[0][0] = 1;
    for (int i = 1; i <= cnt; ++i) {
        for (int j = 0; j <= n; ++j) {
            auto [L, R] = color[i];
            if (j >= L && dp[i - 1][j - L]) {
                dp[i][j] = -1;
            }
            if (j >= R && dp[i - 1][j - R]) {
                dp[i][j] = 1;
            }
        }
    }
    if (!dp[cnt][n2]) {
        cout << "NO\n" << endl;
        return;
    }
    cout << "YES\n";
    vector<int> ans(n + 1);
    vector<int> chk(cnt + 1);
    int tot = n2;
    for (int i = cnt; i >= 1; --i) {
    	auto [L, R] = color[i];
    	if (dp[i][tot] == 1) {
    		tot -= R;
    		chk[i] = 1;
    	} else {
    		tot -= L;
    		chk[i] = 0;
    	}
    }
    for (int i = 1; i <= n; ++i) {
    	int cur = vis[i]; 
    	ans[i] = (chk[cur] == col[i]) + 1;
    	if (ans[i] == 1) {
    		if (n1) n1--;
    		else ans[i] = 3;
    	}
    }
    for (int i = 1; i <= n; ++i)
        cout << ans[i];
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T = 1;
    // cin >> T;
    while (T--) {
        solve();
    }
}

标签:cnt,Coloring,vis,int,Graph,CF,color,maxn,dp
From: https://www.cnblogs.com/theophania/p/p41.html

相关文章

  • CF1286F Harry The Potter
    CF1286FHarryThePotter首先答案上界为\(n\),就是对每个点用一次操作1。那么我们现在的思维模式就是利用操作2来减少操作1的次数。不难发现,如果操作2的点之间......
  • 40. CF-Not So Simple Polygon Embedding
    链接题解里的几何做法很巧妙,这里记录一下。因为有\(2n\)条边,每条边对应的角度就是\(\dfrac{2\pi}{2n}\)。考虑对角线与底边平行的状态。顺时针或逆时针转动\(\dfrac......
  • CF590E Birthday
    \(\text{Solution}\)建出ACAM后利用fail树就可以确定子串关系了,如果建成有向图然后看问题,考虑最长反链等于最小链覆盖,那么就是求一个可重路径覆盖问题Floyd传递闭......
  • 图神经网络-图采样Graphsage代码实现
    一:为什么要图采样?二 Graphsage采样代码实践GraphSage的PGL完整代码实现位于https://github.com/PaddlePaddle/PGL/tree/main/examples/graphsage,本文实现一个简单......
  • CCF 2022-12
    一:试题编号:2022-12-1试题名称:现值计算时间限制:1.0s内存限制:512.0MB问题描述:样例输入20.05-200100100样例输出-14.059样例说明该项目当前支出 200 元,在接下来两年每年收......
  • [CF1788F] XOR, Tree, and Queries
    题目描述Youaregivenatreeof$n$vertices.Theverticesarenumberedfrom$1$to$n$.Youwillneedtoassignaweighttoeachedge.Lettheweight......
  • Xflow软件与传统CFD软件比较有哪些优势
    在应用传统的基于网格的方法来求解计算流体动力学(CFD)问题时,结果的可靠性高度依赖于网格质量。这样会导致工程师将大部分时间耗费在处理网格离散化上,而不是解决工程问题。此......
  • Xflow软件与传统CFD软件比较有哪些优势
    在应用传统的基于网格的方法来求解计算流体动力学(CFD)问题时,结果的可靠性高度依赖于网格质量。这样会导致工程师将大部分时间耗费在处理网格离散化上,而不是解决工程问题。此......
  • CF1736B 1200 *
    题意解析解析:每个a[i]是由b[i]和b[i+1]取最大公因数得出,所以对于每个b[j]来说应该既是a[j]的倍数,又是a[j-1]的倍数。现实在取的时候,可以取b[j]=lcm(a[j-1],a[j])。然......
  • Inductive Matrix Completion Based on Graph Neural Networks
    目录概符号说明IGMCEnclosingsubgraphNodelabelingGraph-levelGNNOptimization代码ZhangM.andChenY.Inductivematrixcompletionbasedongraphneuralnetwor......