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 = ; int n,m,head[maxn],to[maxn],nextt[maxn],id[maxn],tot = ,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 << ],R[maxn << ],sum1[maxn << ],sum2[maxn << ],tag1[maxn << ],tag2[maxn << ]; 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] + ;
sizee[u] = ;
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 / ) + (i % );
vis[(i / ) + (i % )] = ;
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) >> ;
build(o * ,l,mid);
build(o * + ,mid + ,r);
} void pushup(int o)
{
sum1[o] = sum1[o * ] + sum1[o * + ];
sum2[o] = sum2[o * ] + sum2[o * + ];
} void pushdown(int o)
{
if (tag1[o])
{
tag1[o * ] += tag1[o];
tag1[o * + ] += tag1[o];
sum1[o * ] += tag1[o] * (R[o * ] - L[o * ] + );
sum1[o * + ] += tag1[o] * (R[o * + ] - L[o * + ] + );
tag1[o] = ;
}
if (tag2[o])
{
tag2[o * ] += tag2[o];
tag2[o * + ] += tag2[o];
sum2[o * ] += tag2[o] * (R[o * ] - L[o * ] + );
sum2[o * + ] += tag2[o] * (R[o * + ] - L[o * + ] + );
tag2[o] = ;
}
} void update1(int o,int l,int r,int x,int y)
{
if(x <= l && r <= y)
{
sum1[o] += (r - l + );
tag1[o]++;
return;
}
pushdown(o);
int mid = (l + r) >> ;
if (x <= mid)
update1(o * ,l,mid,x,y);
if (y > mid)
update1(o * + ,mid + ,r,x,y);
} void update2(int o,int l,int r,int x,int y)
{
if(x <= l && r <= y)
{
sum2[o] += (r - l + );
tag2[o]++;
return;
}
pushdown(o);
int mid = (l + r) >> ;
if (x <= mid)
update2(o * ,l,mid,x,y);
if (y > mid)
update2(o * + ,mid + ,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 == )
update1(,,n,pos[t],pos[x]);
else
update2(,,n,pos[t],pos[x]);
x = fa[t];
}
if (x == y)
return;
if (h[x] < h[y])
swap(x,y);
if (tagg == )
update1(,,n,pos[y] + ,pos[x]); //为什么要+1?因为实际维护的是边.
else
update2(,,n,pos[y] + ,pos[x]);
} int query1(int o,int l,int r,int v)
{
if (l == r)
return sum1[o];
pushdown(o);
int mid = (l + r) >> ;
if (v <= mid)
return query1(o * ,l,mid,v);
else
return query1(o * + ,mid + ,r,v);
} int query2(int o,int l,int r,int v)
{
if (l == r)
return sum2[o];
pushdown(o);
int mid = (l + r) >> ;
if (v <= mid)
return query2(o * ,l,mid,v);
else
return query2(o * + ,mid + ,r,v);
} int main()
{
scanf("%d%d",&n,&m);
for (int i = ; 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 = ; i <= n; i++)
if (!h[i])
dfs1(i,),dfs2(i,i);
build(,,n);
for (int i = ; i <= m; i++)
{
if (!vis[i])
{
int x = e[i].x,y = e[i].y;
if (abs(h[x] - h[y]) % == ) //偶环
{
cntb++;
change(x,y,-);
}
else
{
cnta++;
flag = i;
change(x,y,);
}
}
}
if (cnta == )
{
for (int i = ; i <= m; i++)
anss[++ans] = i;
}
else
{
if (cnta == )
anss[++ans] = flag;
for (int i = ; i <= n; i++)
{
if (query1(,,n,pos[i]) == cnta && query2(,,n,pos[i]) == )
anss[++ans] = bianhao[i];
}
sort(anss + ,anss + + ans);
}
printf("%d\n",ans);
for(int i = ; i <= ans; i++)
printf("%d ",anss[i]);
printf("\n"); return ;
}
    

