time limit per test : 1.5 seconds
memory limit per test : 256 megabytes
分数:2800
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)(1 ≤ n ≤ 10^4, 0 ≤ m ≤ 10^4)(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)v, u (1 ≤ v ≤ n, 1 ≤ u ≤ n)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
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
题意:
给定一张图,你可以选择一条边删去,使得整个图变为一个二分图。求出所有这样的边。
题解:
我们可以先按照给图染色的方式遍历整个图。我们将边分成这几类,一种是树边,一种是非树边,一种是坏边(连接两个同色的点),一种是非坏边(非树边中连接两个不同色的点)。如果坏边的数量等于0,那么我们可以删除所有的非树边。如果坏边的数量等于1,那么我们可以删除所有的坏边。如果坏边的数量大于1,我们可以删除任意一条非树边。
现在只要处理出这些东西就行了。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,m;
struct edge{
int to,nt,w;
}e[100004];
int ne,h[10004];
int flag,vis[10004],brk[100004];
ll f[10004],col[10004];
void add(int u,int v,int w){
e[++ne].to=v;e[ne].w=w;
e[ne].nt=h[u];h[u]=ne;
}
void dfs(int x,int fa){
vis[x]=1;
for(int i=h[x];i;i=e[i].nt){
if(e[i].to==fa||col[x]<col[e[i].to])continue;
else if(!vis[e[i].to]){
col[e[i].to]=col[x]+1;
dfs(e[i].to,x);
f[x]+=f[e[i].to];
brk[i]=f[e[i].to];
}
else if((col[x]-col[e[i].to])%2==1){
f[x]--;f[e[i].to]++;
}
else{
f[x]++;f[e[i].to]--;
brk[i]=1;
flag++;
}
}
}
int main(){
scanf("%d%d",&n,&m);
ne=0;flag=0;
memset(h,0,sizeof(h));
for(int i=1;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
add(u,v,i);
add(v,u,i);
}
for(int i=1;i<=n;i++){
if(!vis[i])dfs(i,0);
}
if(!flag){
printf("%d\n",m);
for(int i=1;i<=ne;i+=2){
printf("%d ",e[i].w);
}
puts("");
}
else{
vector<int>ans;ans.clear();
for(int i=1;i<=ne;i++){
if(brk[i]==flag){
ans.push_back(e[i].w);
}
}
printf("%d\n",ans.size());
for(int i=0;i<ans.size();i++){
printf("%d ",ans[i]);
}
if(ans.size())puts("");
}
return 0;
}