您的位置:首页 > 其它

Codeforces Codeforces Round #383 (Div. 2) E (DFS染色)

2017-03-17 21:13 429 查看
题目链接:http://codeforces.com/contest/742/problem/E

题意:

有一个环形的桌子,一共有n对情侣,2n个人,一共有两种菜。

现在让你输出一种方案,满足以下要求:

情侣间吃不同的菜

相邻的三个人不能都吃同一种菜

输出任意一个解:

先将相邻的两个人连边,这样就满足了3个人不吃同样一种菜。

情侣间连边。

#include <bits/stdc++.h>

using namespace std;

const int maxn = 2*100010;

vector <int> G[maxn];
int color[maxn];
int b[maxn];
int g[maxn];

bool dfs(int u)
{
for(int i=0; i<G[u].size(); i++)
{
int v = G[u][i];
if(color[u]==color[v]) return false;
if(!color[v])
{
color[v] = 3 - color[u];
if(!dfs(v)) return false;
}
}
return true;
}

int main()
{
int n;
scanf("%d",&n);

for(int i=1; i<=n; i++)
{
G[i*2-1].push_back(2*i);
G[i*2].push_back(2*i-1);
}

for(int i=0; i<n; i++)
{
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
b[i] = u;
g[i] = v;
}

for(int i=1; i<=2*n; i++)
{
if(color[i]==0)
{
color[i] = 1;
dfs(i);
}
}

for(int i=0; i<n; i++)
{
printf("%d %d\n",color[b[i]],color[g[i]]);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: