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\n",cnt);
    for(int i=1; i<=cnt; i++)
    {
        printf("%d%s",ans[i],i==cnt?"\n":" ");
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值