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 ...
随机推荐
- python爬虫基础11-selenium大全5/8-动作链
Selenium笔记(5)动作链 本文集链接:https://www.jianshu.com/nb/25338984 简介 一般来说我们与页面的交互可以使用Webelement的方法来进行点击等操作. ...
- solr配置中文分词器
配置IK分词器 在/opt/solr-7.7.1/server/solr-webapp/webapp/WEB-INF/lib目录中加入IK分词器的jar包 在/opt/solr-7.7.1/serve ...
- sqli-labs less1 &&less3&&less4学习心得
0x01.less1 id=1/ id=1 and 1=1结果正常 id=1 and 1=2结果正常,不合理 id=1'提示:
- ubuntu更新内核后卡在自检无法开机的解决方法
下载deb包安装,重启后卡在自检,黑屏. 重启进旧内核,仍然卡在自检,黑屏. 强制关机后再重启,在grub按e修改启动项,改成直接进命令行模式.使用 sudo apt-get remove linux ...
- py文件转exe时包含paramiko模块出错解决方法
问题描述:python代码中包含paramiko模块的远程登录ssh,在用pyInstaller转为exe时报错, 报错提示为“No handlers could be found for logge ...
- java内存模型学习
根据 JVM 规范,JVM 内存共分为虚拟机栈.堆.方法区.程序计数器.本地方法栈五个部分. 虚拟机的内存模型分为两部分:一部分是线程共享的,包括 Java 堆和方法区:另一部分是线程私有的,包括虚拟 ...
- seleniumIDE使用
1.selenium IDE使用:适用于火狐浏览器 2.界面按钮包括录制(右上角的红点),运行脚本(中上页的绿色三角,包括依次运行和单个运行的2个运行按钮) 3.导出文件为.java,在文件选项中
- python 获得列表中每个元素出现次数的最快方法
import collections import numpy as np import random import time def list_to_dict(lst): dic = {} for ...
- xml和pandas结合处理的一个小例子-待完善
#!/usr/bin/env python3 # -*- coding:utf-8 -*- import pandas import json import xml.etree.ElementTree ...
- 漫谈登录桩(MockStub)的实现
2014年6月4日,6月的第一个星期三,我正式入职百度,开始baiduer的工作.这不到2个月的时间,因为人力资源这边原因,我从INF部门离开,拉到了百度Hi-Server团队中来.2个完全不着调的岗 ...