Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 10435 | Accepted: 3888 |
Description
A network administrator manages a large network. The network consists of N computers and M links between pairs of computers. Any pair of computers are connected directly or indirectly by successive links, so data can be transformed between any two computers. The administrator finds that some links are vital to the network, because failure of any one of them can cause that data can't be transformed between some computers. He call such a link a bridge. He is planning to add some new links one by one to eliminate all bridges.
You are to help the administrator by reporting the number of bridges in the network after each new link is added.
Input
The input consists of multiple test cases. Each test case starts with a line containing two integers N(1 ≤ N ≤ 100,000) and M(N - 1 ≤ M ≤ 200,000).
Each of the following M lines contains two integers A and B ( 1≤ A ≠ B ≤ N), which indicates a link between computer A and B. Computers are numbered from 1 to N. It is guaranteed that any two computers
are connected in the initial network.
The next line contains a single integer Q ( 1 ≤ Q ≤ 1,000), which is the number of new links the administrator plans to add to the network one by one.
The i-th line of the following Q lines contains two integer A and B (1 ≤ A ≠ B ≤ N), which is the i-th added new link connecting computer A and B.
The last test case is followed by a line containing two zeros.
Output
For each test case, print a line containing the test case number( beginning with 1) and Q lines, the i-th of which contains a integer indicating the number of bridges in the network after the first i new links are added. Print a blank line after the output for each test case.
Sample Input
3 2 1 2 2 3 2 1 2 1 3 4 4 1 2 2 1 2 3 1 4 2 1 2 3 4 0 0
Sample Output
Case 1: 1 0 Case 2: 2 0
连通分量+LCA
题意:一个无向图可以有重边,下面q个操作,每次在两个点间连接一条有向边,每次连接后整个无向图还剩下多少桥(注意是要考虑之前连了的边,每次回答是在上一次的基础之上)
首先运行一次tarjan,求出桥和缩点,那么远无向图将缩点为一棵树,树边正好是原来的桥。每次连接两点,看看这两点是不是在同一个缩点内,如果是,那么缩点后的树没任何变化,如果两点属于不同的缩点,那么连接起来,然后找这两个缩点的LCA,,因为从点u到LCA再到点v再到点u,将形成环,里面的树边都会变成不是桥。计数的时候注意,有些树边可能之前已经被标记了,这次再经过不能再标记
首先按思路写了个代码,跑了2s多,因为显式建树了。不过如果理解好tarjan的话,其实发现不需要显式建树,可以利用tarjan留下的dfn和low的信息找LCA
另外找LCA这里用朴素的方法,因为这次找LCA是要找到路径的,而且途中有些边是被标记了的。朴素的方法就是在树中记录节点的父亲,然后沿着父亲走回根去
修改后跑了1s多一点。牛人是用并查集来缩点,能跑出200ms
方法2:先将网络中的桥求出来,在求的过程中进行并查集缩点,在询问的时候,进行最朴素的LCA查找最近公共祖先,在求的过程中判断节点与父节点是不是在同一个集合中,如果不在同一个集合,说明是桥,则这个桥将不存在,将两个集合合并。暴力代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <stack>
#include <map>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll maxn = 1e5 + 5;
int n, m, low[maxn], dfn[maxn], id[maxn], scc_cnt, dfs_cnt, cnt, bridge[maxn], dep[maxn], fa[maxn];
vector<int> v[maxn];
map<ll, int> mp;
struct node
{
int x, y;
bool operator < (const node &a) const
{
if(a.x == x)
return y < a.y;
return x < a.x;
}
}ans[maxn];
void init()
{
memset(low, 0, sizeof(low));
memset(id, 0, sizeof(id));
memset(dfn, 0, sizeof(dfn));
memset(dep, 0, sizeof(dep));
memset(bridge, 0, sizeof(bridge));
scc_cnt = dfs_cnt = cnt = 0;
for(int i = 0; i < maxn; i++)
v[i].clear();
mp.clear();
}
//void addAns(int x,int y) //所有割边
//{
// if(x > y) swap(x,y);
// ans[cnt].x = x, ans[cnt].y = y;
// cnt++;
//}
void tarjan(int x, int f)
{
dfn[x] = low[x] = ++dfs_cnt;
dep[x] = dep[fa[x]] + 1;
for(int i = 0; i < v[x].size(); i++)
{
int to = v[x][i];
if(to == f) continue;
if(!dfn[to])
{
fa[to] = x;
tarjan(to, x);
low[x] = min(low[x], low[to]);
if(low[to] > dfn[x])
{
cnt++;
bridge[to] = 1;
}
}
else if(dfn[to] < dfn[x])
low[x] = min(low[x], dfn[to]);
}
}
void lca(int u, int v)
{
if(dep[u] < dep[v]) swap(u, v);
while(dep[u] > dep[v])
{
if(bridge[u]) cnt--, bridge[u] = 0;
u = fa[u];
}
while(u != v)
{
if(bridge[u]) cnt--, bridge[u] = 0;
if(bridge[v]) cnt--, bridge[v] = 0;
u = fa[u], v = fa[v];
}
}
bool Hash(int u, int v) //哈希去重边
{
if(mp[u*maxn+v]) return true;
if(mp[v*maxn+u]) return true;
mp[u*maxn+v] = mp[v*maxn+u] = true;
return false;
}
int main()
{
int q, ca = 1;
while(~scanf("%d%d",&n, &m), n)
{
init();
int x, y, k;
for(int i = 1; i <= m; i++)
{
scanf("%d%d", &x, &y);
// if(Hash(x, y)) continue;
v[x].push_back(y);
v[y].push_back(x);
}
tarjan(1, 0);
// cout << cnt << endl;
scanf("%d", &q);
printf("Case %d:\n", ca++);
while(q--)
{
scanf("%d%d", &x, &y);
lca(x, y);
printf("%d\n", cnt);
}
}
return 0;
}
并查集缩点
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <stack>
#include <map>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll maxn = 1e5 + 5;
int n, m, low[maxn], dfn[maxn], id[maxn], scc_cnt, dfs_cnt, cnt, bridge[maxn], dep[maxn], fa[maxn], pre[maxn];
vector<int> v[maxn];
map<ll, int> mp;
struct node
{
int x, y;
bool operator < (const node &a) const
{
if(a.x == x)
return y < a.y;
return x < a.x;
}
}ans[maxn];
void init()
{
memset(low, 0, sizeof(low));
memset(id, 0, sizeof(id));
memset(dfn, 0, sizeof(dfn));
memset(dep, 0, sizeof(dep));
memset(bridge, 0, sizeof(bridge));
scc_cnt = dfs_cnt = cnt = 0;
for(int i = 0; i < maxn; i++) pre[i] = i;
for(int i = 0; i < maxn; i++)
v[i].clear();
mp.clear();
}
//void addAns(int x,int y) //所有割边
//{
// if(x > y) swap(x,y);
// ans[cnt].x = x, ans[cnt].y = y;
// cnt++;
//}
int Find(int x)
{
return pre[x] == x ? x : pre[x] = Find(pre[x]);
}
void join(int x, int y)
{
pre[Find(y)] = Find(x);
}
void Judge(int x)
{
if(Find(x) != Find(fa[x])) //看是不是桥, 是的话 合并,并且取消这个桥
{
cnt--;
join(x, fa[x]);
}
}
void tarjan(int x, int f)
{
dfn[x] = low[x] = ++dfs_cnt;
dep[x] = dep[fa[x]] + 1;
for(int i = 0; i < v[x].size(); i++)
{
int to = v[x][i];
if(to == f) continue;
if(!dfn[to])
{
fa[to] = x;
tarjan(to, x);
low[x] = min(low[x], low[to]);
if(low[to] > dfn[x])
{
cnt++;
}
else
join(x, to);
}
else if(dfn[to] < dfn[x])
low[x] = min(low[x], dfn[to]);
}
}
void lca(int u, int v)
{
if(dep[u] < dep[v]) swap(u, v);
while(dep[u] > dep[v])
{
Judge(u);
u = fa[u];
}
while(u != v)
{
Judge(u), Judge(v);
u = fa[u], v = fa[v];
}
}
bool Hash(int u, int v) //哈希去重边
{
if(mp[u*maxn+v]) return true;
if(mp[v*maxn+u]) return true;
mp[u*maxn+v] = mp[v*maxn+u] = true;
return false;
}
int main()
{
int q, ca = 1;
while(~scanf("%d%d",&n, &m), n)
{
init();
int x, y, k;
for(int i = 1; i <= m; i++)
{
scanf("%d%d", &x, &y);
if(Hash(x, y)) continue;
v[x].push_back(y);
v[y].push_back(x);
}
tarjan(1, 0);
// cout << cnt << endl;
scanf("%d", &q);
printf("Case %d:\n", ca++);
while(q--)
{
scanf("%d%d", &x, &y);
if(Find(x) != Find(y))
lca(x, y);
printf("%d\n", cnt);
}
}
return 0;
}