[Codeforces 272E] Dima and Horses (图染色构造)

Codeforces - 272E

N <script id="MathJax-Element-2" type="math/tex">N</script>匹马,其中有一些马是敌对关系,要求将整个图分为两部分,
每部分里的任意一匹马在同一部分中的敌人不超过一个
其中任意一匹马的敌人不超过三个,无解输出 -1


首先分析一匹马的至多三个敌人的所有状态,发现不可能无解
直到了这点以后就可以随便搞了
首先将所有点都染成 0,然后找到其中不满足条件的丢到队列里
然后不断地更改不满足条件的颜色,再将其影响到的点丢到队列里
由于保证有解,所以很快就能构造出一组合法解

#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <string>
#include <complex>
using namespace std;
typedef pair<int,int> Pii;
typedef long long LL;
typedef unsigned long long ULL;
typedef double DBL;
typedef long double LDBL;
#define MST(a,b) memset(a,b,sizeof(a))
#define CLR(a) MST(a,0)
#define SQR(a) ((a)*(a))
#define PCUT puts("\n----------")

const int maxn=3e5+10, maxm=6e5+10;
struct Graph
{
    int ndn, edn, last[maxn];
    int u[maxm], v[maxm], nxt[maxm];
    void init(int _n) {ndn=_n; edn=0; MST(last,-1);}
    void adde(int _u, int _v)
    {
        u[edn]=_u; v[edn]=_v;
        nxt[edn]=last[_u];
        last[_u]=edn++;
    }
};

int N,M;
Graph G;
int col[maxn];
bool inq[maxn];
queue<int> que;

int Cnt(int);

int main()
{
    #ifdef LOCAL
    freopen("in.txt", "r", stdin);
//  freopen("out.txt", "w", stdout);
    #endif

    while(~scanf("%d%d", &N, &M))
    {
        G.init(N);
        CLR(col); CLR(inq);
        for(int i=0,u,v; i<M; i++)
        {
            scanf("%d%d", &u, &v);
            G.adde(u,v); G.adde(v,u);
        }
        while(que.size()) que.pop();
        for(int i=1; i<=N; i++) if(!Cnt(i)) que.push(i);
        while(que.size())
        {
            int u=que.front(); que.pop();
            if(Cnt(u) != col[u]) continue;
            col[u] ^= 1;
            for(int e=G.last[u]; ~e; e=G.nxt[e]) if(!inq[G.v[e]]) que.push(G.v[e]);
        }
        for(int i=1; i<=N; i++) putchar(col[i]+'0'); puts("");
    }
    return 0;
}

int Cnt(int u)
{
    int cnt0=0, cnt1=0;
    for(int e=G.last[u],v; ~e; e=G.nxt[e]) if(col[v=G.v[e]]) cnt1++; else cnt0++;
    return cnt0>cnt1?0:1;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值