zoj2588(割边)

/*
* this code is made by LinMeiChen
* Problem:zoj 1311
* Type of Problem:
* Thinking:
* Feeling:
*/
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<string>
#include<vector>
#include<queue>
#include<list>
using namespace std;
typedef long long lld;
typedef unsigned int ud;
#define oo 0x3f3f3f3f
using namespace std;
#define maxn 10010
#define maxm 200010
int mark[maxn], head[maxn];
int dfn[maxn*2], low[maxn*2];
int n, g_dfn, n_bg;
struct Edge
{
int v, next, id, tag;
}E[maxm];
int tol;
int bg[maxn];


void DFS(int u, int fa)
{
mark[u] = 1;
dfn[u] = low[u] = ++g_dfn;
for (int i = head[u]; i != -1; i = E[i].next)
{
int v = E[i].v;
if (!mark[v])
{
DFS(v, u);
low[u] = min(low[u], low[v]);
if (low[v] > dfn[u] && !E[i].tag)
bg[E[i].id] = ++n_bg;
}
else if (v != fa)
low[u] = min(low[u], dfn[v]);
}
}


void add(int u, int v, int id)
{
for (int i = head[u]; i != -1; i = E[i].next)
{
if (E[i].v == v)
{
E[i].tag++;
return;
}
}


E[tol].v = v;
E[tol].id = id;
E[tol].next = head[u];
head[u] = tol++;
}


void add_edge(int u, int v, int id)
{
add(u, v, id);
add(v, u, id);
}


void Init()
{
for (int i = 0; i < maxm; i++)
E[i].tag = 0;
memset(mark, 0, sizeof mark);
memset(head, -1, sizeof head);
memset(dfn, 0, sizeof dfn);
memset(low, 0, sizeof low);
memset(bg, 0, sizeof bg);
g_dfn = 1;
n_bg = 0;
tol = 0;
dfn[1] = low[1] = 1;
}


int main()
{
int T, n, m, u, v;
scanf("%d", &T);
while (T--)
{
Init();
scanf("%d%d", &n, &m);
for (int i = 1; i <= m;i++)
{
scanf("%d%d", &u, &v);
add_edge(u, v, i);
}
DFS(1, -1);
printf("%d\n", n_bg);
bool first = true;
for (int i = 1; i <= m; i++)
{
if (bg[i])
{
if (first)
first = false;
else
printf(" ");
printf("%d", i);
}
}
puts("");
if (T)
printf("\n");
}
return 0;
}

你可能感兴趣的:(算法,网络,ZOJ)