poj3694 Network 【图论-Tarjan-Lca】
来源:互联网 发布:买岛国片在淘宝输什么 编辑:程序博客网 时间:2024/04/25 06:12
Network
Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 9848 Accepted: 3657
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
AC代码:
# include <cstdio># include <cstring># define MAXN 500005struct EDGE{ int v; int next;}edge[MAXN];int low[MAXN];int dfn[MAXN];int head[MAXN];int pre[MAXN];int depth[MAXN];int bridge[MAXN];int tot, index;int cut;int casecount;void Init(){ tot = 0; memset(head, -1, sizeof(head));}void Addedge(int u, int v){ edge[tot].v = v; edge[tot].next = head[u]; head[u] = tot ++;}void Tarjan(int u){ low[u] = dfn[u] = ++index; depth[u] = depth[pre[u]] + 1; for (int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].v; if (!dfn[v]) { pre[v] = u; Tarjan(v); if (low[v] < low[u]) { low[u] = low[v]; } if (low[v] > dfn[u]) { cut ++; bridge[v] = 1; } } else if (v != pre[u]) { if (dfn[v] < low[u]) { low[u] = dfn[v]; } } }}void Lca(int u, int v){ while (depth[u] > depth[v]) { if (bridge[u]) { cut --; bridge[u] = 0; } u = pre[u]; } while (depth[u] < depth[v]) { if (bridge[v]) { cut --; bridge[v] = 0; } v = pre[v]; } while (u != v) { if (bridge[u]) { cut --; bridge[u] = 0; } if (bridge[v]) { cut --; bridge[v] = 0; } u = pre[u]; v = pre[v]; }}void Solve(int n){ int q, a, b, i; memset(bridge, 0, sizeof(bridge)); memset(dfn, 0, sizeof(dfn)); memset(depth, 0, sizeof(depth)); for (i = 1; i <= n; i++) { pre[i] = i; } for (i = 1; i <= n; i++) { pre[i] = i; } cut = 0; index = 0; Tarjan(1); scanf("%d", &q); while (q--) { scanf("%d %d", &a, &b); Lca(a, b); printf("%d\n", cut); } printf("\n");}int main(void){ casecount = 1; int n, m; while (~scanf("%d %d", &n, &m), n && m) { printf("Case %d:\n", casecount ++); Init(); for (int i = 0; i < m; i++) { int a, b; scanf("%d %d", &a, &b); Addedge(a, b); Addedge(b, a); } Solve(n); } return 0;}
- poj3694 Network 【图论-Tarjan-Lca】
- POJ3694-Network(Tarjan缩点+LCA)
- poj3694 Network 无向图tarjan求桥+LCA
- poj3694 Network Tarjan+树链剖分
- poj3694 Network Tarjan(树链剖分)
- [POJ3694]Network(tarjan+链剖)
- POJ3694 Network 割边+LCA
- POJ3694 Network 割边 LCA
- poj3694 LCA+并查集+tarjan求割边
- 【POJ3694】Network {tarjan+并查集}
- poj3694--Network(双连通缩点+lca)
- Network-POJ3694并查集+LCA
- POJ3694 Network(边双连通+LCA)
- Network-POJ3694并查集+LCA
- poj3694+hdu2460 求桥+缩点+LCA/tarjan
- POJ3694(tarjan缩点+并查集+LCA)
- Tarjan+LCA POJ 3694 Network
- tarjan -LCA POJ-3417-Network
- c/c++有益的建议
- Linux中查看某个软件的安装路径
- 数据库端防止并发的SQL语句 插入时就判断是否存在
- 自定义连接池
- 集群环境下spring+session+redis,session共享问题
- poj3694 Network 【图论-Tarjan-Lca】
- 文章标题
- 为什么要使用jQuery?
- find命令之exec
- jquery中attr和prop的区别
- 统计输入中每个值连续出现了多少次
- explain分析查询
- Directshow & Opencv进行UVC开发
- SpringMVC-Interceptor拦截器