码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #383 (Div. 1) C(二分图)

时间:2017-02-23 19:00:48      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:scanf   air   make   span   push   force   void   using   存在   

一道很巧妙的二分图的题目

简单分析性质可知,一个合法序列一定是由12,21这样的子串构成的,所以相邻的每隔2个两两配对

然后BF和GF互相配对,思考一下,如果存在奇环,那么必定有一个BG有两个GF,或者一个GF有两个BF,所以不存在这种情况,一定有解

直接二分图判断即可

#include <iostream>
#include <cstdio>
#include <vector>
#define mp make_pair
#define fi first
#define se second
using namespace std;
const int maxn = 200050;
vector<int> G[maxn];
int color[maxn];
vector <pair <int, int>> Q;
void dfs(int x)
{
    for(auto to : G[x])
    {
        if(!color[to])
        {
            color[to] = color[x] == 1 ? 2 : 1;
            dfs(to);
        }
    }
}
int n, x, y;
int main()
{
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
    {
        scanf("%d %d", &x, &y); x--; y--;
        G[x].push_back(y); G[y].push_back(x);
        Q.push_back(mp(x, y));
    }
    for(int i = 0; i < n; i ++)
        G[2*i].push_back(2*i+1), G[2*i+1].push_back(2*i);
    for(int i = 0; i < 2*n; i++)
        if(!color[i]) { color[i] = 1; dfs(i); }
    for(auto x : Q) printf("%d %d\n", color[x.fi], color[x.se]);
}

 

Codeforces Round #383 (Div. 1) C(二分图)

标签:scanf   air   make   span   push   force   void   using   存在   

原文地址:http://www.cnblogs.com/Saurus/p/6434529.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!