CF429E Points and Segments 构造、欧拉回路
如果把一条线段\([l,r]\)看成一条无向边\((l,r+1)\),从\(l\)走到\(r+1\)表示线段\([l,r]\)染成红色,从\(r+1\)走到\(l\)表示线段\([l,r]\)染成蓝色,那么题目等价于给每一条边定下方向,使得对于所有点,从左往右经过的次数和从右往左经过的次数差的绝对值不超过\(1\)。
对于直线上所有奇数度的点,从左往右两两配对,那么就是要求对于所有的点从左往右经过的次数等于从右往左经过的次数。不难想到这是一个欧拉图。求解欧拉回路给边定向即可。
注意直线上的奇数度的点不能随意配对,因为如果对于\(A<B<C<D\)四个奇数入度的点,连\((A,D)\)\((B,C)\),而找到的欧拉回路方案中边的方向为\(A \rightarrow D , B \rightarrow C\),那么在这两条边去掉之后线段\([B,C]\)从左往右的次数比从右往左的次数少\(2\),就不符合题意了。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iomanip>
#include<set>
#include<vector>
//This code is written by Itst
using namespace std;
inline int read(){
int a = 0;
char c = getchar();
while(!isdigit(c))
c = getchar();
while(isdigit(c)){
a = a * 10 + c - 48;
c = getchar();
}
return a;
}
#define PII pair < int , int >
const int MAXN = 2e5 + 7;
multiset < PII > Edge[MAXN];
int lsh[MAXN] , in[MAXN] , N , cntL;
vector < PII > tmp;
bool col[MAXN];
void dfs(int x){
for(auto t = Edge[x].begin() ; !Edge[x].empty() ; t = Edge[x].begin()){
int a = t->first , b = t->second;
Edge[x].erase(t);
Edge[a].erase(Edge[a].find(PII(x , -b)));
if(b > 0) col[b] = 1;
dfs(a);
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in","r",stdin);
//freopen("out","w",stdout);
#endif
N = read();
for(int i = 1 ; i <= N ; ++i){
int a = read() , b = read() + 1;
tmp.push_back(PII(a , b));
lsh[++cntL] = a; lsh[++cntL] = b;
}
sort(lsh + 1 , lsh + cntL + 1);
cntL = unique(lsh + 1 , lsh + cntL + 1) - lsh - 1;
for(int i = 0 ; i < N ; ++i){
int p = lower_bound(lsh + 1 , lsh + cntL + 1 , tmp[i].first) - lsh , q = lower_bound(lsh + 1 , lsh + cntL + 1 , tmp[i].second) - lsh;
Edge[p].insert(PII(q , i + 1));
Edge[q].insert(PII(p , -i - 1));
in[p] ^= 1; in[q] ^= 1;
}
int pre = 0;
for(int i = 1 ; i <= cntL ; ++i)
if(in[i])
if(!pre) pre = i;
else{
Edge[pre].insert(PII(i , 0));
Edge[i].insert(PII(pre , 0));
++in[pre]; ++in[i];
pre = 0;
}
for(int i = 1 ; i <= cntL ; ++i)
if(!Edge[i].empty())
dfs(i);
for(int i = 1 ; i <= N ; ++i)
cout << col[i] << ' ';
return 0;
}
CF429E Points and Segments 构造、欧拉回路的更多相关文章
- 【CF429E】Points and Segments(欧拉回路)
[CF429E]Points and Segments(欧拉回路) 题面 CF 洛谷 题解 欧拉回路有这样一个性质,如果把所有点在平面内排成一行,路径看成区间的覆盖,那么每个点被从左往右的覆盖次数等于 ...
- CF429E Points and Segments
链接 CF429E Points and Segments 给定\(n\)条线段,然后给这些线段红蓝染色,求最后直线上上任意一个点被蓝色及红色线段覆盖次数之差的绝对值不大于\(1\),构造方案,\(n ...
- 【CF429E】 Points and Segments(欧拉回路)
传送门 CodeForces 洛谷 Solution 考虑欧拉回路有一个性质. 如果把点抽出来搞成一条直线,路径看成区间覆盖,那么一个点从左往右被覆盖的次数等于从右往左被覆盖的次数. 发现这个性质和本 ...
- Codeforces 429E - Points and Segments(欧拉回路)
Codeforces 题面传送门 & 洛谷题面传送门 果然我不具备融会贯通的能力/ll 看到这样的设问我们可以很自然地联想到这道题,具体来说我们可以通过某种方式建出一张图,然后根据" ...
- 【CF429E】Points and Segments 欧拉回路
[CF429E]Points and Segments 题意:给你数轴上的n条线段$[l_i,r_i]$,你要给每条线段确定一个权值+1/-1,使得:对于数轴上的任一个点,所有包含它的线段的权值和只能 ...
- [CF429E]Points ans Segments_欧拉回路
Points and Segments 题目链接:www.codeforces.com/contest/429/problem/E 注释:略. 题解: 先离散化. 发现每个位置如果被偶数条线段覆盖的话 ...
- Codeforces Round #245 (Div. 2) A. Points and Segments (easy) 贪心
A. Points and Segments (easy) Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/con ...
- 『ACM C++』 Codeforces | 1066A - Points in Segments
大一生活真 特么 ”丰富多彩“ ,多彩到我要忙到哭泣,身为班长,很多班级的事情需要管理,也是,什么东西都得体验学一学,从学生会主席.团委团总支.社团社长都体验过一番了,现在差个班长也没试过,就来体验了 ...
- Codeforces Round #501 (Div. 3) 1015A Points in Segments (前缀和)
A. Points in Segments time limit per test 1 second memory limit per test 256 megabytes input standar ...
随机推荐
- Visual Studio 20周年,我和VS不得不说的故事
Visual Studio 2017正式版已如期发布(点击这里查看发布全记录)!自去年 11 月正式宣布 Visual Studio 项目之后,微软终于正式推出了“宇宙最强集成开发环境(IDE)”的最 ...
- iOS------自动查找项目中不用的图片资源
注意:删除的时候要谨慎!别什么图都删了,看看对项目有没有作用.这个插件有时也会有一定的误差. 具体操作步骤: 1.去github上下载LSUnusedResources(下载地址:https://gi ...
- Android Studio: Error:Cannot locate factory for objects of type DefaultGradleConnector, as ConnectorServiceRegistry
将别人的项目导入自己的环境下出现的问题. Gradle refresh failed; Error:Cannot locate factory for objects of type DefaultG ...
- 方向键控制圆球运动(简易)(js)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- html之head标签
本文内容: head标签 介绍 常用子标签 meta title link style script 首发时间:2018-02-12 修改: 2018-04-24:修改了标题名称,重新排版了内容,使得 ...
- OneAPM大讲堂 | 提高JavaScript性能的30个技巧
文章系国内领先的 ITOM 管理平台供应商 OneAPM 编译呈现. 您是网站管理员还是网页开发人员?想创建超快速的网站吗? 今天我们来看看 JavaScript,这项神奇而又复杂的技术.它使网站内容 ...
- linux:644、755、777权限详解
第一位7等于4+2+1,rwx,所有者具有读取.写入.执行权限: 第二位5等于4+1+0,r-x,同组用户具有读取.执行权限但没有写入权限: 第三位5,同上,也是r-x,其他用户具有读取.执行权限但没 ...
- JMeter—总结
Jmter简单总结 简单的使用篇 jmeter简单的使用 Jmeter中默认语言的显示 jmeter利用自身代理录制脚本 Jmeter运行后出现乱码 http cookie管理中cookie poli ...
- nodejs 第一天
一.nodejs 安装 略过 二.IDE :webstorm(汉化) 三.nodejs 和 js 的区别 1.在ECMAScript 部分node和js 是一样的,比如数据类型的定义,语法结构,内置对 ...
- JavaScript获取IE版本号与HTML设置ie文档模式
JavaScript获取IE版本代码: var gIE = getIE(); alert(gIE.version) function getIE() { var rmsie = /(msie) ([\ ...