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 [构造 二分图染色]的更多相关文章

  1. [cf741C]Arpa’s overnight party and Mehrdad’s silent entering

    直接令2i-1和2i的位置不相同,相当于有2n条边,对其进行二分图染色即可(这张图一定不存在奇环). 假设给出的n条关系是A类边,2i-1和2i的边是B类边,可以发现一条路径一定是AB交替(因为A/B ...

  2. 二分图匹配 + 构造 E. Arpa’s overnight party and Mehrdad’s silent entering

    http://codeforces.com/contest/742/problem/E 跪着看题解后才会的. 对于任何一对BF[i]和GF[i] 连接了一条边后,那么他们和隔壁都是不会有边相连的了,这 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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,求每个子树中最长的边,满 ...

随机推荐

  1. canvans生成图片<p>标签文字不居中

    尝试使用magin 0px auto ;width:100%解决.

  2. elasticsearch 6.2.4添加用户密码认证

    elasticsearch 6.3版本之前的添加认证需安装x-pack插件,6.3之后貌似去掉了这个. 1.安装x-pack 先切换到elastic用户下,在执行以下命令 $cd /data/elas ...

  3. (三)Python运算符

    一.python运算符相关 Python语言支持以下类型的运算符: 算术运算符 比较(关系)运算符 赋值运算符 逻辑运算符 位运算符 成员运算符 身份运算符 运算符优先级 1.python算数运算符 ...

  4. Linux忘记root密码 单用户模式 及启动加密

    单用户模式: 在系统启动引导读秒时,按任意键进入系统选项 再按  e  键   选择第二项  按 e  进入编辑  输入  空格  1 然后回车  再按B  键 不需要密码即可进入系统 再passwd ...

  5. 简单文本悬浮div提示效果

    <html> <head> <script src="jquery-1.9.1.min.js" type="text/javascript& ...

  6. javaFX的控制台实现

    最近做了个javaFX的工具,想弄个控制台输出信息,准备用TextArea来模拟console,但直接操纵console对象的话不依赖这个项目的地方就无法输出信息到控制台了,至于log,以前弄过一个输 ...

  7. Virtual Machine

    之前说到可以使用Assembly language来实现程序编写,把程序通过一个Assembler就可以得到计算机可以操作的二进制文件. 但是Assembly language依旧不适于编程,但怎么将 ...

  8. 第一章 初识Mysql

    Mysql是一个开放源代码的数据库管理系统(DBMS),它是由MySQL AB 公司开发.发布并支持的. 登录 -- mysql #本地登录,默认用户root,空密码,用户为root@127.0.0. ...

  9. 帆软报表(finereport)使用row_number ()进行组内排序

    ROW_NUMBER()函数将针对SELECT语句返回的每一行,从1开始编号,赋予其连续的编号.在查询时应用了一个排序标准后,只有通过编号才能够保证其顺序是一致的,当使用ROW_NUMBER函数时,也 ...

  10. Codeforces Round #352 (Div. 2) (A-D)

    672A Summer Camp 题意: 1-n数字连成一个字符串, 给定n , 输出字符串的第n个字符.n 很小, 可以直接暴力. Code: #include <bits/stdc++.h& ...