Edge coloring of bipartite graph CodeForces - 600F (二分图染色)

大意:给定二分图, 求将边染色, 使得任意邻接边不同色且使用颜色种类数最少

最少颜色数即为最大度数, 要输出方案的话, 对于每一条边(u,v), 求出u,v能使用的最小的颜色$t0$,$t1$

若t0=t1, 直接染就行不会有冲突, 否则就染为t0, 这样的话可能产生冲突, 就将冲突的边换成t1, 依次递归下去

由于二分图的性质, 最终一定可以找到一条边不冲突

#include <iostream>
#include <algorithm>
#include <cstdio>
#define REP(i,a,n) for(int i=a;i<=n;++i)
using namespace std;

const int N = 1e3+10, INF = 0x3f3f3f3f;
int a, b, m;
int g[N][N];
int f[2][N][N], c[N*N];

void dfs(int a, int b, int x, int y, int now, int pre) {
	int to=f[b][y][now];
	f[a][x][now]=y,f[b][y][now]=x;
	if (!to) f[b][y][pre]=0;
	else dfs(b,a,y,to,pre,now);
}

int main() {
	scanf("%d%d%d", &a, &b, &m);
	int ans = 0;
	REP(i,1,m) {
		int u, v;
		scanf("%d%d", &u, &v);
		g[u][v] = i;
		int t0=1,t1=1;
		while (f[0][u][t0]) ++t0;
		while (f[1][v][t1]) ++t1;
		ans = max(ans, max(t0,t1));
		if (t0==t1) f[0][u][t0] = v, f[1][v][t0] = u;
		else dfs(0,1,u,v,t0,t1);
	}
	REP(i,1,a) REP(j,1,ans) if (f[0][i][j]) {
		c[g[i][f[0][i][j]]]=j;
	}
	printf("%d\n", ans);
	REP(i,1,m) printf("%d ", c[i]);
	puts("");
}

原文地址:https://www.cnblogs.com/uid001/p/10421728.html

时间: 2024-08-29 17:29:24

Edge coloring of bipartite graph CodeForces - 600F (二分图染色)的相关文章

HDU 5313 Bipartite Graph(二分图染色+01背包水过)

Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants to make the graph become a complete bipartite graph with most edges by adding some extra edges. Soda needs you to tell him the maximum number of edges

HDU 5313——Bipartite Graph——————【二分图+dp+bitset优化】

Bipartite Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 840    Accepted Submission(s): 285 Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he w

Codeforces 1093D. Beautiful Graph【二分图染色】+【组合数】

<题目链接> 题目大意: 给你一个无向图(该无向图无自环,且无重边),现在要你给这个无向图的点加权,所加权值可以是1,2,3.给这些点加权之后,要使得任意边的两个端点权值之和为奇数,问总共有多少种可能?结果mod 998244353. 解题分析: 整张图的所有顶点赋权之后,一定分为奇.偶两部分点集,并且,要想使的该图满足条件,任意边的两个端点的奇偶性应该是不同的,所以我们可以用DFS对图进行二分图染色,将图分为两个部分,需要注意的是,该图未必连通.然后就是DFS的过程中,如果下一个点已经染过色

hdu 5313 Bipartite Graph 完全二分图 深搜 bitset应用

Bipartite Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 577    Accepted Submission(s): 154 Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he

hdu 5313 Bipartite Graph(dfs染色 或者 并查集)

Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants to make the graph become a complete bipartite graph with most edges by adding some extra edges. Soda needs you to tell him the maximum number of edges

HDU5313——DP+vector——Bipartite Graph

Soda has a bipartite graph with n vertices and m undirected edges. Now he wants to make the graph become a complete bipartite graph with most edges by adding some extra edges. Soda needs you to tell him the maximum number of edges he can add. Note: T

Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)

Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are given a directed graph consisting of n vertices and m edges (each edge is directed, so it can

hdu5313 Bipartite Graph

题意描述: 有一个n个点m条边的二分图,通过加边使得这张图变成一个边数最多的完全二分图. 最多能够新加多少条边. 注意重边是不允许的. 解题思路: 1.先对二分图染色(dfs),统计二分图中每个连通块(注意:这个二分图并不一定连通)中黑色和白色的数量(黑.白是相对的,不同连通块之间的黑.白没有联系): 2.从每个连通块中选出黑或白的数量作为整个二分图中白色的那组,根据题目描述我们只需要找到怎样分组才能使完全二分图的边数最多(显然对于总共n个顶点的 二分图,如果两组顶点各一半时能使总的边数最大),

CodeForces - 862B Mahmoud and Ehab and the bipartiteness(二分图染色)

题意:给定一个n个点的树,该树同时也是一个二分图,问最多能添加多少条边,使添加后的图也是一个二分图. 分析: 1.通过二分图染色,将树中所有节点分成两个集合,大小分别为cnt1和cnt2. 2.两个集合间总共可以连cnt1*cnt2条边,给定的是一个树,因此已经连了n-1条边,所以最多能连cnt1*cnt2-(n-1)条边. 3.注意输出. #include<cstdio> #include<cstring> #include<cstdlib> #include<