Codeforces 429E Points and Segments 欧拉回路

题意

给出 n n 条线段[li,ri],现在要把每条线段染成红色或蓝色,要求对于每个点 x x ,设有rx条线段满足覆盖点x且被染成红色, bx b x 条线段被染成蓝色,要求满足 |rxbx|1 | r x − b x | ≤ 1 。输出方案。
n105 n ≤ 10 5

分析

对于被覆盖偶数次的点,覆盖他的红色线段数和蓝色线段数必然要相等。对于被覆盖奇数次的点,我们新加一些线段来覆盖他们,使得每个点恰好被覆盖偶数次。这样的话,问题就变成了把每条线段赋值1或-1,要求每个点的权值和为0。
那么如果我们把每个点看做图上的一个点,每个区间 [li,ri] [ l i , r i ] 看成一条边 li>ri+1 l i − > r i + 1 ,然后向左走看做权值为1,向右走权值为-1,那么一种合法的染色方案一定对应原图中的一条欧拉回路。
为什么这样是对的呢?
考虑把权值差分一下,把区间加看做单点修改,那么所有位置都为0当且仅当差分后的序列所有位置都为0。把每个点的权值看作是入度-出度,那么一条边向左和向右就对应着线段权值为1或-1。这样想的话正确性就很显然了。
然后就做完了。

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>

const int N=100005;

int n,cnt,last[N*2],w[N*2],tot,t[N*2],deg[N*2];
struct data{int l,r;}a[N];
struct edge{int to,next,use;}e[N*10];

void addedge(int u,int v)
{
    deg[u]++;deg[v]++;
    e[++cnt].to=v;e[cnt].next=last[u];last[u]=cnt;
    e[++cnt].to=u;e[cnt].next=last[v];last[v]=cnt;
}

void dfs(int x)
{
    for (int &i=last[x];i;i=e[i].next)
        if (!e[i].use&&!e[i^1].use)
        {
            deg[e[i].to]--;deg[e[i^1].to]--;
            e[i].use=1;dfs(e[i].to);
        }
}

int main()
{
    scanf("%d",&n);cnt=1;
    for (int i=1;i<=n;i++)
    {
        scanf("%d%d",&a[i].l,&a[i].r);
        a[i].r++;w[++tot]=a[i].l;w[++tot]=a[i].r;
    }
    std::sort(w+1,w+tot+1);tot=std::unique(w+1,w+tot+1)-w-1;
    for (int i=1;i<=n;i++)
    {
        int l=std::lower_bound(w+1,w+tot+1,a[i].l)-w;
        int r=std::lower_bound(w+1,w+tot+1,a[i].r)-w;
        t[l]^=1;t[r]^=1;addedge(l,r);
    }
    tot++;w[tot]=w[tot-1];
    for (int i=1;i<tot;i++)
    {
        t[i]^=t[i-1];
        if (t[i]==1) addedge(i,i+1);
    }
    for (int i=1;i<=tot;i++) if (deg[i]&1) {puts("-1");return 0;}
    for (int i=1;i<=tot;i++)
        if (deg[i]) dfs(i);
    for (int i=1;i<=n;i++) printf("%d ",e[i*2].use);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值