分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

poj~3694Network

发布时间:2023-09-06 01:56责任编辑:沈小雨关键词:暂无标签

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 21 22 321 21 34 41 22 12 31 421 23 40 0

Sample Output

Case 1:10Case 2:20


图论关于桥的模板

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 using namespace std; 6 ?7 const int maxn = 2e5 + 10; 8 int head[maxn], dfn[maxn], vis[maxn], fa[maxn], low[maxn]; 9 int dcnt, bcnt, tot;10 struct node {11 ????int v, next, used;12 } edge[4 * maxn] ;13 int isbridge[maxn];14 void add(int u, int v) {15 ????edge[tot].v = v;16 ????edge[tot].next = head[u];17 ????edge[tot].used = 0;18 ????head[u] = tot++;19 }20 void lca(int u, int v) {21 ????if (dfn[u] < dfn[v]) swap(u, v);22 ????while(dfn[u] > dfn[v]) {23 ????????if(isbridge[u]) bcnt--;24 ????????isbridge[u] = 0;25 ????????u = fa[u];26 ????}27 ????while(u != v) {28 ????????if (isbridge[u]) bcnt--;29 ????????if (isbridge[v]) bcnt--;30 ????????isbridge[u] = isbridge[v] = 0;31 ????????u = fa[u], v = fa[v];32 ????}33 }34 void dfs(int u) {35 ????vis[u] = 1;36 ????dfn[u] = low[u] = ++dcnt;37 ????for (int i = head[u]; i != -1 ; i = edge[i].next )38 ????????if (!edge[i].used) {39 ????????????edge[i].used = edge[i ^ 1].used = 1;40 ????????????int v = edge[i].v;41 ????????????if (!vis[v]) {42 ????????????????fa[v] = u;43 ????????????????dfs(v);44 ????????????????low[u] = min(low[u], low[v]);45 ????????????????if (dfn[u] < low[v]) {46 ????????????????????bcnt++;47 ????????????????????isbridge[v] = 1;48 ????????????????}49 ????????????} else if (vis[v] == 1) low[u] = min(low[u], dfn[v]);50 ????????}51 ????vis[u] = 2;52 }53 void init() {54 ????tot = dcnt = bcnt = 0;55 ????memset(head, -1, sizeof(head));56 ????memset(isbridge, 0, sizeof(0));57 ????memset(vis, 0, sizeof(head));58 }59 int main() {60 ????int cas = 1, n, m, q;61 ????while(scanf("%d%d", &n, &m) != EOF) {62 ????????if (n == 0 && m == 0 ) break;63 ????????init();64 ????????for (int i = 0 ; i < m ; i++) {65 ????????????int u, v;66 ????????????scanf("%d%d", &u, &v);67 ????????????add(u, v);68 ????????????add(v, u);69 ????????}70 ????????fa[1] = 1;71 ????????dfs(1);72 ????????printf("Case %d:\n", cas++);73 ????????scanf("%d", &q);74 ????????while(q--) {75 ????????????int u, v;76 ????????????scanf("%d%d", &u, &v);77 ????????????lca(u, v);78 ????????????printf("%d\n", bcnt);79 ????????}80 ????????printf("\n");81 ????}82 ????return 0;83 }

poj~3694Network

原文地址:https://www.cnblogs.com/qldabiaoge/p/9091118.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved