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,求每个子树中最长的边,满 ...
随机推荐
- Java线程池源码解析
线程池 假如没有线程池,当存在较多的并发任务的时候,每执行一次任务,系统就要创建一个线程,任务完成后进行销毁,一旦并发任务过多,频繁的创建和销毁线程将会大大降低系统的效率.线程池能够对线程进行统一的分 ...
- java 根据经纬度坐标计算两点的距离算法
/** * @Desc 根据经纬度坐标计算两点的距离算法<br> * @Author yangzhenlong <br> * @Data 2018/5/9 18:38 */ p ...
- Oracle 里 case 和decode的简单用法
case 就相想当于C#里面switch l 列:根据员工的职位,计算加薪后的薪水数据 如果职位是Analyst , 加薪10% 如果职位是Programmer 加薪5% 如果职位是clerk ...
- 419 JQuery and BootStrap
把葵花宝典剩下的知识讲完了 又学习了bootstrap <link rel="stylesheet" href="css/bootstrap.css"&g ...
- JAVA学习笔记(1)—— eclipse自动补全和主题及字体配置
1.自动补全功能 (1)打开 Eclipse -> Window -> Perferences (2)选择Java -> Editor -> Content Assist -& ...
- python操作redis命令
Python操作redis from redis import StrictRedis, ConnectionPoolredis_url="redis://:xxxx@112.27.10.1 ...
- 常用的user32说明
函数名称 说明 ActiveKeyboardLayout 激活一个不同的键盘布局,该布局必须先由LoadKeyBoardLayout函数装载AdjustWindowRect 根据希望的用户矩形大小来计 ...
- Jmeter JDBC Connection Configuration 链接失败,提示Error preloading the connection pool
修改数据配置的连接数即可:修改为小一点 下面是oracle 配置连接的方式
- Ubuntu 14.04 升级 nginx/1.8.1
参考文档:https://segmentfault.com/a/1190000008116875 https://www.ilanni.com/?p=11788 先停止nginx服务 service ...
- 记一个 dubbo中hessian2反序列化 Map 的一个问题
dubbo版本: 2.5.10 解决方案: 改用 "dubbo" 序列化方式 (但是 dubbo 方式也存在一个比较坑的问题 详见: Dubbo序列化多个CopyOnWriteAr ...