【Description】
Yixght is a manager of the company called SzqNetwork(SN). Now she’s very worried because she has just received a bad news which denotes that DxtNetwork(DN), the SN’s business rival, intents to attack the network of SN. More unfortunately, the original network of SN is so weak that we can just treat it as a tree. Formally, there are N nodes in SN’s network, N-1 bidirectional channels to connect the nodes, and there always exists a route from any node to another. In order to protect the network from the attack, Yixght builds M new bidirectional channels between some of the nodes.
As the DN’s best hacker, you can exactly destory two channels, one in the original network and the other among the M new channels. Now your higher-up wants to know how many ways you can divide the network of SN into at least two parts.
【Input】
The first line of the input file contains two integers: N (1 ≤ N ≤ 100 000), M (1 ≤ M ≤ 100 000) — the number of the nodes and the number of the new channels.
Following N-1 lines represent the channels in the original network of SN, each pair (a,b) denote that there is a channel between node a and node b.
Following M lines represent the new channels in the network, each pair (a,b) denote that a new channel between node a and node b is added to the network of SN.
【Output】
Output a single integer — the number of ways to divide the network into at least two parts.
【Sample Input】
4 1
1 2
2 3
1 4
3 4
【Sample Output】
3
【题目大意】给出一张n个点、m条边的无向图,现有Q个询问,每个询问给出一条边 (u,v),求加入这条边后图中桥(割边)的数量。
【题解】tarjan+并查集
虽然题目给出了一张无向图,但由于要求的是割边数量,环显然是无用的。所以先进行tarjan缩点,得到一棵树,树上的边即为初始的割边。
对于询问给出的边 (u,v),有两种情况:
<1>若u,v两点在一个连通块内,此边对割边数量没有影响;
<2>若u,v两点不在一个连通块内,则加入这条边之后会形成一个新的环,这个环上的其他边就是减少的割边。
考虑情况<2>,在形成新环后,我们从u,v两点向上走。一直走到两点的公共祖先后停止,经过的路径就是环上的边。由于整棵树的边数小于n,暴力地扫描这些边就可以了。扫描完后,将路径上的点合并。合并时用到并查集。
//详见程序。
#include
#include
#include
#define N 100005
struct edge{ int to,nxt;}e[N<<2],e1[N<<2];
int n,m,Q,col,clo,t,cnt,cnt1,
fi[N],fi1[N],dfn[N],low[N],z[N],c[N],q[N],fa[N],d[N],f[N];
void add(int u,int v)
{
e[++cnt].to=v;e[cnt].nxt=fi[u];fi[u]=cnt;
}
void add1(int u,int v)
{
e1[++cnt1].to=v;e1[cnt1].nxt=fi1[u];fi1[u]=cnt1;
}
void tarjan(int x,int k)
{
dfn[x]=low[x]=++clo;z[++t]=x;
for (int i=fi1[x];i;i=e1[i].nxt)
if (!dfn[e1[i].to])
{
tarjan(e1[i].to,i^1);
low[x]=std::min(low[x],low[e1[i].to]);
}
else if (i!=k) low[x]=std::min(low[x],dfn[e1[i].to]);
if (dfn[x]==low[x])
{
for (++col;z[t]!=x;c[z[t--]]=col);
c[z[t--]]=col;
}
}
int find(int x){ return f[x]==x?x:f[x]=find(f[x]);}
int dfs(int x,int ff)
{
for (int i=fi[x];i;i=e[i].nxt)
if (e[i].to!=ff)
{
d[e[i].to]=d[x]+1;
fa[e[i].to]=x;
dfs(e[i].to,x);
}
}
int main()
{
for (int cas=1;~scanf("%d%d\n",&n,&m);++cas)
{
if (!n && !m) break;
printf("Case %d:\n",cas);
cnt1=1;cnt=clo=t=col=0;
memset(fi,0,sizeof(fi));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(f,0,sizeof(f));
memset(d,0,sizeof(d));
memset(c,0,sizeof(c));
memset(fi1,0,sizeof(fi1));
for (int i=1;i<=m;++i)
{
int u,v;scanf("%d%d\n",&u,&v);
add1(u,v);add1(v,u);
}
for (int i=1;i<=n;++i)
if (!dfn[i]) tarjan(i,0);
for (int i=1;i<=n;++i)
for (int j=fi1[i];j;j=e1[j].nxt)
if (c[i]!=c[e1[j].to]) add(c[i],c[e1[j].to]);
for (int i=1;i<=col;++i) f[i]=i;
--col;dfs(1,0);scanf("%d\n",&Q);
for (;Q--;)
{
int u,v;scanf("%d%d\n",&u,&v);
u=find(c[u]);v=find(c[v]);
if (d[u]std::swap(u,v);
int i=u,j=v;
for (;f[i]!=f[j];)
{
if (d[f[i]]std::swap(i,j);
i=find(fa[i]);--col;
}
printf("%d\n",col);
if (u==v) continue;
for (;f[u]!=f[v];)
{
if (d[f[u]]std::swap(u,v);
f[u]=i;u=find(fa[u]);
}
}
printf("\n");
}
return 0;
}
【题外话】做题的时候绕了好几个弯路,感觉最近总是很容易想偏题= =心累累的
最后数组开小了在POJ上RE了无数次(因为树的题做多了意识里感觉这题也是树就开了n*2的数组TAT)