您的位置:首页 > 其它

Codeforces Round #383 (Div. 1) C. Arpa’s overnight party and Mehrdad’s silent entering(贪心,二分图)

2016-12-07 12:07 495 查看
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 arounda table. i-th pair consisted of a boy, sitting on the ai-thchair, and his girlfriend, sitting on the bi-thchair. The chairs were numbered 1through 2n inclockwise 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 and1 areconsidered consecutive.Find the answer for the Mehrdad question. If it was possible, find some arrangement of food types that satisfies the conditions.InputThe first line contains an integer n (1  ≤  n  ≤  105) —the number of pairs of guests.The i-th of the next n linescontains 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'sguaranteed that there was exactly one person sitting on each chair.OutputIf there is no solution, print -1.Otherwise print n lines, the i-thof them should contain two integers which represent the type of food for the i-th pair. The first integer in the line is the typeof 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.Exampleinput
3
1 4
2 5
3 6
output
1 2
2 1
1 2
题意:有2*n个情侣围着一张桌子坐着,现在有两种食物要分配,每对情侣的食物不能相同,每连续相邻的三个人的食物也不能全部相同,求一种分配方案。
分析:2*i向2*i-1连一条边,这样加上情侣间的边这个图就成了一个二分图,直接DFS染色即可(智商压制啊)
#include <queue>#include <vector>#include <cstdio>#include <utility>#include <cstring>#include <iostream>#include <algorithm>#define INF 0x3f3f3f3f#define N 200005using namespace std;typedef pair<int,int> pii;int n,vis,color;vector <pii> couple;vector <int> G;void dfs(int u,int col){vis[u] = true;color[u] = col;for(int v : G[u])if(!vis[v]) dfs(v,3-col);}int main(){scanf("%d",&n);for(int i = 1;i <= n;i++){int u,v;scanf("%d%d",&u,&v);G[u].push_back(v);G[v].push_back(u);G[2*i].push_back(2*i-1);G[2*i-1].push_back(2*i);couple.push_back(make_pair(u,v));}for(int i = 1;i <= 2*n;i++)if(!vis[i]) dfs(i,1);for(pii t : couple)printf("%d %d\n",color[t.first],color[t.second]);}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: