Educational Codeforces Round 2 Edge coloring of bipartite graph
题意:
输入一个二分图,用最少的颜色数给它的每条边染色,使得同一个顶点连的边中颜色互不相同。
输出至少需要的颜色数和任意一种染色方案。
分析:
证明不会,只说一下(偷瞄巨巨代码学到的)做法。
假设点的最大度数为\(M\),那么至少需要\(M\)种颜色。
下面给出一种构造方法:
对于一条边\((u, \, v)\),分别找出对于\(u\)和\(v\)还没用到的颜色\(c_1\)和\(c_2\)。
- 如果\(c_1=c_2\),直接用颜色\(c_1\)给这条边染色就行了。
- 如果\(c_1 \neq c_2\),和匈牙利算法的思想一样,我们先给边\((u, \, v)\)染上颜色\(c_1\)。
对于之前和\(v\)染上颜色\(c_1\)的点t,我们用颜色\(c_2\)给边\((v, \, t)\)染色。
如果还有颜色\(c_2\)和\(t\)冲突,持续这个过程,继续把新的颜色腾出来。
这种做法还能顺便解决掉UVa 10615
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1000 + 10;
const int maxm = 100000 + 10;
int col[2][maxn][maxn];
int id[maxn][maxn];
int ans[maxm];
void dfs(int p, int u, int v, int c1, int c2) {
int t = col[p^1][v][c1];
col[p][u][c1] = v; col[p^1][v][c1] = u;
if(!t) {
col[p^1][v][c2] = 0;
return ;
}
dfs(p^1, v, t, c2, c1);
}
int main()
{
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
int cnt = 0;
for(int i = 1; i <= k; i++) {
int u, v; scanf("%d%d", &u, &v);
id[u][v] = i;
int c1 = 1, c2 = 1;
while(col[0][u][c1]) c1++;
while(col[1][v][c2]) c2++;
cnt = max(cnt, max(c1, c2));
if(c1 == c2) {
col[0][u][c1] = v;
col[1][v][c1] = u;
} else {
dfs(0, u, v, c1, c2);
}
}
printf("%d\n", cnt);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= cnt; j++)
if(col[0][i][j])
ans[id[i][col[0][i][j]]] = j;
for(int i = 1; i <= k; i++) printf("%d ", ans[i]);
printf("\n");
return 0;
}
Educational Codeforces Round 2 Edge coloring of bipartite graph的更多相关文章
- CodeForces - 600F Edge coloring of bipartite graph
Discription You are given an undirected bipartite graph without multiple edges. You should paint the ...
- Edge coloring of bipartite graph CodeForces - 600F (二分图染色)
大意:给定二分图, 求将边染色, 使得任意邻接边不同色且使用颜色种类数最少 最少颜色数即为最大度数, 要输出方案的话, 对于每一条边(u,v), 求出u,v能使用的最小的颜色$t0$,$t1$ 若t0 ...
- 【Educational Codeforces Round 36 D】 Almost Acyclic Graph
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 找到任意一个环. 然后枚举删掉其中的某一条边即可. (因为肯定要删掉这个环的,那么方法自然就是删掉其中的某一条边 (其它环,如果都包 ...
- Educational Codeforces Round 2
600A - Extract Numbers 20171106 字符串处理题,稍微注意点细节就能水过 #include<stdlib.h> #include<stdio.h&g ...
- Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序
Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序 [Problem Description] 给你 ...
- Educational Codeforces Round 64 (Rated for Div. 2)题解
Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人.需要注意以下就是正方 ...
- Educational Codeforces Round 21
Educational Codeforces Round 21 A. Lucky Year 个位数直接输出\(1\) 否则,假设\(n\)十进制最高位的值为\(s\),答案就是\(s-(n\mod ...
- Educational Codeforces Round 43
Educational Codeforces Round 43 A. Minimum Binary Number 显然可以把所有\(1\)合并成一个 注意没有\(1\)的情况 view code / ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
随机推荐
- ubuntu中执行定时任务crontab
今天研究了下ubuntu里的crontab内置指令.这是设置定时执行脚本任务的指令,我先测试了下最基础的执行. 第一次使用crontab 时,会出现 no crontab for root - usi ...
- WebStorm技巧-常用快捷键
Ctrl+/ 或 Ctrl+Shift+/ 注释(// 或者/*-*/ ) Shift+F6 重构-重命名 Ctrl+X 删除行 Ctrl+D 复制行 Ctrl+G 查找行 Ctrl+Shift+ ...
- 在 WPF 中的线程
线程处理使程序能够执行并发处理,以便它可以做多个操作一次.节省开发人员从线程处理困难的方式,设计了 WPF (窗口演示文稿基金会).这篇文章可以帮助理解线程在 WPF 中的正确用法. WPF 内部线程 ...
- springMVC数据校验与单文件上传
spring表单标签: <fr:from/> 渲染表单元素 <fr:input/>输入框组件 <fr:password/>密码框组件标签 & ...
- Windows中将nginx添加到服务
下载安装nginx http://nginx.org/en/download.html 下载后解压到C盘 C:\nginx-1.14.0 添加服务 需要借助"Windows Service ...
- ArcGIS for Server 10.3.X 新型紧凑型缓存的解读和应用
早在2010年年底,牛魔王中王在其博客空间牛魔王的作坊中对ArcGIS 10中推出的紧凑型缓存格式进行了详细的解读,详见<ArcGIS 切片缓存紧凑文件格式分析与使用>.紧随着的4年时间里 ...
- shell命令cut
cut命令用来操作字符串,可以理解为剪切字符串的工具: cut有两种用法: 1.剪切字符串中的单个字符(-c参数) 例如: str=abcdef echo $str | cut -c 1-1 输出:a ...
- 同步Windows 10与MIUI 9来完成你的日程安排
我们都知道,outlook可以有效管理你的日程.而MIUI 9在负一屏上全新改版了界面,变得更加全面人性化.接下来我将展示几种方法,让你的Windows 10与MIUI 9协同工作. 一.outloo ...
- Distributed Transaction Coordinator(DTC)一些问题的解决方法
有时运行某个程序或者安装SQL Server时报错. 错误信息: 事务管理器不可用.(从 HRESULT 异常: 0x8004D01B) 启动服务Distributed Transaction Coo ...
- BZOJ 1137: [POI2009]Wsp 岛屿 半平面交
1137: [POI2009]Wsp 岛屿 Time Limit: 10 Sec Memory Limit: 162 MBSec Special JudgeSubmit: 165 Solved: ...