codeforces 19E Fairy

216 篇文章 0 订阅
26 篇文章 0 订阅

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
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 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

二分图+lca+并查集+神奇的思路~

神奇的二分图性质:没有奇环。所以要得到二分图就是把奇环去掉。

另外,把图建成一棵树,那么连接了树中已有的两个点的边是返祖边,返祖边与树边构成环,这里返祖边的判断可以用并查集。

对于奇环(u,v),找到其最近公共祖先f,将u点+1,v点+1,f点-2。

一条边(u, fa(u))被覆盖的次数等于u点的子树和。

(另:程序在BZOJ上是A的,但在codeforces上的最后一组数据被hack掉了……)


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int n,m,fa[1000001],fi[1000001],ne[2000001],w[2000001],id[2000001],f[1000001][21],cnt,deep[1000001],c[1000001],tot,ans[2000001],las,d[1000001];
bool b[1000001];

struct node{
	int x,y,b;
}ed[2000001];

void add(int u,int v,int idd)
{
	w[++cnt]=v;ne[cnt]=fi[u];id[cnt]=idd;fi[u]=cnt;
}

void added(int u,int v,int idd)
{
	add(u,v,idd);add(v,u,idd);
}

int findd(int u)
{
	return fa[u]==u ? u:fa[u]=findd(fa[u]);
}

void init()
{
	for(int i=1;i<=n;i++)
	  for(int j=1;j<=20;j++) f[i][j]=f[f[i][j-1]][j-1];
}

int lca(int u,int v)
{
	if(deep[u]<deep[v]) swap(u,v);
	for(int i=20;i>=0;i--)
	  if((deep[u]-(1<<i))>=deep[v]) u=f[u][i];
	if(u==v) return u;
	for(int i=20;i>=0;i--)
	  if(f[u][i]!=f[v][i]) u=f[u][i],v=f[v][i];
	return f[u][0];
}

void build(int u)
{
	b[u]=1;
	for(int i=fi[u];i;i=ne[i])
	  if(w[i]!=f[u][0])
	  {
	  	deep[w[i]]=deep[u]+1;f[w[i]][0]=u;d[w[i]]=i;build(w[i]);
	  }
}

void addnum(int u)
{
	b[u]=1;
	for(int i=fi[u];i;i=ne[i])
	  if(w[i]!=f[u][0])
	  {
	  	addnum(w[i]);c[u]+=c[w[i]];
	  }
}

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) fa[i]=i;
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&ed[i].x,&ed[i].y);
		int k1=findd(ed[i].x),k2=findd(ed[i].y);
		if(k1==k2) ed[i].b=1;
		else added(ed[i].x,ed[i].y,i),ed[i].b=0,fa[k1]=k2;
	}
	for(int i=1;i<=n;i++) if(!b[i]) build(i);init();
	for(int i=1;i<=m;i++)
	  if(ed[i].b)
	  {
	  	int lc=lca(ed[i].x,ed[i].y);
	  	int f=-1;
	  	if(!((deep[ed[i].x]+deep[ed[i].y])%2)) f=1,tot++,las=i;
	  	c[ed[i].x]+=f;c[ed[i].y]+=f;c[lc]-=2*f;
	  }
	if(!tot)
	{
		printf("%d\n",m);
		for(int i=1;i<m;i++) printf("%d ",i);
		if(m) printf("%d\n",m);return 0;
	}
	memset(b,0,sizeof(b));
	for(int i=1;i<=n;i++) if(!b[i]) addnum(i);
	for(int i=1;i<=n;i++) if(c[i]==tot) ans[++ans[0]]=id[d[i]];
	if(tot==1) ans[++ans[0]]=las;
	printf("%d\n",ans[0]);sort(ans+1,ans+ans[0]+1);
	for(int i=1;i<ans[0];i++) printf("%d ",ans[i]);
	if(ans[0]) printf("%d\n",ans[ans[0]]);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值