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

Input
4 3 5
1 2
2 2
3 2
4 1
4 3
Output
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的更多相关文章

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

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

  2. Educational Codeforces Round 2 Edge coloring of bipartite graph

    题意: 输入一个二分图,用最少的颜色数给它的每条边染色,使得同一个顶点连的边中颜色互不相同. 输出至少需要的颜色数和任意一种染色方案. 分析: 证明不会,只说一下(偷瞄巨巨代码学到的)做法. 假设点的 ...

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

    Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...

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

    Bipartite Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

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

    Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...

  6. 二分图点染色 BestCoder 1st Anniversary($) 1004 Bipartite Graph

    题目传送门 /* 二分图点染色:这题就是将点分成两个集合就可以了,点染色用dfs做, 剩下的点放到点少的集合里去 官方解答:首先二分图可以分成两类点X和Y, 完全二分图的边数就是|X|*|Y|.我们的 ...

  7. 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 ...

  8. Codeforces 1027E Inverse Coloring 【DP】

    Codeforces 1027E Inverse Coloring 题目链接 #include<bits/stdc++.h> using namespace std; #define N ...

  9. CodeForces 505B Mr. Kitayuta's Colorful Graph

    Mr. Kitayuta's Colorful Graph Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d ...

随机推荐

  1. Anaconda的安装、使用以及与PyCharm的链接

    初学Python的盆友们是否有这样的疑惑: 选择Python2还是Python3呢?(后者并不完全兼容前者) 听说两者可以同时安装,那怎么管理呢? Python那么丰富的第三方库,一个一个装太麻烦啦 ...

  2. Gym - 101775L SOS 博弈 找规律

    题目:https://cn.vjudge.net/problem/Gym-101775L PS:训练赛中被这道题折磨的不轻,和队友反复推必胜态与必败态试图推导出公式或者规律,然后推的心态逐渐失控,,, ...

  3. #2 create and populate a database && realistic and practical applications

    The Chapter3 & Chapter4 of this book tells you how to create a realistic app on the web through ...

  4. adb -a server nodaemon,设备一直显示 offline,而 adb devices 一直显示 device【已解决】

    1. adb -a server nodaemon 一直显示 offline 2. adb devices 一直显示 device 谷歌 和 度娘了一圈,未寻得解决办法 # 解决方法 问题已解决,使用 ...

  5. 并查集 - BZOJ 1104 [POI2007]洪水

    BZOJ 1104 [POI2007]洪水 描述 AKD 市处在一个四面环山的谷地里.最近一场大暴雨引发了洪水,AKD 市全被水淹没了.Blue Mary,AKD 市的市长,召集了他的所有顾问(包括你 ...

  6. Mime类型与文件后缀对照表及探测文件MIME的方法

    说明:刚刚写了一篇<IHttpHandler的妙用(2):防盗链!我的资源只有我的用户才能下载>的文章,网址:http://blog.csdn.net/zhoufoxcn/archive/ ...

  7. MAC OS X 终端命令入门

    在这里记下..防止丢失 pwd 当前工作目录 cd(不加参数) 进root cd(folder) 进入文件夹 cd .. 上级目录 cd ~ 返回root cd - 返回上一个访问的目录 rm 文件名 ...

  8. 关于 NSData 的数据类型(2进制,16进制之间)及深入剖析

    1. NSData 与 NSString NSData-> NSString NSString *aString = [[NSString alloc initWithData:adataenc ...

  9. python练手系列-分布式监控

    如果我们要写一个监控系统,要注意哪些问题和需求? [1] agent收集数据的时候需要通过系统调用少的方法收集到我们需要数据,一般来说我们优先使用python自带的系统方法,然后是读取/proc 文件 ...

  10. python-高级编程-06-长连接&连接池

    我们都知道tcp是基于连接的协议,其实这个连接只是一个逻辑上面的概念,在ip层来看,tcp和udp仅仅是内容上稍有差别而已. tcp 的连接仅仅是连接两端对于四元组和sequence号的一种约定而已 ...