codeforce 742 E. Arpa’s overnight party and Mehrdad’s silent entering (分食物||二分图染色+dfs)
2016-12-18 20:27
387 查看
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 saw
n pairs of friends sitting around a table.
i-th pair consisted of a boy, sitting on the
ai-th chair, and his girlfriend, sitting on the
bi-th chair. The chairs were numbered
1 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 chairs
2n 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.
Input
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 and
bi (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.
Output
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 the
i-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, print
1, otherwise print 2.
If there are multiple solutions, print any of them.
Example
Input
Output
题目大意:有2n个人围成一圈坐在桌子边上,每个人占据一个位子,对应这2n个人是n对情侣,要求情侣不能吃同一种食物,并且桌子上相邻的三个人的食物必须有两个人是不同的,只有两种食物(1或者是2),问一种可行分配方式。
思路:
1、相邻的三个人需要有两个人的食物是不同的,那么其实如果我们只要保证每两个人之间的食物种类是不同的即可。
那么对应我们建立无向边:
(i*2,i*2-1);
因为每对情侣我们也需要要求食物种类是不同的,那么对应我们还要建立无向边:
(输入进来的第一个编号,输入进来的第二个编号);
2、很容易发现,因为一共只有两种食物,那么整个图是一个二分图的模型,对应我们接下来只需要对每个点进行染色即可。
这里Dfs实现。
Arpa loves overnight parties. In the middle of one of these parties Mehrdad suddenly appeared. He saw
n pairs of friends sitting around a table.
i-th pair consisted of a boy, sitting on the
ai-th chair, and his girlfriend, sitting on the
bi-th chair. The chairs were numbered
1 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 chairs
2n 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.
Input
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 and
bi (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.
Output
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 the
i-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, print
1, otherwise print 2.
If there are multiple solutions, print any of them.
Example
Input
3 1 4 2 5 3 6
Output
1 2 2 1 1 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 ; int a [2],vis ,col ; 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; }
相关文章推荐
- Codeforces Round #383 (Div. 2)E. 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 C. Arpa’s overnight party and Mehrdad’s silent entering
- 二分图染色——Codeforces741C 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(二分图)
- 【CF741C】Arpa's overnight party and Mehrdad's silent entering 题解
- CodeForces 741C Arpa’s overnight party and Mehrdad’s silent entering
- Codeforces Round #383 (Div. 1) C. Arpa’s overnight party and Mehrdad’s silent entering
- 二分图匹配 + 构造 E. Arpa’s overnight party and Mehrdad’s silent entering
- codeforce 742 D. Arpa's weak amphitheater and Mehrdad's valuable Hoses (分组背包+并查集))
- 742 C. Arpa's loud Owf and Mehrdad's evil plan codeforces
- 742 A - Arpa’s hard exam and Mehrdad’s naive cheat codeforces
- 742 B - Arpa’s obvious problem and Mehrdad’s terrible solution codeforces
- Arpa's weak amphitheater and Mehrdad's valuable Hoses CodeForces - 742D (分组背包)
- codeforce 741 B. Arpa's weak amphitheater and Mehrdad's valuable Hoses(背包 dp)
- Codeforces Round #383 (Div. 2)C. Arpa's loud Owf and Mehrdad's evil plan(dfs&lcm)
- Codeforces 742B B. Arpa’s obvious problem and Mehrdad’s terrible solution
- CF 742 B-Arpa’s obvious problem and Mehrdad’s terrible solution
- Codeforces Round #383 (Div. 2) A. Arpa’s hard exam and Mehrdad’s naive cheat(快速幂水题)