CF547D Mike and Fish
欧拉回路,巧妙的解法。
发现每一个点$(x, y)$实际上是把横坐标和$x$和纵坐标$y$连一条线,然后代进去跑欧拉回路,这样里一条边对应了一个点,我们只要按照欧拉回路间隔染色即可。
注意到原图可能并不是一个完全的欧拉图,但是度数为奇数的点只可能有偶数个,我们可以在连完边之后再把度数为奇数的点两两配对连边,这样子就是一个完全的欧拉图了。
然后……这个图仍然可能不连通,所以每一个点都代进去搜一下。
思考一下这样子为什么是对的,我们从一个点开始走,如果这个点染了一个颜色,那么这个点同一行或者是同一列的点都不要再染同一个颜色了,向别的地方走的意思就是不染相同的颜色,这样子看似贪心地染一定能求出所需要地答案,欧拉回路其实就是确定了一个染色的顺序,我们只要看一看走了正反哪一条边就可以确定染色了。
时间复杂度应当是$O(n)$的,但是我还写了个离散化……
Code:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int N = 2e5 + ;
const int inf = << ; int n, totx, toty, tot = , head[N << ];
int deg[N << ], ax[N], ay[N], col[N];
bool vis[N << ]; struct Edge {
int to, nxt;
} e[N << ]; inline void add(int from, int to) {
e[++tot].to = to;
e[tot].nxt = head[from];
head[from] = tot;
} struct Innum {
int val, id; friend bool operator < (const Innum &x, const Innum &y) {
if(x.val != y.val) return x.val < y.val;
else return x.id < y.id;
} } in[N]; inline void read(int &X) {
X = ; char ch = ; int op = ;
for(; ch > '' || ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} inline void chkMax(int &x, int y) {
if(y > x) x = y;
} inline void discrete(int *arr, int preLen, int &nowLen) {
in[].val = -inf;
for(int i = ; i <= preLen; i++)
in[i].val = arr[i], in[i].id = i;
sort(in + , in + + preLen);
nowLen = ;
for(int i = ; i <= preLen; i++) {
if(in[i].val != in[i - ].val) ++nowLen;
arr[in[i].id] = nowLen;
}
} void dfs(int x) {
for(int &i = head[x]; i; i = e[i].nxt) {
int y = e[i].to, t = i;
if(vis[t] || vis[t ^ ]) continue;
vis[t] = ;
dfs(y);
}
} int main() {
read(n);
for(int i = ; i <= n; i++)
read(ax[i]), read(ay[i]); /* printf("%d\n", n);
for(int i = 1; i <= n; i++)
printf("%d %d\n", ax[i], ay[i]); */ discrete(ax, n, totx), discrete(ay, n, toty);
for(int i = ; i <= n; i++) {
add(ax[i], ay[i] + totx), add(ay[i] + totx, ax[i]);
++deg[ax[i]], ++deg[ay[i] + totx];
} int lst = ;
for(int i = ; i <= totx + toty; i++) {
if(!(deg[i] & )) continue;
if(!lst) lst = i;
else add(lst, i), add(i, lst), ++deg[i], ++deg[lst], lst = ;
} for(int i = ; i <= totx + toty; i++)
if(deg[i]) dfs(i); for(int i = ; i <= n; i++)
putchar(vis[i << ] ? 'r' : 'b'); printf("\n");
return ;
}
CF547D Mike and Fish的更多相关文章
- CF547D Mike and Fish 建图
题意: 有点长→CF547DMike and Fish. 分析: 其实也没什么好分析的,我这也是看的题解. (不过,那篇题解好像文字的代码不太对劲) 这里直接说做法,正确性自证: 对输入的,将横.纵坐 ...
- cf547D. Mike and Fish(欧拉回路)
题意 题目链接 Sol 说实话这题我到现在都不知道咋A的. 考试的时候是对任意相邻点之间连边,然后一分没有 然后改成每两个之间连一条边就A了.. 按说是可以过掉任意坐标上的点都是偶数的数据啊.. #i ...
- 「CF547D」 Mike and Fish
「CF547D」 Mike and Fish 传送门 介绍三种做法. \(\texttt{Solution 1}\) 上下界网络流 我们将每一行.每一列看成一个点. 两种颜色的数量最多相差 \(1\) ...
- Codeforces 247D Mike and Fish
Mike and Fish 我们可以把这个模型转换一下就变成有两类点,一类是X轴, 一类是Y轴, 每个点相当于对应的点之间建一条边, 如果这条边变红两点同时+1, 变蓝两点同时-1. 我们能发现这个图 ...
- CF 547 D. Mike and Fish
D. Mike and Fish http://codeforces.com/contest/547/problem/D 题意: 给定平面上n个点,将这些点染成红或者蓝色,要求每行.每列红色点与蓝色点 ...
- CodeForces - 547D: Mike and Fish (转化为欧拉回路)(优化dfs稠密图)(定向问题)
As everyone knows, bears love fish. But Mike is a strange bear; He hates fish! The even more strange ...
- codeforces #305 D Mike and Fish
正解貌似是大暴搜? 首先我们考虑这是一个二分图,建立网络流模型后很容易得出一个算法 S->行 容量为Num[X]/2; 行->列 容量为1 且要求(x,y)这个点存在 列->T 容量 ...
- Codeforces 547D Mike and Fish
Description 题面 题目大意:有一个的网格图,给出其中的 \(n\) 个点,要你给这些点染蓝色或红色,满足对于每一行每一列都有红蓝数量的绝对值之差不超过1 Solution 首先建立二分图, ...
- Codeforces.547D.Mike and Fish(思路 欧拉回路)
题目链接 \(Description\) 给定平面上n个点,将这些点染成红or蓝色,要求每行.每列红色点与蓝色点数量的差的绝对值<=1.输出方案(保证有解). \(Solution\) 参考这 ...
随机推荐
- windows服务是如何被调用的?
1.服务介绍 操作系统在启动的时候,会启动一些不需要用户交互的进程.这些进程被称为服务.当操作系统启动后它就自动被运行. 2.组成 服务程序.服务控制程序(SCP,service control pr ...
- matlab中一些常用的函数
stem函数h = stem(x,y); %绘制火柴梗图 ,stem的工作原理是,根据一个x对应一个y,绘制火柴梗图.
- 【BZOJ2908】又是nand 树链剖分+线段树
[BZOJ2908]又是nand escription 首先知道A nand B=not(A and B) (运算操作限制了数位位数为K)比如2 nand 3,K=3,则2 nand 3=not (2 ...
- restful规则
参考连接:https://blog.igevin.info/posts/restful-api-get-started-to-write/#url_rules https://juejin.im/po ...
- C#获取文件的MD5码
using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace Send ...
- bzoj 4104 [Thu Summer Camp 2015]解密运算——思路
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4104 想了很久,想出一个 nlogn (也许是 n2logn )的,可惜空间是 n2 . 已 ...
- MySQL中创建用户与授权
参考地址:http://blog.csdn.net/gebitan505/article/details/51726649 一.创建用户(使用root用户登录进入mysql命令行) create us ...
- SpringCloud子项目
Spring Cloud简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中涉及的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全 ...
- java随机数组
import java.util.Random; public class Ccore { public static void main(String[] args) { for(int i=1;i ...
- 1056 Mice and Rice
题意:略 思路:利用queue来模拟一轮一轮的比赛.自己第一遍做的时候完全没有用queue做的意识,代码写的贼烦最后还只得了17分,非常郁闷.通过本题反映出对queue的应用场景季度不熟悉,STL里面 ...