求最少加几条边使得该图变双连通图,有重边。
思路:求出双连通分量然后“缩点”之后去求叶子节点。判断入度为1的缩点,ans = (leaf+1)/2;注意判重边,因为重边可能影响Tarjan算法中的low[u]的值。
CODE:
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
using
namespace std;
#define MAXN 50010
#define MAXM 100010
struct Edge
{
int v, next;
}edge[MAXM];
int first[MAXN], stack[MAXN], ins[MAXN], dfn[MAXN], low[MAXN];
int belong[MAXM], ind[MAXN];
int n, m;
int cnt;
int scnt, top, tot;
int leaf;
void init()
{
cnt =
0;
scnt = top = tot =
0;
leaf =
0;
memset(first, -
1,
sizeof(first));
memset(dfn,
0,
sizeof(dfn));
memset(ins,
0,
sizeof(ins));
memset(ind,
0,
sizeof(ind));
}
void read_graph(
int u,
int v)
{
edge[cnt].v = v;
edge[cnt].next = first[u], first[u] = cnt++;
}
int check(
int u,
int father)
{
int tot =
0;
for(
int e = first[u]; e != -
1; e = edge[e].next)
{
int v = edge[e].v;
if(father == v) tot++;
}
if(tot >=
2)
return
1;
//
有重边
return
0;
//
无重边
}
/*
由于求DCC是在一个无向连通图中,即为双向的图,该father就是为了
防止某一节点又访问上一个节点(上一个节点搜出该节点)
*/
void Tarjan(
int u,
int father)
{
int v;
low[u] = dfn[u] = ++tot;
ins[u] =
1;
stack[top++] = u;
bool repeat = check(u, father);
for(
int e = first[u]; e != -
1; e = edge[e].next)
{
v = edge[e].v;
if(v == father && !repeat)
continue;
//
搜到上一个节点并且没有重复则跳过,如果重复则继续搜。
if(!dfn[v])
{
Tarjan(v, u);
low[u] = min(low[u], low[v]);
}
else
if(ins[v])
{
low[u] = min(low[u], dfn[v]);
}
}
if(low[u] == dfn[u])
{
scnt++;
do
{
v = stack[--top];
belong[v] = scnt;
ins[v] =
0;
}
while(v != u);
}
}
void solve()
{
init();
while(m--)
{
int u, v;
scanf(
"
%d%d
", &u, &v);
read_graph(u, v);
read_graph(v, u);
}
Tarjan(
1, -
1);
for(
int u =
1; u <= n; u++)
{
for(
int e = first[u]; e != -
1; e = edge[e].next)
{
int v = edge[e].v;
if(belong[u] != belong[v])
{
ind[belong[u]]++;
ind[belong[v]]++;
}
}
}
for(
int i =
1; i <= scnt; i++)
if(ind[i] ==
2) leaf++;
//
由于每个点重复计算了一次,所以叶子节点的度为2
printf(
"
%d\n
", (leaf+
1)/
2);
}
int main()
{
while(~scanf(
"
%d%d
", &n, &m))
{
solve();
}
return
0;
}
CODE2:
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
using
namespace std;
#define MAXN 50010
#define MAXM 100010
struct Edge
{
int v, next;
}edge[MAXM];
int first[MAXN], stack[MAXN], ins[MAXN], dfn[MAXN], low[MAXN];
int belong[MAXM], ind[MAXN];
int n, m;
int cnt;
int scnt, top, tot;
int leaf;
void init()
{
cnt =
0;
scnt = top = tot =
0;
leaf =
0;
memset(first, -
1,
sizeof(first));
memset(dfn,
0,
sizeof(dfn));
memset(ins,
0,
sizeof(ins));
memset(ind,
0,
sizeof(ind));
}
void read_graph(
int u,
int v)
{
for(
int e = first[u]; e != -
1; e = edge[e].next)
//
这样判重边也AC了。
{
if(edge[e].v == v)
return ;
}
edge[cnt].v = v;
edge[cnt].next = first[u], first[u] = cnt++;
}
/*
int check(int u, int father)
{
int tot = 0;
for(int e = first[u]; e != -1; e = edge[e].next)
{
int v = edge[e].v;
if(father == v) tot++;
}
if(tot >= 2) return 1; //有重边
return 0; //无重边
}
*/
/*
由于求DCC是在一个无向连通图中,即为双向的图,该father就是为了
防止某一节点又访问上一个节点(上一个节点搜出该节点)
*/
void Tarjan(
int u,
int father)
{
int v;
low[u] = dfn[u] = ++tot;
ins[u] =
1;
stack[top++] = u;
//
bool repeat = check(u, father);
for(
int e = first[u]; e != -
1; e = edge[e].next)
{
v = edge[e].v;
if(v == father)
continue;
//
搜到上一个节点并且没有重复则跳过,如果重复则继续搜。
if(!dfn[v])
{
Tarjan(v, u);
low[u] = min(low[u], low[v]);
}
else
if(ins[v])
{
low[u] = min(low[u], dfn[v]);
}
}
if(low[u] == dfn[u])
{
scnt++;
do
{
v = stack[--top];
belong[v] = scnt;
ins[v] =
0;
}
while(v != u);
}
}
void solve()
{
init();
while(m--)
{
int u, v;
scanf(
"
%d%d
", &u, &v);
read_graph(u, v);
read_graph(v, u);
}
Tarjan(
1, -
1);
for(
int u =
1; u <= n; u++)
{
for(
int e = first[u]; e != -
1; e = edge[e].next)
{
int v = edge[e].v;
if(belong[u] != belong[v])
{
ind[belong[u]]++;
ind[belong[v]]++;
}
}
}
for(
int i =
1; i <= scnt; i++)
if(ind[i] ==
2) leaf++;
//
由于每个点重复计算了一次,所以叶子节点的度为2
printf(
"
%d\n
", (leaf+
1)/
2);
}
int main()
{
while(~scanf(
"
%d%d
", &n, &m))
{
solve();
}
return
0;
}
CODE3:
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
using
namespace std;
#define MAXN 50010
#define MAXM 100010
struct Edge
{
int v, next;
}edge[MAXM];
int first[MAXN], stack[MAXN], ins[MAXN], dfn[MAXN], low[MAXN];
int belong[MAXM], ind[MAXN];
int n, m;
int cnt;
int scnt, top, tot;
int leaf;
void init()
{
cnt =
0;
scnt = top = tot =
0;
leaf =
0;
memset(first, -
1,
sizeof(first));
memset(dfn,
0,
sizeof(dfn));
memset(ins,
0,
sizeof(ins));
memset(ind,
0,
sizeof(ind));
}
void read_graph(
int u,
int v)
{
edge[cnt].v = v;
edge[cnt].next = first[u], first[u] = cnt++;
}
/*
int check(int u, int father)
{
int tot = 0;
for(int e = first[u]; e != -1; e = edge[e].next)
{
int v = edge[e].v;
if(father == v) tot++;
}
if(tot >= 2) return 1; //有重边
return 0; //无重边
}
*/
/*
由于求DCC是在一个无向连通图中,即为双向的图,该father就是为了
防止某一节点又访问上一个节点(上一个节点搜出该节点)
*/
void Tarjan(
int u,
int father)
{
int v;
low[u] = dfn[u] = ++tot;
ins[u] =
1;
stack[top++] = u;
bool repeat =
0;
for(
int e = first[u]; e != -
1; e = edge[e].next)
{
v = edge[e].v;
if(v == father && !repeat)
//
去掉一条反向边,其余可能是重边。
{
repeat =
1;
continue;
}
if(!dfn[v])
{
Tarjan(v, u);
low[u] = min(low[u], low[v]);
}
else
if(ins[v])
{
low[u] = min(low[u], dfn[v]);
}
}
if(low[u] == dfn[u])
{
scnt++;
do
{
v = stack[--top];
belong[v] = scnt;
ins[v] =
0;
}
while(v != u);
}
}
void solve()
{
init();
while(m--)
{
int u, v;
scanf(
"
%d%d
", &u, &v);
read_graph(u, v);
read_graph(v, u);
}
Tarjan(
1, -
1);
for(
int u =
1; u <= n; u++)
{
for(
int e = first[u]; e != -
1; e = edge[e].next)
{
int v = edge[e].v;
if(belong[u] != belong[v])
{
ind[belong[u]]++;
ind[belong[v]]++;
}
}
}
for(
int i =
1; i <= scnt; i++)
if(ind[i] ==
2) leaf++;
//
由于每个点重复计算了一次,所以叶子节点的度为2
printf(
"
%d\n
", (leaf+
1)/
2);
}
int main()
{
while(~scanf(
"
%d%d
", &n, &m))
{
solve();
}
return
0;
}