题意:

输入一个二分图,用最少的颜色数给它的每条边染色,使得同一个顶点连的边中颜色互不相同。

输出至少需要的颜色数和任意一种染色方案。

分析:

证明不会,只说一下(偷瞄巨巨代码学到的)做法。

假设点的最大度数为\(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的更多相关文章

  1. CodeForces - 600F Edge coloring of bipartite graph

    Discription You are given an undirected bipartite graph without multiple edges. You should paint the ...

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

    大意:给定二分图, 求将边染色, 使得任意邻接边不同色且使用颜色种类数最少 最少颜色数即为最大度数, 要输出方案的话, 对于每一条边(u,v), 求出u,v能使用的最小的颜色$t0$,$t1$ 若t0 ...

  3. 【Educational Codeforces Round 36 D】 Almost Acyclic Graph

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 找到任意一个环. 然后枚举删掉其中的某一条边即可. (因为肯定要删掉这个环的,那么方法自然就是删掉其中的某一条边 (其它环,如果都包 ...

  4. Educational Codeforces Round 2

    600A - Extract Numbers    20171106 字符串处理题,稍微注意点细节就能水过 #include<stdlib.h> #include<stdio.h&g ...

  5. Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序

    Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序 [Problem Description] ​ 给你 ...

  6. Educational Codeforces Round 64 (Rated for Div. 2)题解

    Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人.需要注意以下就是正方 ...

  7. Educational Codeforces Round 21

    Educational Codeforces Round 21  A. Lucky Year 个位数直接输出\(1\) 否则,假设\(n\)十进制最高位的值为\(s\),答案就是\(s-(n\mod ...

  8. Educational Codeforces Round 43

    Educational Codeforces Round 43  A. Minimum Binary Number 显然可以把所有\(1\)合并成一个 注意没有\(1\)的情况 view code / ...

  9. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

随机推荐

  1. C#oracle还原imp实例

    C#来做oracle还原,调用oracle自带函数imp.exe时,需要注意的是:1.imp.exe 中fromuser 和touser两个关键字: fromuser与exp.exe中的owner对应 ...

  2. 基于JAVA的设计模式之代理模式

    概念 王宝强有一个经纪人叫宋喆,这个经纪人很吊,可以代理王宝强做一系列的事情,与粉丝活动.王宝强的微博维护.安排王宝强的行程以及什么什么等等.如果王宝强一个人做岂不是累死.通过这个代理人为王宝强节省了 ...

  3. 终于有人把P2P、P2C、O2O、B2C、B2B、C2C的区别讲透了!还有许多其它类别的类型分享

    平时在看招聘时,经常看到我们是什么B2C电子商务网站,但是一直不知是啥意思,今天在WEB开发者上面看到这篇文章,就是知道了个所以然,以记录分享. P2P.P2C .O2O .B2C.B2B. C2C, ...

  4. ajax请求拿到多条数据拼接显示在页面中

    首先我们拿到的了一坨Json数据 如下 然后通过ajax请求拿到数据 在ajax的success方法中处理和使用数据: 其中包括: 用eval处理这种数据 var outStr = eval('('+ ...

  5. Class 类

    在javascript 中应用类的概念 // javascript web applications 富应用开发 // 类库:生成类的地方:给所有的构造函数提供基础方法,如 extend, inclu ...

  6. vue2使用animate css

    先上几个链接 vue插件大集合:awesome-vue vue2插件: vue2-animate:vue2-animate vue2插件vue2-animateDEMO: vue2-animatede ...

  7. 为DataGridView控件实现复选功能

    实现效果: 知识运用: DataGridViewCheckBoxColumn类 实现代码: private class Fruit { public int Price { get; set; } p ...

  8. spring aop切面中获取代理bean的名字以及bean

    //切面中搞: Map<String , Object> map = (Map)ApplicationContextHelper.getBean(proceedingJoinPoint.g ...

  9. Django 模板函数

    Django 模板函数 在模板中的函数是只需要函数名,不用加括号,自动执行 在前端中的函数 不用加括号,函数自动执行 前端 {% for item in userinfo.keys %} <h3 ...

  10. string 的用法

    上次,我在" Anton And Danik "中为大家介绍了 string 的部分用法 今天,我就再来为大家介绍一下 string 的其他用法 : ( 有可能已经讲过了,不要介意 ...