题目大意:

有 nnn 对情侣坐在 2n2n2n 个板凳上,板凳排成环形。每张凳子恰好坐一个人。 现在有两种食物分给他们。规定:
1、每对情侣中,俩人不能分到同一种食物;
2、环上任意三个相邻的人,不能全分到同一种食物。
给出情侣关系,请构造一种方案或输出 -1。
n<=105n<=10^5n<=105


解题思路

首先我们对这个题目进行化简就是把任意两个不同食物的进行连边
我们先不考虑情侣
先看看第二条件对于任意3点之间我们只有这三种情况,我们发现这三个点都是二分图!!

那么我们就很可以把想办法把图变成二分图!!
会发现,一定会有一条边连着相邻的两个点。那我们考虑直接在相邻的点上连边。

于是构造方法如下:首先情侣连边,然后在原图的相邻点对上,隔一对连一条边,形如:

(蓝边表示情侣连边,红边表示相邻点对隔一对连一条)

为什么要隔一对连一条?因为这样可以限制每个点的度数最多为2,即图上的环全是简单环。
为什么这样没有奇环?因为我连红边的时候,如果我连的这对点之前已经相连,那一定是通过 k 条蓝边和 k-1 条红边,也就是说加上我即将连的红边之后,这肯定是个偶环。


AC code

#include <bits/stdc++.h>
#define mid ((l + r) >> 1)
#define Lson rt << 1, l , mid
#define Rson rt << 1|1, mid + 1, r
#define ms(a,al) memset(a,al,sizeof(a))
#define log2(a) log(a)/log(2)
#define lowbit(x) ((-x) & x)
#define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define INF 0x3f3f3f3f
#define LLF 0x3f3f3f3f3f3f3f3f
#define f first
#define s second
#define endl '\n'
using namespace std;
const int N = 2e6 + 10, mod = 1e9 + 9;
const int maxn = 500010;
const long double eps = 1e-5;
const int EPS = 500 * 500;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef pair<double,double> PDD;
template<typename T> void read(T &x) {x = 0;char ch = getchar();ll f = 1;while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
template<typename T, typename... Args> void read(T &first, Args& ... args) {read(first);read(args...);
}
int n;
vector<PII>p;struct node {int nxt, to;
}edge[maxn];
int head[maxn], cnt;
void add(int x,int y) {edge[cnt] = {head[x],y};head[x] = cnt ++;edge[cnt] = {head[y],x};head[y] = cnt ++;
}
int color[maxn];
void dfs(int u,int c) {color[u]=c;for(int i=head[u];i;i=edge[i].nxt) {int v=edge[i].to;if(color[v])continue;dfs(v,3-c);}
}
int main() {IOS;cin >> n;for(int i = 1 ; i <= n; i ++)add(2*i-1,2*i);for(int i = 1; i <= n; i ++) {int x, y;cin >> x >> y;add(x,y);p.push_back(make_pair(x,y));}for(int i = 1; i <= 2 * n; i ++)if(!color[i])dfs(i,1);for(int i=0;i<n;i++)cout << color[p[i].first] << " " << color[p[i].second] << endl;return 0;
}

图论(二分图构造) ---- 二分图的性质 C. Arpa’s overnight party and Mehrdad’s silent entering相关推荐

  1. CF - 741(C. Arpa’s overnight party and Mehrdad’s silent entering) 二分图构造

    题目链接 题意 已经给出情侣的座位编号,将两种饭分配给N对情侣.满足以下两个要求: 情侣之间不能吃相同的饭 座位号连续的三个人不能吃同一种饭 问是否存在满足的解. 思路 构造二分图 对应情侣的座位建边 ...

  2. codeforces741C Arpa’s overnight party and Mehrdad’s silent entering(二分图)

    题意: 有2n个人围成一圈坐在桌子边上,每个人占据一个位子,对应这2n个人是n对情侣,要求情侣不能吃同一种食物,并且桌子上相邻的三个人的食物必须有两个人是不同的,只有两种食物(1或者是2),问一种可行 ...

  3. CF741C Arpa’s overnight party and Mehrdad’s si

    题目描述: 有n对情侣(2n个人)围成一圈坐在桌子边上,每个人占据一个位子,要求情侣不能吃同一 种食物,并且桌子上相邻的三个人的食物必须有两个人是不同的,只有两种食物(1或者是2),问一种可行分配方式 ...

  4. Codeforces 742B Arpa’s obvious problem and Mehrdad’s terrible solution

    http://codeforces.com/contest/742/problem/B B. Arpa's obvious problem and Mehrdad's terrible solutio ...

  5. CF741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths

    CF741D Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths 好像这个题只能Dsu On Tree? 有根树点分治 统计子树过x的 ...

  6. Codeforces 741 D - Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths

    D - Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths 思路: 树上启发式合并 从根节点出发到每个位置的每个字符的奇偶性记为每个位 ...

  7. 【Codeforces 741 B. Arpa's weak amphitheater and Mehrdad's 】+ 并查集 + 01背包

    B. Arpa's weak amphitheater and Mehrdad's valuable Hoses time limit per test 1 second memory limit p ...

  8. Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses —— DP(01背包)

    题目链接:http://codeforces.com/contest/742/problem/D D. Arpa's weak amphitheater and Mehrdad's valuable ...

  9. Codeforces 742B 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 ...

最新文章

  1. 干货|为什么Kafka不支持读写分离
  2. 远程服务器安装docker和docker-compose
  3. iOS之从N个数里面取M个数的组合算法
  4. dbms_random.seed
  5. List遍历删除元素remove()
  6. linux三剑客之awk
  7. HDU 1422 重温世界杯
  8. Java中学生管理系统(对分数健康查询与修改)
  9. Matlab运行时报License错误
  10. android mt4 macd,超准确的4小时MACD交易策略
  11. 软件篇 之 Mac 安装 PS 19.1.8 并破解。。。
  12. C++异常处理的问题
  13. 网络攻击中常见掩盖真实IP的攻击方式及虚假IP地址追踪溯源方法
  14. CocoaPods深入一点
  15. django html菜单,使用Django模板的导航菜单
  16. java 多线程wait()方法的基本使用——63
  17. 浅谈项目责任成本管理
  18. 华天PDM打通济南重工信息化的“任督二脉”,新动能推动新发展
  19. Python自动生成表情包,从此斗图无敌手!
  20. 文件管理-----操作系统

热门文章

  1. 2005年下半年 网络工程师 上下午试卷【附带答案】
  2. 静态路由和默认路由的区别
  3. C语言\b回退一格!多点的!_只愿与一人十指紧扣_新浪博客
  4. 一个有趣的案例 | 页面扭曲矫正
  5. 【新手必看】17个常见的Python运行时错误
  6. 大厂前端高频面试问题与答案精选
  7. FAQ系列 | 如何保证主从复制数据一致性(转)
  8. 相比Redis,Memcached真的过时了吗?
  9. (转)Unity Assets目录下的特殊文件夹名称(作用和是否会被打包到build中)
  10. 端口基础常识大全+常用端口对照