codeforce 742 E. Arpa’s overnight party and Mehrdad’s silent entering (分食物||二分图染色+dfs)
来源:互联网 发布:淘宝店铺过户近亲属 编辑:程序博客网 时间:2024/04/25 20:32
Note that girls in Arpa’s land are really attractive.
Arpa loves overnight parties. In the middle of one of these parties Mehrdad suddenly appeared. He sawn pairs of friends sitting around a table.i-th pair consisted of a boy, sitting on theai-th chair, and his girlfriend, sitting on thebi-th chair. The chairs were numbered1 through 2n in clockwise direction. There was exactly one person sitting on each chair.
There were two types of food: Kooft and Zahre-mar. Now Mehrdad wonders, was there any way to serve food for the guests such that:
- Each person had exactly one type of food,
- No boy had the same type of food as his girlfriend,
- Among any three guests sitting on consecutive chairs, there was two of them who had different type of food. Note that chairs2n and 1 are considered consecutive.
Find the answer for the Mehrdad question. If it was possible, find some arrangement of food types that satisfies the conditions.
The first line contains an integer n (1 ≤ n ≤ 105) — the number of pairs of guests.
The i-th of the next n lines contains a pair of integers ai andbi (1 ≤ ai, bi ≤ 2n) — the number of chair on which the boy in the i-th pair was sitting and the number of chair on which his girlfriend was sitting. It's guaranteed that there was exactly one person sitting on each chair.
If there is no solution, print -1.
Otherwise print n lines, the i-th of them should contain two integers which represent the type of food for thei-th pair. The first integer in the line is the type of food the boy had, and the second integer is the type of food the girl had. If someone had Kooft, print1, otherwise print 2.
If there are multiple solutions, print any of them.
31 42 53 6
1 22 11 2
题目大意:有2n个人围成一圈坐在桌子边上,每个人占据一个位子,对应这2n个人是n对情侣,要求情侣不能吃同一种食物,并且桌子上相邻的三个人的食物必须有两个人是不同的,只有两种食物(1或者是2),问一种可行分配方式。
思路:
1、相邻的三个人需要有两个人的食物是不同的,那么其实如果我们只要保证每两个人之间的食物种类是不同的即可。
那么对应我们建立无向边:
(i*2,i*2-1);
因为每对情侣我们也需要要求食物种类是不同的,那么对应我们还要建立无向边:
(输入进来的第一个编号,输入进来的第二个编号);
2、很容易发现,因为一共只有两种食物,那么整个图是一个二分图的模型,对应我们接下来只需要对每个点进行染色即可。
这里Dfs实现。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<vector>using namespace std;const int N = 2e5 + 100;vector<int>V[N];int a[N][2],vis[N],col[N];void dfs(int rt,int color){for(int i=0;i<V[rt].size();i++) {int son=V[rt][i];if(vis[son]) continue;vis[son]=1;col[son]=(3^color);dfs(son,3^color);}}int main(){ios::sync_with_stdio(false);int n,i,j;cin>>n;for(i=1;i<=n;i++) {cin>>a[i][0]>>a[i][1];V[a[i][0]].push_back(a[i][1]);V[a[i][1]].push_back(a[i][0]);}for(i=1;i<2*n;i=i+2) {V[i].push_back(i+1);V[i+1].push_back(i);}for(i=1;i<=2*n;i++) {if(vis[i]) continue;vis[i]=1; col[i]=1; dfs(i,1);}for(i=1;i<=n;i++) cout<<col[a[i][0]]<<" "<<col[a[i][1]]<<endl;return 0;}
- codeforce 742 E. Arpa’s overnight party and Mehrdad’s silent entering (分食物||二分图染色+dfs)
- Codeforces742E-Arpa’s overnight party and Mehrdad’s silent entering(构造+二分图染色)
- 二分图染色——Codeforces741C Arpa's overnight party and Mehrdad's silent entering
- Codeforces Round #383 (Div. 2)E. Arpa’s overnight party and Mehrdad’s silent entering【二分图染色】
- 二分图(Arpa’s overnight party and Mehrdad’s silent entering,cf 741C)
- codeforces741C Arpa’s overnight party and Mehrdad’s silent entering(二分图)
- Codeforces Round #383 (Div. 1) C. Arpa’s overnight party and Mehrdad’s silent entering(贪心,二分图)
- Codeforces Round #383 (Div. 1) C. Arpa’s overnight party and Mehrdad’s silent entering
- CodeForces 741C Arpa’s overnight party and Mehrdad’s silent entering
- 【CF741C】Arpa's overnight party and Mehrdad's silent entering 题解
- Codeforces C. Arpa’s overnight party and Mehrdad’s silent entering
- codeforce 742 D. Arpa's weak amphitheater and Mehrdad's valuable Hoses (分组背包+并查集))
- codeforce 741 B. Arpa's weak amphitheater and Mehrdad's valuable Hoses(背包 dp)
- Codeforces 742A-Arpa’s hard exam and Mehrdad’s naive cheat(快速幂)
- 假期训练——CodeForces - 742D Arpa's weak amphitheater and Mehrdad's valuable Hoses DP+dfs
- Codeforces Round #383 (Div. 2)C. Arpa's loud Owf and Mehrdad's evil plan(dfs&lcm)
- 【codeforces】741E. Arpa’s abnormal DNA and Mehrdad’s deep interest【后缀数组+分块】
- [字符串HASH && 阈值]Codeforces 741E.Arpa’s abnormal DNA and Mehrdad’s deep interest
- java反射之动态代理学习笔记
- SSH工作原理
- 树莓派禁止休眠设置
- IO流之字节流
- 大数据Spark “蘑菇云”行动第100课:Hive性能调优之企业级Join、MapJoin、GroupBy、Count、数据倾斜彻底解密和最佳实践
- codeforce 742 E. Arpa’s overnight party and Mehrdad’s silent entering (分食物||二分图染色+dfs)
- MySql 1130-host ... is not allowed to connect to this MySql server
- 广电发布“中国原创游戏精品出版工程”通知
- 生成试验数据的茎叶图,并显示不同区间数据的个数
- hover
- linux sed 匹配替换
- RandomAccessFile
- 网站程序员学习路线
- tomcat配置文件server.xml含义说明