POJ 3694 Network(割边+LCA)
来源:互联网 发布:apache cgi区别 编辑:程序博客网 时间:2024/04/25 12:20
题意:一个无向图可以有重边,下面q个操作,每次在两个点间连接一条边,每次连接后整个无向图还剩下多少桥(注意是要考虑之前连了的边,每次回答是在上一次的基础之上)
思路:先跑一次tarjan,求出桥和缩点,那么远无向图将缩点为一棵树,树边正好是原来的桥。每次连接两点,看看
这两点是不是在同一个缩点内,如果是,那么缩点后的树没任何变化,如果两点属于不同的缩点,那么连接起来,然
后找这两个缩点的LCA,,因为从点u到LCA再到点v再到点u,将形成环,里面的树边都会变成不是桥。计数的时候
注意,有些树边可能之前已经被标记了,这次再经过不能再标记
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<stack>#include<algorithm>using namespace std;const int maxn = 1e5+5;vector<int> g[maxn];int n, m, q, bcnt, dfs_clock, scc_cnt, fa[maxn], low[maxn], dfn[maxn], belong[maxn];bool isbridge[maxn];stack<int> s;void LCA(int u, int v){ if(dfn[u] < dfn[v]) swap(u, v); while(dfn[u] > dfn[v]) { if(isbridge[u]) bcnt--; isbridge[u] = 0; u = fa[u]; } while(u != v) { if(isbridge[u]) bcnt--; if(isbridge[v]) bcnt--; isbridge[u] = isbridge[v] = 0; u = fa[u]; v = fa[v]; }}void tarjan(int u, int f){ fa[u] = f; dfn[u] = low[u] = ++dfs_clock; for(int i = 0; i < g[u].size(); i++) { int v = g[u][i]; if(!dfn[v]) { tarjan(v, u); low[u] = min(low[u], low[v]); } else if(fa[u] != v) low[u] = min(low[u], dfn[v]); }}void solve(){ tarjan(1, 0); for(int i = 1; i <= n; i++) { int v = fa[i]; if(v > 0 && low[i] > dfn[v]) isbridge[i] = 1, bcnt++; //桥标记标在v和i视情况而定 }}int main(void){ int ca = 1; while(cin >> n >> m && n+m) { while(!s.empty()) s.pop(); bcnt = dfs_clock = scc_cnt = 0; memset(dfn, 0, sizeof(dfn)); memset(belong, 0, sizeof(belong)); memset(isbridge, 0, sizeof(isbridge)); for(int i = 0; i < maxn; i++) g[i].clear(); for(int i = 1; i <= m; i++) { int u, v; scanf("%d%d", &u, &v); g[u].push_back(v); g[v].push_back(u); } solve(); printf("Case %d:\n", ca++); scanf("%d", &q); while(q--) { int u, v; scanf("%d%d", &u, &v); LCA(u, v); printf("%d\n", bcnt); } puts(""); } return 0;}
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
1 0
- POJ 3694 Network(割边+LCA)
- POJ 3694 Network(割边+LCA)
- POJ 3694 Network 割边+LCA
- POJ 3694 Network 割边+LCA
- 【POJ 3694】 Network(割边<桥>+LCA)
- POJ3694 Network 割边+LCA
- POJ3694 Network 割边 LCA
- POJ 3694 Network 割边
- poj 3694 Network(桥+边联通分量+LCA)
- POJ 3694 Network (边双连通缩点、LCA)
- poj 3694 Network 边双连通+LCA
- poj 3694 Network(双连通求桥+lca)
- poj 3694 Network(tarjan + LCA)
- poj 3694 Network(桥+lca)
- poj 3694 Network(Tarjan+LCA)
- POJ 3694Network (tarjan bcc + LCA)
- poj 3694 Network (强联通分量缩点+割桥+lca查询)
- Tarjan+LCA POJ 3694 Network
- php7新特性
- django笔记
- 鸟哥的linux私房菜学习笔记4
- 易聚之交错时空
- tab栏切换(原生js实现)
- POJ 3694 Network(割边+LCA)
- http-关于application/x-www-form-urlencoded等字符编码的解释说明
- Hudson之——持续集成服务器的安装与配置
- 对象成员
- Android Things:用户驱动-传感器
- 应用统计学与R语言实现学习笔记(一)——简介
- 经典例题(时钟)
- windows几个网络相关命令
- leetcode 72 Edit Distance(DP)