• Codeforces 19.E Fairy


    E. Fairy
    time limit per test
    1.5 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Once upon a time there lived a good fairy A. One day a fine young man B came to her and asked to predict his future. The fairy looked into her magic ball and said that soon the fine young man will meet the most beautiful princess ever and will marry her. Then she drew on a sheet of paper n points and joined some of them with segments, each of the segments starts in some point and ends in some other point. Having drawn that picture, she asked the young man to erase one of the segments from the sheet. Then she tries to colour each point red or blue so, that there is no segment having points of the same colour as its ends. If she manages to do so, the prediction will come true. B wants to meet the most beautiful princess, that's why he asks you to help him. Find all the segments that will help him to meet the princess.

    Input

    The first input line contains two integer numbers: n — amount of the drawn points and m — amount of the drawn segments (1 ≤ n ≤ 104, 0 ≤ m ≤ 104). The following m lines contain the descriptions of the segments. Each description contains two different space-separated integer numbers vu (1 ≤ v ≤ n, 1 ≤ u ≤ n) — indexes of the points, joined by this segment. No segment is met in the description twice.

    Output

    In the first line output number k — amount of the segments in the answer. In the second line output k space-separated numbers — indexes of these segments in ascending order. Each index should be output only once. Segments are numbered from 1 in the input order.

    Examples
    input
    Copy
    4 4
    1 2
    1 3
    2 4
    3 4
    output
    4
    1 2 3 4
    input
    Copy
    4 5
    1 2
    2 3
    3 4
    4 1
    1 3
    output
    1
    5
    题目大意:给定一个无向图,问删哪些边之后这个图变成二分图。求的是一个边的集合,实际上只删一条边.
    分析:挺好的一道题.
       一个图是二分图的充要条件是不存在奇环.将一条奇环上的边删去就能破坏掉这个奇环,如果要破坏掉所有的奇环,那么删的边就必须是所有奇环的交集.
       仅仅只是删掉交集这么简单吗?如果一条边同时在偶环和奇环上,删掉这条边后偶环和奇环就会重新组合成一个奇环.那么删的这条边就必须满足两个条件:1.在所有奇环的交集中. 2.不在任何偶环上.
       那么找环就好了.天真的我以为直接dfs+栈维护一下就好了.这道题的环是会重叠的,这种做法行不通......换一种做法,每个点记录第一条连向这个点的边的编号(其实记录的就是树边),那么可以把边转换为点,在点上对树边进行操作,非树边需要特判一下.
       每次找到一条非树边,这条边连接的两个点是u,v,如果构成了一个奇环,就在维护奇环线段树中把u,v这条链+1,否则在维护偶环线段树中把u,v这条链+1.怎么提取这条链?树链剖分!
       最后是一些细节:如果没有奇环,所有的边都满足条件;如果奇环只有1个,那么那个奇环的非树边要考虑进来;维护的是一个图而不是树,在dfs时只考虑树边!
    #include <stack>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    const int maxn = 20010;
    
    int n,m,head[maxn],to[maxn],nextt[maxn],id[maxn],tot = 1,vis[maxn],cnta,ans,cntb,anss[maxn];
    int bianhao[maxn],h[maxn],son[maxn],top[maxn],pos[maxn],sizee[maxn],cnt,fa[maxn],flag;
    int L[maxn << 2],R[maxn << 2],sum1[maxn << 2],sum2[maxn << 2],tag1[maxn << 2],tag2[maxn << 2];
    
    struct node
    {
        int x,y;
    }e[maxn];
    
    void add(int x,int y)
    {
        to[tot] = y;
        nextt[tot] = head[x];
        head[x] = tot++;
    }
    
    void dfs1(int u,int faa)
    {
        h[u] = h[faa] + 1;
        sizee[u] = 1;
        fa[u] = faa;
        for (int i = head[u];i;i = nextt[i])
        {
            int v = to[i];
            if (v == faa || h[v])
                continue;
            bianhao[v] = (i / 2) + (i % 2);
            vis[(i / 2) + (i % 2)] = 1;
            dfs1(v,u);
            sizee[u] += sizee[v];
            if (sizee[v] > sizee[son[u]])
                son[u] = v;
        }
    }
    
    void dfs2(int u,int topp)
    {
        pos[u] = ++cnt;
        id[cnt] = u;
        top[u] = topp;
        if (son[u])
            dfs2(son[u],topp);
        for (int i = head[u];i;i = nextt[i])
        {
            int v = to[i];
            if (v == fa[u] || v == son[u] || fa[v] != u)
                continue;
            dfs2(v,v);
        }
    }
    
    void build(int o,int l,int r)
    {
        L[o] = l;
        R[o] = r;
        if (l == r)
            return;
        int mid = (l + r) >> 1;
        build(o * 2,l,mid);
        build(o * 2 + 1,mid + 1,r);
    }
    
    void pushup(int o)
    {
        sum1[o] = sum1[o * 2] + sum1[o * 2 + 1];
        sum2[o] = sum2[o * 2] + sum2[o * 2 + 1];
    }
    
    void pushdown(int o)
    {
        if (tag1[o])
        {
            tag1[o * 2] += tag1[o];
            tag1[o * 2 + 1] += tag1[o];
            sum1[o * 2] += tag1[o] * (R[o * 2] - L[o * 2] + 1);
            sum1[o * 2 + 1] += tag1[o] * (R[o * 2 + 1] - L[o * 2 + 1] + 1);
            tag1[o] = 0;
        }
        if (tag2[o])
        {
            tag2[o * 2] += tag2[o];
            tag2[o * 2 + 1] += tag2[o];
            sum2[o * 2] += tag2[o] * (R[o * 2] - L[o * 2] + 1);
            sum2[o * 2 + 1] += tag2[o] * (R[o * 2 + 1] - L[o * 2 + 1] + 1);
            tag2[o] = 0;
        }
    }
    
    void update1(int o,int l,int r,int x,int y)
    {
        if(x <= l && r <= y)
        {
            sum1[o] += (r - l + 1);
            tag1[o]++;
            return;
        }
        pushdown(o);
        int mid = (l + r) >> 1;
        if (x <= mid)
            update1(o * 2,l,mid,x,y);
        if (y > mid)
            update1(o * 2 + 1,mid + 1,r,x,y);
    }
    
    void update2(int o,int l,int r,int x,int y)
    {
        if(x <= l && r <= y)
        {
            sum2[o] += (r - l + 1);
            tag2[o]++;
            return;
        }
        pushdown(o);
        int mid = (l + r) >> 1;
        if (x <= mid)
            update2(o * 2,l,mid,x,y);
        if (y > mid)
            update2(o * 2 + 1,mid + 1,r,x,y);
    }
    
    void change(int x,int y,int tagg)
    {
        if (h[x] < h[y])
            swap(x,y);
        while (top[x] != top[y])
        {
            if (h[top[x]] < h[top[y]])
                swap(x,y);
            int t = top[x];
            if (tagg == 1)
                update1(1,1,n,pos[t],pos[x]);
            else
                update2(1,1,n,pos[t],pos[x]);
            x = fa[t];
        }
        if (x == y)
            return;
        if (h[x] < h[y])
            swap(x,y);
        if (tagg == 1)
            update1(1,1,n,pos[y] + 1,pos[x]); //为什么要+1?因为实际维护的是边.
        else
            update2(1,1,n,pos[y] + 1,pos[x]);
    }
    
    int query1(int o,int l,int r,int v)
    {
        if (l == r)
            return sum1[o];
        pushdown(o);
        int mid = (l + r) >> 1;
        if (v <= mid)
            return query1(o * 2,l,mid,v);
        else
            return query1(o * 2 + 1,mid + 1,r,v);
    }
    
    int query2(int o,int l,int r,int v)
    {
        if (l == r)
            return sum2[o];
        pushdown(o);
        int mid = (l + r) >> 1;
        if (v <= mid)
            return query2(o * 2,l,mid,v);
        else
            return query2(o * 2 + 1,mid + 1,r,v);
    }
    
    int main()
    {
        scanf("%d%d",&n,&m);
        for (int i = 1; i <= m; i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            e[i].x = x;
            e[i].y = y;
            add(x,y);
            add(y,x);
        }
        for (int i = 1; i <= n; i++)
            if (!h[i])
                dfs1(i,0),dfs2(i,i);
        build(1,1,n);
        for (int i = 1; i <= m; i++)
        {
            if (!vis[i])
            {
                int x = e[i].x,y = e[i].y;
                if (abs(h[x] - h[y]) % 2 == 1)  //偶环
                {
                    cntb++;
                    change(x,y,-1);
                }
                else
                {
                    cnta++;
                    flag = i;
                    change(x,y,1);
                }
            }
        }
        if (cnta == 0)
        {
            for (int i = 1; i <= m; i++)
                anss[++ans] = i;
        }
        else
        {
            if (cnta == 1)
                anss[++ans] = flag;
            for (int i = 2; i <= n; i++)
            {
                if (query1(1,1,n,pos[i]) == cnta && query2(1,1,n,pos[i]) == 0)
                    anss[++ans] = bianhao[i];
            }
            sort(anss + 1,anss + 1 + ans);
        }
        printf("%d
    ",ans);
        for(int i = 1; i <= ans; i++)
            printf("%d ",anss[i]);
        printf("
    ");
    
        return 0;
    }

        
  • 相关阅读:
    Java 源码 Byte
    Git 常用命令add
    Mybatis Doc Configuration XML
    Java 源码 Integer
    Java 源码 Boolean
    Java 知识 fair and nonfair 锁
    Java 源码 Character
    Windows 窗口 左上角 有一个 “NO DC” 字样,一些 WPF 的应用显示有问题 的 解决办法
    java中判断对象属性值是否为空的函数 工具类
    Oracle跨库清洗数据数据
  • 原文地址:https://www.cnblogs.com/zbtrs/p/8451872.html
Copyright © 2020-2023  润新知