POJ 3694——Network——————【连通图,LCA求桥】
Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
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 andB.
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
题目大意:n个点,m条边。q次询问,问你新加入无向边ui,vi后,图中的桥还有多少。
解题思路:求桥,标记桥,LCA过程中消去桥。
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
using namespace std;
const int maxn = 100100;
struct Edge{
int from,to,dist,next;
Edge(){}
Edge(int _to,int _next):to(_to),next(_next){}
}edges[maxn*4];
int dfn[maxn];
int fa[maxn],bridge[maxn],dep[maxn];
int head[maxn];
int tot, brinum;
int dfs_clock;
void init(){
tot = 0;
dfs_clock = 0;
brinum = 0;
memset(head,-1,sizeof(head));
memset(bridge,0,sizeof(bridge));
memset(dfn,0,sizeof(dfn));
}
void AddEdge(int _u,int _v){
edges[tot].to = _v;
edges[tot].next = head[_u];
head[_u] = tot++;
}
int dfs(int u,int f){
int lowu = dfn[u] = ++dfs_clock;
int child = 0;
for(int i = head[u]; i != -1; i = edges[i].next){
int v = edges[i].to;
if(!dfn[v]){
child++;
fa[v] = u;
int lowv = dfs(v,i);
lowu = min(lowv,lowu);
if(lowv > dfn[u]){ //标记桥
bridge[v] = 1;
brinum++;
}
}else if(dfn[v] < dfn[u] && (f^1) != i){
lowu = min(lowu,dfn[v]);
}
}
// low[u] = lowu;
return lowu;
}
void LCA(int u,int v){ //简化的LCA,把求LCA过程中的桥减掉
while(dfn[u] < dfn[v]){
if(bridge[v] == 1){
brinum--;
bridge[v] = 0;
}
v = fa[v];
}
while(dfn[u] > dfn[v]){
if(bridge[u] == 1){
brinum--;
bridge[u] = 0;
}
u = fa[u];
}
while( u != v ){
if(bridge[u]) {
brinum--;
bridge[u] = 0;
}
if(bridge[v]){
brinum--;
bridge[v] = 0;
}
u = fa[u];
v = fa[v];
}
}
int main(){
int n, m, q, cas = 0;
while(scanf("%d%d",&n,&m)!=EOF&&(n+m)){
int a,b;
init();
for(int i = 0; i < m; i++){
scanf("%d%d",&a,&b);
AddEdge(a,b);
AddEdge(b,a);
}
for(int i = 1; i <= n; i++){
if(!dfn[i])
dfs(i,-1);
}
printf("Case %d:\n",++cas);
scanf("%d",&q);
for(int i = 0; i < q; i++){
scanf("%d%d",&a,&b);
LCA(a,b);
printf("%d\n",brinum);
}puts("");
}
return 0;
}
最新文章
- Dreamweaver 扩展开发:C-level extensibility and the JavaScript interpreter
- Python基础(一)
- ListView遍历每个Item出现NullPointerException的异常
- java日期比较,日期计算
- [设计模式] 8 组合模式 Composite
- OpenJudge/Poj 1915 Knight Moves
- C++内存管理(超长,例子很详细,排版很好)
- nopCommerce_3.00-Nop.Core.Caching
- 转 - CSS深入理解vertical-align和line-height的基友关系
- shell脚本编写步骤及其常用命令和符号
- 一些比较隐秘的OJ的网址
- 【Learning】带花树——一般图最大匹配
- Redis 持久化RDB 和AOF
- 记录自己的 django管理 开发环境 和 生产环境 配置过程
- java 彻底理解 byte char short int float long double
- [UE4]行为树,组合节点:Selector和Sequence
- PID控制算法的C语音实现
- [洛谷1681]最大正方形II
- Yii2 查询条件
- 随机森林(Random Forest,简称RF)