zoukankan      html  css  js  c++  java
  • 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 mlines 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

    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 

    写了一晚上的代码

    其实树上差分就是差分数组

    不过就是使用dfs来进行差分操作

    注意具体分析

    本题不能存在奇数环

    //树上差分
    #include<bits/stdc++.h>
    using namespace  std;
    int v[10005];
    int u[10005];
    vector<int>G[10005];
    int vis[10005];
    int dep[10005];
    int num1[10005];
    int num2[10005];
    int sum1=0;
    int sum2=0;
    int ans[10005];
    int dfs(int x,int father)
    {
        vis[x]=1;
        for(auto V:G[x])
        {
            if(V==father) continue;
            if(vis[V]==0)
            {
               // cout<<":1"<<x<<" "<<V<<endl;
                dep[V]=dep[x]+1;
                dfs(V,x);
            }
            else if(vis[V]==1&&dep[V]<dep[x])
            {  // cout<<":2"<<x<<" "<<V<<endl;
                int len=dep[x]-dep[V]+1;
                if(len%2==1)
                {
                    num1[x]+=1;
                    num1[V]-=1;
                    sum1++;
                }
                else
                {
                    num2[x]++;
                    num2[V]--;
                    sum2++;
                }
            }
        }
    }
    int dfs_sum(int x,int father)
    {
        vis[x]=1;
        for(auto V:G[x])
        {
            if(V==father) continue;
            if(vis[V]==0)
            {
                dfs_sum(V,x);
                num1[x]+=num1[V];
                num2[x]+=num2[V];
            }
        }
    }
    int main()
    {
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d",&u[i],&v[i]);
            G[u[i]].push_back(v[i]);
            G[v[i]].push_back(u[i]);
        }
        memset(vis,0,sizeof(vis));
        for(int i=1; i<=n; i++) //建立差分数组
        {
            if(vis[i]==0) dfs(i,0);
        }
        memset(vis,0,sizeof(vis));
        for(int i=1; i<=n; i++)
        {
            if(vis[i]==0)
            dfs_sum(i,0);
        }
        int cnt=0;
        for(int i=1; i<=m; i++)
        {
            if(dep[u[i]]<dep[v[i]]) swap(u[i],v[i]);
            if(sum1==0||dep[u[i]]!=dep[v[i]]+1&&sum1==1&&(dep[u[i]]-dep[v[i]]+1)%2==1||dep[v[i]]+1==dep[u[i]]&&num1[u[i]]==sum1&&num2[u[i]]==0)
            {
                cnt++;
                ans[cnt]=i;
            }
        }
        printf("%d
    ",cnt);
        for(int i=1; i<=cnt; i++)
        {
            printf("%d%s",ans[i],i==cnt?"
    ":" ");
        }
    }
    
  • 相关阅读:
    C盘的可用空间忽大忽小
    安装软件时不能指定软件的安装目录
    MySQL安装排坑
    Butterfly主题目录生成不了问题
    apache+php安装配置的各种问题
    环境变量配置不成功
    http模块
    Node.js
    Spring Boot入门
    代码优化笔记
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852223.html
Copyright © 2011-2022 走看看