1. 程式人生 > >Codeforces Beta Round #19E. Fairy

Codeforces Beta Round #19E. Fairy

each wan index write trie property all ios tdi

二分圖判定的性質 圖中是否存在奇環 若無則是二分圖 否則不是 那麽我們考慮維護圖中哪些環邊會構成奇環 我們可以LCT去做 把形成的環邊選出來 然後對於奇環上的邊+1 偶環上的邊-1 最後邊的權值等於奇環數量那麽這條邊可以被選定(我的LCT常數有點大啊 但是CF上好像92ms bzoj上T掉了 又不想寫0(n)的特技差分 那就隊友去寫吧2333333 反正算法思路好像是沒有問題的

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define link(x) for(edge *j=h[x];j;j=j->next)
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,r,l) for(int i=r;i>=l;i--)
const int MAXN=3e4+10;
const double eps=1e-8;
const int inf=1e9;
#define ll long long
using namespace std;
struct FastIO
{
    static const int S=200;
    int wpos;
    char wbuf[S];
    FastIO():wpos(0){}
    inline int xchar()
    {
        static char buf[S];
        static int len=0,pos=0;
        if(pos==len) pos=0,len=fread(buf,1,S,stdin);
        if(pos==len) exit(0);
        return buf[pos++];
    }
    inline int read()
    {
        int s=1,c=xchar(),x=0;
        while(c<=32) c=xchar();
        if(c==‘-‘) s=-1,c=xchar();
        for(;‘0‘<=c&&c<=‘9‘;c=xchar()) x=x*10+c-‘0‘;
        return x*s;
    }
    ~FastIO()
    {
        if(wpos) fwrite(wbuf,1,wpos,stdout),wpos=0;
    }
}io;
int pre[MAXN],ch[MAXN][2],res[MAXN],key[MAXN],minn[MAXN],size[MAXN],add[MAXN],sum[MAXN];
bool rt[MAXN];
inline void newnode(int x,int vul){add[x]=sum[x]=pre[x]=ch[x][0]=ch[x][1]=res[x]=0;key[x]=vul;minn[x]=x;rt[x]=1;size[x]=1;}
inline void reverse(int r){
	if(!r)return ;
	swap(ch[r][0],ch[r][1]);
	res[r]^=1;
}
inline void Add(int x,int vul){
	if(!x)return ;
	sum[x]+=vul;add[x]+=vul;
}
inline void push(int x){
	if(res[x]){
		reverse(ch[x][0]);
		reverse(ch[x][1]);
		res[x]^=1;
	}
	if(add[x]!=0){
		Add(ch[x][0],add[x]);
		Add(ch[x][1],add[x]);
		add[x]=0;
	}
}
inline void up(int x){
	minn[x]=x;
	if(key[minn[ch[x][0]]]<key[minn[x]])minn[x]=minn[ch[x][0]];
	if(key[minn[ch[x][1]]]<key[minn[x]])minn[x]=minn[ch[x][1]];
	size[x]=size[ch[x][0]]+size[ch[x][1]]+1;
}
inline void P(int x){
	if(!rt[x])P(pre[x]);
	push(x);
}
inline void rotate(int x,int kind){
	int y=pre[x];
	ch[y][!kind]=ch[x][kind];pre[ch[x][kind]]=y;
	if(rt[y])rt[x]=1,rt[y]=0;
	else ch[pre[y]][ch[pre[y]][1]==y]=x;
	pre[x]=pre[y];ch[x][kind]=y;pre[y]=x;
	up(y);
}
inline void splay(int x){
	P(x);
	while(!rt[x]){
		if(rt[pre[x]])rotate(x,ch[pre[x]][0]==x);
		else{
			int y=pre[x];int kind=ch[pre[y]][0]==y;
			if(ch[y][kind]==x)rotate(x,!kind),rotate(x,kind);
			else rotate(y,kind),rotate(x,kind);
		}
	}
	up(x);	
}
inline void access(int x){
	int y=0;
	while(x){
		splay(x);
		if(ch[x][1])pre[ch[x][1]]=x,rt[ch[x][1]]=1;
		ch[x][1]=y;up(x);
		if(y)rt[y]=0;
		y=x;x=pre[x];
	}
}
inline void mroot(int u){access(u);splay(u);reverse(u);}
inline void destory(int x){access(x);splay(x);pre[ch[x][1]]=pre[ch[x][0]]=0;rt[ch[x][1]]=rt[ch[x][0]]=1;ch[x][0]=ch[x][1]=0;}
inline void Link(int u,int v){mroot(u);pre[u]=v;}
inline int querty(int u,int v){mroot(u);access(v);splay(v);return minn[v];}
inline int Querty(int u,int v){mroot(u);access(v);splay(v);return (size[v]+1)/2;}
inline void update(int u,int v,int vul){mroot(u);access(v);splay(v);Add(v,vul);}
inline bool pd(int u,int v){
	while(pre[u])u=pre[u];
	while(pre[v])v=pre[v];
	return u==v;
}
vector<int>v1,ans;
pii d[MAXN];
int main(){
	key[0]=inf;minn[0]=0;
	int n,m;n=io.read();m=io.read();
	inc(i,1,n)newnode(i,inf);
	int cnt=0;
	inc(i,1,m){
		d[i].first=io.read();d[i].second=io.read();newnode(n+i,i);
		if(d[i].first==d[i].second){cnt++;sum[i+n]++;continue;}
		if(pd(d[i].first,d[i].second)){
			int sz=Querty(d[i].first,d[i].second);int t1=querty(d[i].first,d[i].second);v1.pb(t1-n);destory(t1);
		}
		Link(d[i].first,n+i);
		Link(d[i].second,n+i);
	}
	for(int i=0;i<v1.size();i++){
		int t1=Querty(d[v1[i]].first,d[v1[i]].second);
		if(t1%2)cnt++,update(d[v1[i]].first,d[v1[i]].second,1),sum[v1[i]+n]++;
		else update(d[v1[i]].first,d[v1[i]].second,-1);
	}
	if(!cnt){
		printf("%d\n",m);
		for(int i=1;i<=m;i++){
			if(i==1)printf("%d",i);
			else printf(" %d",i);
		}
		puts("");
		return 0;
	}
	for(int i=1;i<=m;i++)mroot(i+n);
	inc(i,1,m)if(sum[i+n]==cnt)ans.pb(i);
	printf("%d\n",ans.size());
	for(int i=0;i<ans.size();i++){
		if(i==0)printf("%d",ans[i]);
		else printf(" %d",ans[i]);
	}
	puts("");
}

 

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

Examples input Copy
4 4
1 2
1 3
2 4
3 4
output Copy
4
1 2 3 4
input Copy
4 5
1 2
2 3
3 4
4 1
1 3
output Copy
1
5

Codeforces Beta Round #19E. Fairy