zoukankan      html  css  js  c++  java
  • 19E Fairy

    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 v, u (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
    4 4
    1 2
    1 3
    2 4
    3 4
    Output
    4
    1 2 3 4
    Input
    4 5
    1 2
    2 3
    3 4
    4 1
    1 3
    Output
    1
    5
    传送门
    分析
    我们先对点黑白染色并建树,进行此操作之后将有3种边:
    1.树边
    2.非树黑白异色边
    3.非树同色边
    于是产生了以下几种情况:
    仅有一条非树同色边就可以删这个边,否则不能删这种边
    有非树同色边就不能删非树异色边,否则可删
    树边如果在奇环上且在所有奇环上可删,否则不可删

    一个没有简单奇环的图,一定没有奇环。所以题目要找的就是覆盖所有简单奇环的边。在dfs树上,简单环与返祖边一一对应。所以在dfs的时候每遇到一个返祖边,就差分标记路径。最后找到被所有路径覆盖的树边。
    上面这个只是解法的思路,实现起来有很多细节:

    如果某个树边在一个偶环上,删除它之后会产生新的奇环,所以还要差分标记一下偶环个数。

    构成奇环的返祖边可以被纳入答案,当且仅当图中只有一个奇环【因为他只能覆盖一个奇环】。

    如果图中没有奇环,可以随意删除一条边。

    注意原图不联通的情况。

    代码

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<cctype>
    #include<cmath>
    #include<cstdlib>
    #include<queue>
    #include<ctime>
    #include<vector>
    #include<set>
    #include<map>
    #include<stack>
    using namespace std;
    struct edge{
          int next,to,fa,cl;
    }e[1100000];
    int head[1100000],col[1100000];
    bool used[1100000];
    int od[1100000],uod[1100000],dep[1100000];
    int cnt,n,m,ans,sum,top;
    int Ans[1100000];
    void add(int u,int v,int no){
          e[cnt].to=v;
          e[cnt].next=head[u];
          head[u]=cnt++;
          e[cnt].to=u;
          e[cnt].next=head[v];
          head[v]=cnt++;
    }
    /*
      cl=-1:树边
      cl=1:奇环边
      cl=2:偶环边
    */
    void dfs(int x,int no){
          int i,j,k;
          top++;
          dep[x]=top;
          used[x]=1;
          for(i=head[x];~i;i=e[i].next){
              if(e[i].cl==-1)continue;
              k=e[i].to;
              if(!used[k]){//没走过
              e[i].cl=e[i^1].cl=-1;
              dfs(k,i>>1);
              //向下累加奇环和偶环数目
              od[no]+=od[i>>1];
                uod[no]+=uod[i>>1];
            }
              else {
                  //清除重复记录
                  if(e[i].cl==1)
                    od[no]--;
                  else if(e[i].cl==2)
                    uod[no]--;
                    else{
                        if((dep[k]-dep[x])&1){//偶环
                            e[i].cl=e[i^1].cl=2;
                        uod[no]++;
                    }
                      else {
                          e[i].cl=e[i^1].cl=1;
                          od[no]++;
                          sum++;
                      }
                   }
              }
          }
          top--;
    }
    void go(){
          printf("%d ",m);
          for(int i=1;i<=m;i++)
             printf("%d ",i);
          puts("");
    }
    int main()
    {     int i,j,k,v,u;
          memset(head,-1,sizeof(head));
          scanf("%d%d",&n,&m);
          for(i=1;i<=m;i++){
              scanf("%d%d",&u,&v);
              add(u,v,i);
          }
          for(i=1;i<=n;i++)
             if(!used[i]){
                dfs(i,0);
             }
          if(sum==0){
              go();
              return 0;
          }
          for(i=0;i<m;i++){
             if(od[i]==sum&&uod[i]==0)Ans[++ans]=i;
               else if(sum==1&&e[i<<1].cl==1)Ans[++ans]=i;
          }
          printf("%d ",ans);
          for(i=1;i<=ans;i++)
             printf("%d ",Ans[i]+1);
          puts("");
          return 0;
    }

  • 相关阅读:
    poj2528Mayor's posters(线段树加离散化)第一次接触离散化 做的挺辛苦的
    poj1789Truck History(简单最小生成树)
    【洛谷P2468】粟粟的书架
    【洛谷P3523】DYNDynamite
    【SP1716】GSS3 Can you answer these queries III
    【洛谷P3320】寻宝游戏
    【洛谷P3322】排序
    【GMOJ6293】迷宫
    【GMOJ4051】序列统计
    【洛谷P4719】【模板】动态 DP
  • 原文地址:https://www.cnblogs.com/yzxverygood/p/8439904.html
Copyright © 2011-2022 走看看