Ferry Kingdom is a nice little country located on N islands that are connected by M bridges. All bridges are very beautiful and are loved by everyone in the kingdom. Of course, the system of bridges is designed in such a way that one can get from any island to any other one.
But recently the great sorrow has come to the kingdom. Ferry Kingdom was conquered by the armies of the great warrior Jordan and he has decided to burn all the bridges that connected the islands. This was a very cruel decision, but the wizards of Jordan have advised him no to do so, because after that his own armies would not be able to get from one island to another. So Jordan decided to burn as many bridges as possible so that is was still possible for his armies to get from any island to any other one.
Now the poor people of Ferry Kingdom wonder what bridges will be burned. Of course, they cannot learn that, because the list of bridges to be burned is kept in great secret. However, one old man said that you can help them to find the set of bridges that certainly will not be burned.
So they came to you and asked for help. Can you do that?
Input
The input contains multiple test cases. The first line of the input is a single integer T (1 <= T <= 20) which is the number of test cases. T test cases follow, each preceded by a single blank line.
The first line of each case contains N and M - the number of islands and bridges in Ferry Kingdom respectively (2 <= N <= 10 000, 1 <= M <= 100 000). Next M lines contain two different integer numbers each and describe bridges. Note that there can be several bridges between a pair of islands.
Output
On the first line of each case print K - the number of bridges that will certainly not be burned. On the second line print K integers - the numbers of these bridges. Bridges are numbered starting from one, as they are given in the input.
Two consecutive cases should be separated by a single blank line. No blank line should be produced after the last test case.
Sample Input
2 6 7 1 2 2 3 2 4 5 4 1 3 4 5 3 6 10 16 2 6 3 7 6 5 5 9 5 4 1 2 9 8 6 4 2 10 3 8 7 9 1 4 2 4 10 5 1 6 6 10
Sample Output
2 3 7 1 4
题意:
tarjan算法求桥:
先看一个TLE代码:建立所有边,在tarjan算法里面判断重边并且标记桥,然后枚举所有边输出标记的边的序号。这个思想。
#include <cstdio>
#include <cstring>
#include <stack>
#include <algorithm>
#define MAXN 10000+10
#define MAXM 200000+10
using namespace std;
struct Edge
{
int from, to, next, id;//加上边的序号
}edge[MAXM];
int N, M;
int head[MAXN], top;
int low[MAXN], dfn[MAXN];
int bridge;//桥总数目
int recdfn;
int mark[MAXN];//mark[i]标记第i+1条边是否为割边
void init()
{
top = 0;
memset(head, -1, sizeof(head));
}
void addEdge(int u, int v, int ID)
{
Edge E = {u, v, head[u], ID};
edge[top] = E;
head[u] = top++;
Edge E1 = {v, u, head[v], ID};
edge[top] = E1;
head[v] = top++;
}
void getMap()
{
int a, b;
for(int i = 0; i < M; i++)
{
mark[i] = 0;
scanf("%d%d", &a, &b);
addEdge(a, b, i);
}
}
void tarjan(int u, int fa)
{
low[u] = dfn[u] = ++recdfn;
int have = 0;
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if(!have && v == fa)
{
have = 1;
continue;
}
if(!dfn[v])
{
tarjan(v, u);
low[u] = min(low[u], low[v]);
if(low[v] > dfn[u])
{
int ID = edge[i].id;
if(!mark[ID]) bridge++;
mark[ID] = 1;
}
}
else low[u] = min(low[u], dfn[v]);
}
}
void find_edgecut(int l, int r)
{
memset(low, 0, sizeof(low));
memset(dfn, 0, sizeof(dfn));
recdfn = 0; bridge = 0;
for(int i = l; i <= r; i++)
if(!dfn[i]) tarjan(i, i);
}
void output(int m)
{
printf("%d\n", bridge);
if(bridge == 0)
return ;
int k = 0;
for(int i = 0; i < m; i++)
{
if(mark[i])
{
if(k > 0) printf(" ");
printf("%d", i+1);
k++;
}
}
printf("\n");
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &N, &M);
init();
getMap();
find_edgecut(1, N);
output(M);
if(t)
printf("\n");
}
return 0;
}
改进了一下,不标记桥了,直接用数组存储桥对应边的序号,然后升序排列数组后,输出即可,但还是TLE。。。 原因:建边太多。。。
TLE代码:
#include <cstdio>
#include <cstring>
#include <stack>
#include <algorithm>
#define MAXN 10000+10
#define MAXM 200000+10
using namespace std;
struct Edge
{
int from, to, next, id;//加上边的序号
}edge[MAXM];
int N, M;
int head[MAXN], top;
int low[MAXN], dfn[MAXN];
int bridge;//桥总数目
int recdfn;
int mark[MAXN];//mark[i]标记第i+1条边是否为割边
int cut_edge[MAXN];//存储桥的序号
void init()
{
top = 0;
memset(head, -1, sizeof(head));
}
void addEdge(int u, int v, int ID)
{
Edge E = {u, v, head[u], ID};
edge[top] = E;
head[u] = top++;
Edge E1 = {v, u, head[v], ID};
edge[top] = E1;
head[v] = top++;
}
void getMap()
{
int a, b;
for(int i = 1; i <= M; i++)
{
mark[i] = 0;
scanf("%d%d", &a, &b);
addEdge(a, b, i);
}
}
void tarjan(int u, int fa)
{
low[u] = dfn[u] = ++recdfn;
int have = 0;
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if(!have && v == fa)
{
have = 1;
continue;
}
if(!dfn[v])
{
tarjan(v, u);
low[u] = min(low[u], low[v]);
if(low[v] > dfn[u])
{
int ID = edge[i].id;
cut_edge[bridge++] = ID;
}
}
else low[u] = min(low[u], dfn[v]);
}
}
void find_edgecut()
{
memset(low, 0, sizeof(low));
memset(dfn, 0, sizeof(dfn));
recdfn = 0; bridge = 0;
tarjan(1, 1);//连通图一次就够了
}
void output()
{
printf("%d\n", bridge);
if(bridge == 0)
return ;
sort(cut_edge, cut_edge+bridge);
for(int i = 0; i < bridge; i++)
{
if(i > 0) printf(" ");
printf("%d", cut_edge[i]);
}
printf("\n");
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &N, &M);
init();
getMap();
find_edgecut();
output();
if(t)
printf("\n");
}
return 0;
}
没办法,不能建太多边,因此我就在建边过程中增加了对重边的处理。
处理方式:对于建立的每一条边判断建边后是否会出现重边,若出现重边,那么该边不再建立,并标记那个已经建立的边;反之建边。
第一个AC代码:采用存储桥对应边的序号1110ms
#include <cstdio>
#include <cstring>
#include <stack>
#include <algorithm>
#define MAXN 20000+10
#define MAXM 220000+10
using namespace std;
struct Edge
{
int from, to, next, num, id;//加上边的序号
}edge[MAXM];
int N, M;
int head[MAXN], top;
int low[MAXN], dfn[MAXN];
int bridge;//桥总数目
int recdfn;//记录每一个深度优先数
int cut_edge[MAXN];//存储桥的序号
void init()
{
top = 0;
memset(head, -1, sizeof(head));
}
void addEdge(int u, int v, int ID)
{
int i;
for(i = head[u]; i != -1; i = edge[i].next)//判断是否有重边
{
if(edge[i].to == v)
break;
}
if(i != -1)//重边 num标记为1
{
edge[i].num = 1;
}
else//否则建立新边
{
Edge E = {u, v, head[u], 0, ID};
edge[top] = E;
head[u] = top++;
}
// Edge E = {u, v, head[u], ID};
// edge[top] = E;
// head[u] = top++;
// Edge E1 = {v, u, head[v], ID};
// edge[top] = E1;
// head[v] = top++;
}
void getMap()
{
int a, b;
for(int i = 1; i <= M; i++)
{
scanf("%d%d", &a, &b);
addEdge(a, b, i);
addEdge(b, a, i);
}
}
void tarjan(int u, int fa)
{
low[u] = dfn[u] = ++recdfn;
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if(v == fa) continue;
if(!dfn[v])
{
tarjan(v, u);
low[u] = min(low[u], low[v]);
if(low[v] > dfn[u] && edge[i].num == 0)
{
int ID = edge[i].id;
cut_edge[bridge++] = ID;//存储边的序号
}
}
else low[u] = min(low[u], dfn[v]);
}
}
void find_edgecut(int l, int r)
{
memset(low, 0, sizeof(low));
memset(dfn, 0, sizeof(dfn));
recdfn = 0; bridge = 0;
tarjan(1, 0);//题中说 这是连通图 一次就够了
}
void output()
{
printf("%d\n", bridge);
if(bridge == 0)
return ;
sort(cut_edge, cut_edge+bridge);//升序排列
for(int i = 0; i < bridge; i++)
{
if(i > 0) printf(" ");
printf("%d", cut_edge[i]);
}
printf("\n");
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &N, &M);
init();
getMap();
find_edgecut(1, N);
output();
if(t)
printf("\n");
}
return 0;
}
第二个AC代码:标记边 1120ms
#include <cstdio>
#include <cstring>
#include <stack>
#include <algorithm>
#define MAXN 20000+10
#define MAXM 220000+10
using namespace std;
struct Edge
{
int from, to, next, num, id;//加上边的序号
}edge[MAXM];
int N, M;
int head[MAXN], top;
int low[MAXN], dfn[MAXN];
int bridge;//桥总数目
int recdfn;//记录每一个深度优先数
int mark[MAXM];//标记边
void init()
{
top = 0;
memset(head, -1, sizeof(head));
}
void addEdge(int u, int v, int ID)
{
int i;
for(i = head[u]; i != -1; i = edge[i].next)//判断是否有重边
{
if(edge[i].to == v)
break;
}
if(i != -1)//重边 num标记为1
{
edge[i].num = 1;
}
else//否则建立新边
{
Edge E = {u, v, head[u], 0, ID};
edge[top] = E;
head[u] = top++;
}
// Edge E = {u, v, head[u], ID};
// edge[top] = E;
// head[u] = top++;
// Edge E1 = {v, u, head[v], ID};
// edge[top] = E1;
// head[v] = top++;
}
void getMap()
{
int a, b;
for(int i = 1; i <= M; i++)
{
mark[i] = 0;
scanf("%d%d", &a, &b);
addEdge(a, b, i);
addEdge(b, a, i);
}
}
void tarjan(int u, int fa)
{
low[u] = dfn[u] = ++recdfn;
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if(v == fa) continue;
if(!dfn[v])
{
tarjan(v, u);
low[u] = min(low[u], low[v]);
if(low[v] > dfn[u] && edge[i].num == 0)
{
int ID = edge[i].id;
bridge++;
mark[ID] = 1;
}
}
else low[u] = min(low[u], dfn[v]);
}
}
void find_edgecut(int l, int r)
{
memset(low, 0, sizeof(low));
memset(dfn, 0, sizeof(dfn));
recdfn = 0; bridge = 0;
tarjan(1, 0);//题中说 这是连通图 一次就够了
}
void output()
{
printf("%d\n", bridge);
if(bridge == 0)
return ;
int k = 0;
for(int i = 1; i <= M; i++)
{
if(mark[i])
{
if(k > 0) printf(" ");
printf("%d", i);
k++;
}
}
printf("\n");
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &N, &M);
init();
getMap();
find_edgecut(1, N);
output();
if(t)
printf("\n");
}
return 0;
}