Codeforces 19.E Fairy的更多相关文章

  1. codeforces 19 D. Points(线段树+set二分)

    题目链接:http://codeforces.com/contest/19/problem/D 题意:给出3种操作:1)添加点(x,y),2)删除点(x,y),3)查询离(x,y)最近的右上方的点. ...

  2. Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale 二分

    C. Anton and Fairy Tale 题目连接: http://codeforces.com/contest/785/problem/C Description Anton likes to ...

  3. Codeforces Gym100735 I.Yet another A + B-Java大数 (KTU Programming Camp (Day 1) Lithuania, Birˇstonas, August 19, 2015)

    I.Yet another A + B You are given three numbers. Is there a way to replace variables A, B and C with ...

  4. Codeforces Gym100735 G.LCS Revised (KTU Programming Camp (Day 1) Lithuania, Birˇstonas, August 19, 2015)

    G.LCS Revised   The longest common subsequence is a well known DP problem: given two strings A and B ...

  5. Codeforces Gym100735 E.Restore (KTU Programming Camp (Day 1) Lithuania, Birˇstonas, August 19, 2015)

    E - Restore Given a matrix A of size N * N. The rows are numbered from 0 to N-1, the columns are num ...

  6. Educational Codeforces Round 19 A, B, C, E(xjb)

    题目链接:http://codeforces.com/contest/797 A题 题意:给出两个数n, k,问能不能将n分解成k个因子相乘的形式,不能输出-1,能则输出其因子: 思路:将n质因分解, ...

  7. 【19.77%】【codeforces 570D】Tree Requests

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. 【19.46%】【codeforces 551B】ZgukistringZ

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. 【codeforces 785C】Anton and Fairy Tale

    [题目链接]:http://codeforces.com/contest/785/problem/C [题意] 容量为n的谷仓,每一天都会有m个谷子入仓(满了就视为m);第i天 会有i只鸟叼走i个谷子 ...

随机推荐

  1. jquery中国地图插件

    插件下载地址: http://www.17sucai.com/preview/1266961/2018-09-18/map/js/jsMap-1.1.0.min.js jsMap 项目介绍 这是一个功 ...

  2. Loadrunner教程--常用操做流程

    1loadrunner压力测试一般使用流程 1.1loadrunner压力测试原理 本质就是在loadrunner上模拟多个用户同时按固定行为访问web站点.其中固定行为在loadrunner中是通过 ...

  3. 第18次Scrum会议(10/30)【欢迎来怼】

    一.小组信息 队名:欢迎来怼小组成员队长:田继平成员:李圆圆,葛美义,王伟东,姜珊,邵朔,冉华 小组照片 二.开会信息 时间:2017/10/30 17:19~17:38,总计19min.地点:东北师 ...

  4. 基于NABCD评论探路者团队贪吃蛇作品及改进建议

    1.根据(不限于)NABCD评论作品的选题 N:随着人们生活压力越来越大,需要去去缓解压力,并且也需要不断进步,学习英语知识. A:它是基于java开发的一款软件,采用逐个吃字母,加长蛇身,增强记忆的 ...

  5. 【探路者】团队Alpha周贡献分数分配结果

    经本组成员商议,根据老师提供的分数,(每人携带10分进入团队,[探路者]团队7人,共计35分). 本周每位同学携带10分进入组内,7人共计70分.分数公布如下: 吴雨丹 15分 贾雅杰 12分 蔺依铭 ...

  6. Right-BICEP测试四则运算2

    根据Right-BICEP单元测试的方法,我对我写的四则运算2的程序进行了测试: 1.测试能否控制使用乘除 有乘除 无乘除 2.测试是否能加括号 不加括号 加括号 3.能否控制结果没有负数 无负数 4 ...

  7. OOP 1.5 类和对象的基本概念与用法1

    1.定义 面向对象的基本特点:抽象.封装.继承.多态 面向对象程序设计方法:将某类客观事物的共同特点归纳出来,形成一个数据结构 抽象:将事物所能进行的行为归纳出来,形成一个个函数,这些函数可以用来操作 ...

  8. C#高级编程 (第六版) 学习 第四章:继承

    第四章 继承 1,继承的类型 实现继承: 一个类派生于一个基类型,拥有该基类型所有成员字段和函数. 接口继承 一个类型只继承了函数的签名,没有继承任何实现代码.   2,实现继承 class MyDe ...

  9. 浅谈 Sql Server 游标

    查询语句可能返回多条记录,如果数据量非常大,需要使用游标来逐条读取查询结果集中的记录.应用程序可以根据需要滚动或浏览其中的数据.本篇介绍游标的概念.分类.以及基本操作等内容. 一:认识游标游标是SQL ...

  10. DNS服务器的解析

    ---恢复内容开始--- DNS前言: 英特网作为域名和IP地址相互映射的一个分不式数据库,能够使用户更方便的访问互联网.而不用去记住能够被机器直接读取的IP地址的过程叫做域名解析(或主机名解析).D ...