Codeforces Round #383 (Div. 2)E. Arpa’s overnight party and Mehrdad’s silent entering【二分图染色】

    xiaoxiao2021-12-15  73

    E. Arpa’s overnight party and Mehrdad’s silent entering time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

    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 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实现。

    Ac代码:

    #include<stdio.h> #include<string.h> #include<vector> using namespace std; int color[200500]; int p[100500][2]; int vis[200500]; vector<int >mp[200500]; void Dfs(int u,int col) { color[u]=col; vis[u]=1; for(int i=0;i<mp[u].size();i++) { int v=mp[u][i]; if(vis[v]==0) { Dfs(v,3-col); } } } int main() { int n; while(~scanf("%d",&n)) { for(int i=1;i<=n;i++) { int x,y; scanf("%d%d",&x,&y); p[i][0]=x; p[i][1]=y; mp[x].push_back(y); mp[y].push_back(x); mp[2*i].push_back(2*i-1); mp[2*i-1].push_back(2*i); } memset(vis,0,sizeof(vis)); for(int i=1;i<=2*n;i++) { if(vis[i]==0) { vis[i]=1; Dfs(i,1); } } for(int i=1;i<=n;i++) { printf("%d %d\n",color[p[i][0]],color[p[i][1]]); } } }

    转载请注明原文地址: https://ju.6miu.com/read-1000067.html

    最新回复(0)