CF741C.Arpa’s overnight party and Mehrdad’s silent entering [构造 二分图染色]
CF741C - Arpa’s overnight party and Mehrdad’s silent entering
题意:
有 n 对情侣坐成一个圈,有两种食物Kooft and Zahre-mar,要给每个人分其中一种,要求每对情侣的食物不同,任意连续的三个人必须要有两人食物不同。
求分配方案,无解输出-1
1≤n ≤100000
注意:“Kooft” is something make people die. “Zahre-mar” meaning is “Venom of Snake”.
好开心啊
构造。
情侣连边。为满足连续三人有两人不同,强制2i和2i+1连边。
这样不会产生奇环,一定有解,二分图染色
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 2e5+5;
int n, a[N], b[N];
struct edge {
int v, ne;
} e[N<<1];
int cnt, h[N];
inline void ins(int u, int v) {
e[++cnt] = (edge) {v, h[u]}; h[u] = cnt;
e[++cnt] = (edge) {u, h[v]}; h[v] = cnt;
}
int col[N];
void dfs(int u, int p) {
col[u] = p;
for(int i=h[u]; i; i=e[i].ne) {
int v = e[i].v;
if(col[v]) continue;
dfs(v, 3-p);
}
}
int main() {
//freopen("in", "r", stdin);
ios::sync_with_stdio(false); cin.tie(); cout.tie();
cin >> n;
for(int i=1; i<=n; i++) {
cin >> a[i] >> b[i];
ins(a[i], b[i]);
ins(i<<1, i<<1|1);
}
for(int i=1; i<=n<<1; i++) if(!col[i]) dfs(i, 1);
for(int i=1; i<=n; i++) cout << col[a[i]] << ' ' << col[b[i]] << '\n';
}
CF741C.Arpa’s overnight party and Mehrdad’s silent entering [构造 二分图染色]的更多相关文章
- [cf741C]Arpa’s overnight party and Mehrdad’s silent entering
直接令2i-1和2i的位置不相同,相当于有2n条边,对其进行二分图染色即可(这张图一定不存在奇环). 假设给出的n条关系是A类边,2i-1和2i的边是B类边,可以发现一条路径一定是AB交替(因为A/B ...
- 二分图匹配 + 构造 E. Arpa’s overnight party and Mehrdad’s silent entering
http://codeforces.com/contest/742/problem/E 跪着看题解后才会的. 对于任何一对BF[i]和GF[i] 连接了一条边后,那么他们和隔壁都是不会有边相连的了,这 ...
- code forces 383 Arpa's loud Owf and Mehrdad's evil plan(有向图最小环)
Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 megab ...
- Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(分组背包+dsu)
D. Arpa's weak amphitheater and Mehrdad's valuable Hoses Problem Description: Mehrdad wants to invit ...
- Arpa's weak amphitheater and Mehrdad's valuable Hoses
Arpa's weak amphitheater and Mehrdad's valuable Hoses time limit per test 1 second memory limit per ...
- Arpa's loud Owf and Mehrdad's evil plan
Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 megab ...
- Codeforces Round #383 (Div. 2)C. Arpa's loud Owf and Mehrdad's evil plan
C. Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 me ...
- Codeforces Round #383 (Div. 2) B. Arpa’s obvious problem and Mehrdad’s terrible solution
B. Arpa’s obvious problem and Mehrdad’s terrible solution time limit per test 1 second memory limit ...
- CF 741D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths [dsu on tree 类似点分治]
D. Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths CF741D 题意: 一棵有根树,边上有字母a~v,求每个子树中最长的边,满 ...
随机推荐
- RabbitMQ 和 Kafka
============================RabbitMQ 术语============================RabbitMQ 有很多术语和Kafka不一样, 理解这些术语十分 ...
- vue父子组件生命周期执行顺序
之前写了vue的生命周期,本以为明白了vue实例在创建到显示在页面上以及销毁等一系列过程,以及各个生命周期的特点.然而今天被问到父子组件生命周期执行顺序的时候一头雾水,根本不知道怎么回事.然后写了一段 ...
- redis基础篇
1.redis常见的数据结构 redis是一种以键值对存储的高性能内存数据库,有五种常用的数据类型,string,list,hash,set,zset. 2.redis的过期时间 redis中的key ...
- sql选择
关系型数据库遵循ACID规则 1.A (Atomicity) 原子性 原子性很容易理解,也就是说事务里的所有操作要么全部做完,要么都不做,事务成功的条件是事务里的所有操作都成功,只要有一个操作失败,整 ...
- Python自动化中的键盘事件
1) from selenium import webdriver 加载模块 2) b = webdriver.Friefox() 打开浏览器 3) b.get(‘http://xxxxxxx’) 打 ...
- LeetCode第二十三题-合并n个有序链表
Merge k Sorted Lists 问题简介:合并k个已排序的链表并将其作为一个排序链表返回. 举例: 输入: [ 1->4->5, 1->3->4, 2->6 ] ...
- Java 多线程 - 锁优化
http://www.cnblogs.com/pureEve/p/6421273.html https://www.cnblogs.com/mingyao123/p/7424911.html
- Elasticsearch一些使用笔记(持续更新)
这篇博客记录这一些运维ES的一些经验. 1.节点磁盘使用率过高,导致ES集群shard无法分配,丢失数据? 有两个配置,分配副本的时候 参数名称 默认值 含义 cluster.routing.allo ...
- [Linux]出错处理errno
概述 公共头文件<errno.h>定义了一个整型值errno以及可以赋予它的各种常量. 大部分函数出错后返回-1,并且自动给errno赋予当前发生的错误枚举值. 需要注意的一点是,errn ...
- Python env使用(virtualenv)
前言 Python 的 virualenv 模块闻名已久,乘着有点时间,学习一下 变更记录 # 19.3.26 创建文章 # 19.3.27 完善文章 正文 安装 pip install virt ...