CF19E Fairy

来源:互联网 发布:董小飒淘宝零食店 编辑:程序博客网 时间:2024/04/30 00:15

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 (1n104,0m104). 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.

Example
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
题意:给你n个点,m条边,问你删除哪一条边后,能够确保构成一个二分图。也就是黑白染色成功。输出所有可能。
思路:不难想到的暴力的方法就是枚举边,然后染色,不过这样复杂度是O(nm)的,显然会超时。
那我们需要利用二分图的性质:
一个图作为二分的充要条件就是不存在奇环。那么对于一个联通的图:
(首先建立dfs深搜树,树上向下的边称为竖边,往祖先回的边称为返祖边)
1.如果没有奇环,那么任意删掉一条边就可以。
2.如果存在奇环,(奇环上)如果删除返祖边则必须只有这一个奇环,否则不可能符合条件。而对于竖边,则必须包含在所有的奇环中,并且不能包含在偶环中。为什么不能包含在偶环中呢,因为删掉这个公共边之后能构成新的奇环。
那么解决方法就有了~

#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>#include <queue>using namespace std;typedef long long LL;const int MAXN = 1e4+5;const int inf = 1e9;int n,m;//前向星int cnt;int head[MAXN];struct edge{    int u,v;    int next;} edge[MAXN<<1];void add(int u,int v){    edge[cnt].u = u;    edge[cnt].v = v;    edge[cnt].next = head[u];    head[u] = cnt++;}//给出的边struct link{    int u,v;    int odd = 0,even = 0;    bool flag = 0;//1为竖边,0为返祖边} link[MAXN];//建立深搜树,划分竖边,记录深度bool vis[MAXN];int deep[MAXN];void dfs(int u,int d){    vis[u] = 1;    deep[u] = d;    for(int i = head[u]; ~i; i = edge[i].next)    {        int v = edge[i].v;        if(!vis[v])        {            link[i>>1].flag = 1;            dfs(v,d+1);        }    }}//统计每条边所在奇偶环数量int odd[MAXN],even[MAXN];void dfs_count(int u){    vis[u] = 1;    for(int i = head[u]; ~i; i = edge[i].next)    {        int v = edge[i].v;        if(!vis[v])        {            dfs_count(v);            odd[u] += odd[v];            even[u] += even[v];            link[i>>1].odd = odd[v];            link[i>>1].even = even[v];        }    }}//记录答案int ans_cnt;int ans[MAXN];int main(){    scanf("%d%d",&n,&m);    cnt = 0;    for(int i = 1; i <= n; ++i)head[i] = -1;    for(int i = 0; i < m; ++i)    {        scanf("%d%d",&link[i].u,&link[i].v);        add(link[i].u,link[i].v);        add(link[i].v,link[i].u);    }    for(int i = 1; i <= n; ++i)if(!vis[i])dfs(i,1);    int tot = 0;    for(int i = 0; i < m; ++i)    {        if(link[i].flag)continue;        int u = link[i].u;        int v = link[i].v;        if(deep[u] > deep[v])swap(u,v);        if((deep[v] - deep[u])&1)even[u]--,even[v]++;        else odd[u]--,odd[v]++,link[i].odd++,tot++;    }    //如果不存在奇环    if(!tot)    {        printf("%d\n",m);        for(int i = 0; i < m; ++i)printf("%d ",i+1);        return 0;    }    //存在奇环,先统计每条边所在的奇偶环数量    memset(vis,0,sizeof vis);    for(int i = 1; i <= n; ++i)if(!vis[i])dfs_count(i);    //统计答案    ans_cnt = 0;    for(int i = 0; i < m; ++i)    {        if(link[i].flag)//如果是竖边        {            if(!link[i].even && link[i].odd == tot)ans[ans_cnt++] = i+1;        }        else if(tot == 1 &&link[i].odd)ans[ans_cnt++] = i+1;    }    printf("%d\n",ans_cnt);    for(int i = 0; i < ans_cnt; ++i)printf("%d ",ans[i]);    return 0;}
原创粉丝点击