CodeForces - 600F Edge coloring of bipartite graph
Discription
You are given an undirected bipartite graph without multiple edges. You should paint the edges of graph to minimal number of colours, so that no two adjacent edges have the same colour.
Input
The first line contains three integers a, b, m (1 ≤ a, b ≤ 1000, 0 ≤ m ≤ 105), a is the size of the first part, b is the size of the second part, m is the number of edges in the graph.
Each of the next m lines contains two integers x, y (1 ≤ x ≤ a, 1 ≤ y ≤ b), where x is the number of the vertex in the first part and y is the number of the vertex in the second part. It is guaranteed that there are no multiple edges.
Output
In the first line print integer c — the minimal number of colours. The second line should contain m integers from 1 to c — the colours of the edges (in the order they appear in the input).
If there are several solutions, you can print any one of them.
Example
4 3 5
1 2
2 2
3 2
4 1
4 3
3
1 2 3 1 2 一种全新的题目类型:给二分图染色使得任意两个邻接的边颜色不同,求最少需要的颜色并输出任意一种方案。 如果仅仅需要输出最小颜色数的话,根据二分图没有奇环的性质是很容易yy,答案就是度数最大的点的度数,但是还要输出方案,,,这有点gg。
假设我们已经处理好了前i-1条边,现在要加入第i条边(左右连接的顶点分别是u,v,并且其没有出现过的颜色最小分别是C1,C2)。我们先把这条边染成c1色,
但是这样V顶点可能就有两个颜色c1的边了。如果v顶点原来还有一条颜色是c1的边的话,那么就把它染成c2色,再进入另一个端点继续递归。。 我们发现这种操作就是把(u,v)这条边加进原来一条 由颜色C1,C2交替出现的路径中去,其中v是原来的路径端点之一,如果(u,v)和原来v的边颜色冲突的话,
就把原来路径上的所有邻接边颜色互换一下,所以肯定有解。 现在我们唯一需要证明的是 这种策略可以使得 所有出现的边的最大颜色编号 就是 度数最大的点的度数。
考虑我们都是贪心的找需要加入的边的两个端点 没有出现过的最小颜色,所以假设出现了编号大于度数最大的点度数 的边,那么说明这条边的某个顶点的度数一定
大于度数最大的点的度数,但是这显然是矛盾的,所以不成立。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1005;
int G[maxn][maxn],C[maxn*100];
int A,B,m,dy[2][maxn][maxn],ans;
int u[maxn*100],v[maxn*100],c[2]; void dfs(int a,int b,int x,int y,int now,int pre){
if(now==pre){ dy[a][x][now]=y,dy[b][y][now]=x; return;} int to=dy[b][y][now];
dy[a][x][now]=y,dy[b][y][now]=x; if(!to) dy[b][y][pre]=0;
else dfs(b,a,y,to,pre,now);
} int main(){
scanf("%d%d%d",&A,&B,&m);
for(int i=1;i<=m;i++){
scanf("%d%d",u+i,v+i);
G[u[i]][v[i]]=i;
} for(int i=1;i<=m;i++){
c[0]=c[1]=1;
while(dy[0][u[i]][c[0]]) c[0]++;
while(dy[1][v[i]][c[1]]) c[1]++;
ans=max(ans,max(c[0],c[1]));
dfs(0,1,u[i],v[i],c[0],c[1]);
} for(int i=1;i<=A;i++)
for(int j=1;j<=ans;j++) if(dy[0][i][j]) C[G[i][dy[0][i][j]]]=j; printf("%d\n",ans);
for(int i=1;i<=m;i++) printf("%d ",C[i]);
return 0;
}
CodeForces - 600F Edge coloring of bipartite graph的更多相关文章
- Edge coloring of bipartite graph CodeForces - 600F (二分图染色)
大意:给定二分图, 求将边染色, 使得任意邻接边不同色且使用颜色种类数最少 最少颜色数即为最大度数, 要输出方案的话, 对于每一条边(u,v), 求出u,v能使用的最小的颜色$t0$,$t1$ 若t0 ...
- Educational Codeforces Round 2 Edge coloring of bipartite graph
题意: 输入一个二分图,用最少的颜色数给它的每条边染色,使得同一个顶点连的边中颜色互不相同. 输出至少需要的颜色数和任意一种染色方案. 分析: 证明不会,只说一下(偷瞄巨巨代码学到的)做法. 假设点的 ...
- hdu 5313 Bipartite Graph(dfs染色 或者 并查集)
Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...
- HDU 5313——Bipartite Graph——————【二分图+dp+bitset优化】
Bipartite Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- HDU 5313 Bipartite Graph(二分图染色+01背包水过)
Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...
- 二分图点染色 BestCoder 1st Anniversary($) 1004 Bipartite Graph
题目传送门 /* 二分图点染色:这题就是将点分成两个集合就可以了,点染色用dfs做, 剩下的点放到点少的集合里去 官方解答:首先二分图可以分成两类点X和Y, 完全二分图的边数就是|X|*|Y|.我们的 ...
- Learning Query and Document Similarities from Click-through Bipartite Graph with Metadata
读了一篇paper,MSRA的Wei Wu的一篇<Learning Query and Document Similarities from Click-through Bipartite Gr ...
- Codeforces 1027E Inverse Coloring 【DP】
Codeforces 1027E Inverse Coloring 题目链接 #include<bits/stdc++.h> using namespace std; #define N ...
- CodeForces 505B Mr. Kitayuta's Colorful Graph
Mr. Kitayuta's Colorful Graph Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
随机推荐
- static关键字所导致的内存泄漏问题
大家都知道内存泄漏和内存溢出是不一样的,内存泄漏所导致的越来越多的内存得不到回收的失手,最终就有可能导致内存溢出,下面说一下使用staitc属性所导致的内存泄漏的问题. 在dalvik虚拟机中,sta ...
- R-data.table
data.table可以扩展和增强data.frame的功能,在分组操作和组合时访问速度更快. require(data.table) theDT = data.table(A=1:10, B=let ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- 【HIHOCODER 1599】逃离迷宫4
描述 小Hi被坏女巫抓进一座由无限多个格子组成的矩阵迷宫. 小Hi一开始处于迷宫(x, y)的位置,迷宫的出口在(a, b).小Hi发现迷宫被女巫施加了魔法,假设当前他处在(x, y)的位置,那么他只 ...
- day03 set集合,文件操作,字符编码以及函数式编程
嗯哼,第三天了 我们来get 下新技能,集合,个人认为集合就是用来list 比较的,就是把list 转换为set 然后做一些列表的比较啊求差值啊什么的. 先看怎么生成集合: list_s = [1,3 ...
- mysql 索引和查询优化
对于任何DBMS,索引都是进行优化的最主要的因素.对于少量的数据,没有合适的索引影响不是很大,但是,当随着数据量的增加,性能会急剧下降.如果对多列进行索引(组合索引),列的顺序非常重要,MySQL仅能 ...
- [git 学习篇]远程创库
实际情况往往是这样,找一台电脑充当服务器的角色,每天24小时开机,其他每个人都从这个“服务器”仓库克隆一份到自己的电脑上,并且各自把各自的提交推送到服务器仓库里,也从服务器仓库中拉取别人的提交. 完全 ...
- [git 学习篇] git文件版本回退再学习
需求; 准备把readme.txt回退到上一个版本,也就是“add distributed”的那个版本 首先,Git必须知道当前版本是哪个版本,在Git中,用HEAD表示当前版本,也就是最新的提交3 ...
- list 类
题外:len = sizeof(a)/sizeof(a[0]); 求出数组长度 1.list是一种以双向链表方式实现的一种顺序容器.list容器中,存放元素的存储单元可以是连续的也可以是不连续的. 2 ...
- Mysql书签
关于 MySQL UTF8 编码下生僻字符插入失败/假死问题的分析