Network

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1014    Accepted Submission(s): 206

Problem 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

 
Source
 

题意

给你一个无向图,有多次操作。每次操作加入一条边,然后询问有多少桥。

题解

就先Tarjan求出有多少桥,然后每次操作,就在搜索树上求个lca,在求的过程中遇到的桥都会失效,因为构成了环。详见代码

#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<vector>
#include<cstring>
#include<string>
#include<algorithm>
#include<stack>
#define MAX_N 100005
using namespace std; vector<int> G[MAX_N]; int father[MAX_N]; int dfn[MAX_N],low[MAX_N],ind=;
bool vis[MAX_N]; int n,m; int sum = ;
bool isBridge[MAX_N]; int p; void Tarjan(int u) {
father[u] = p;
dfn[u] = low[u] = ++ind;
vis[u] = ;
for (int i = ; i < G[u].size(); i++) {
int v = G[u][i];
if (v == p)continue;
if (!vis[v]) {
int tmp = p;
p = u;
Tarjan(v);
p = tmp;
low[u] = min(low[u], low[v]);
if (low[v] > dfn[u]) {
sum++;
isBridge[v] = ;
}
}
else
low[u] = min(dfn[v], low[u]);
}
} int main() {
int cas = ;
cin.sync_with_stdio(false);
while (cin >> n >> m) {
if (n == && m == )break;
memset(vis, , sizeof(vis)); for (int i = ; i <= n; i++)G[i].clear();
sum = ;
memset(isBridge, , sizeof(isBridge));
ind = ;
memset(father, , sizeof(father));
for (int i = ; i < m; i++) {
int u, v;
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
p = ;
Tarjan();
int q;
cin >> q;
cout << "Case " << ++cas << ":" << endl;
while (q--) {
int u, v;
cin >> u >> v;
if (dfn[u] < dfn[v])swap(u, v);
while (dfn[u] > dfn[v]) {
if (isBridge[u])sum--;
isBridge[u] = ;
u = father[u];
}
while (u != v) {
if (isBridge[v])sum--;
isBridge[v] = ;
v = father[v];
}
cout << sum << endl;
}
cout << endl;
}
return ;
}

注意要扩栈

HDU 2460 Network 傻逼Tarjan的更多相关文章

  1. HDU 2460 Network(双连通+树链剖分+线段树)

    HDU 2460 Network 题目链接 题意:给定一个无向图,问每次增加一条边,问个图中还剩多少桥 思路:先双连通缩点,然后形成一棵树,每次增加一条边,相当于询问这两点路径上有多少条边,这个用树链 ...

  2. HDU 2460 Network(桥+LCA)

    http://acm.hdu.edu.cn/showproblem.php?pid=2460 题意:给出图,求每次增加一条边后图中桥的数量. 思路: 先用tarjan算法找出图中所有的桥,如果lowv ...

  3. HDU 2460 Network 边双连通分量 缩点

    题意: 给出一个无向连通图,有\(m\)次操作,每次在\(u, v\)之间加一条边,并输出此时图中桥的个数. 分析: 先找出边双连通分量然后缩点得到一棵树,树上的每条边都输原图中的桥,因此此时桥的个数 ...

  4. BZOJ1718: [Usaco2006 Jan] Redundant Paths 分离的路径【边双模板】【傻逼题】

    LINK 经典傻逼套路 就是把所有边双缩点之后叶子节点的个数 //Author: dream_maker #include<bits/stdc++.h> using namespace s ...

  5. 傻逼Eclipse笔记

    Eclipse 这么傻逼的工具,还有人用,真是奇了怪了. Invalid project description 我想打开SVN 的代码 ,别让我拷到别的地方,怎么破? 正确答案是: 删除 Eclip ...

  6. BZOJ-1625 宝石手镯 01背包(傻逼题)

    傻逼题,懒得打,复制蛋蛋的.. 1625: [Usaco2007 Dec]宝石手镯 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 1076 Solved: ...

  7. Codeforces Gym 100338I TV Show 傻逼DFS,傻逼题

    Problem I. TV ShowTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest ...

  8. BZOJ 2222: [Cqoi2006]猜数游戏【神奇的做法,傻逼题,猜结论】

    2222: [Cqoi2006]猜数游戏 Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 604  Solved: 260[Submit][Status ...

  9. [BZOJ]4644: 经典傻逼题

    某天我觉得一切题目都是那么不可做,于是百度了一下"傻逼题"-- 题目大意:对于图中的任意一个点集(可以为空或者全集),所有恰好有一个端点在这个点集中的边组成的集合被称为割.一个割的 ...

随机推荐

  1. Git for Windows 工具的使用(一)

    如果你还不知道什么是Git,只知道GitHub,但是还不会用,我想这个教程会帮助你. 前言 鉴于网上目前的教材都太落后,GitHub for Windows已经更新了多个版本,好多界面都发生了变化,所 ...

  2. Linux学习-善用判断式

    利用 test 指令的测试功能 要检测系统上面某些文件或者是相关的属性时,利用 test 这个指令来工作真是好用得不 得了, 举例来说,我要检查 /dmtsai 是否存在时,使用: [dmtsai@s ...

  3. python偏函数使用

    偏函数依托于python functools模块.

  4. 使用docker+tomcat部署jenkins

  5. P3388 【模板】割点(割顶)

    P3388 [模板]割点(割顶) 题目背景 割点 题目描述 给出一个n个点,m条边的无向图,求图的割点. 输入输出格式 输入格式: 第一行输入n,m 下面m行每行输入x,y表示x到y有一条边 输出格式 ...

  6. ogre的初始化与启动以及显示对象设置

    ogre的使用方法1---自动设置 1.ogre初始化:首先实例化一个Root对象 Root * root = new Root(); Root * root = new Root("plu ...

  7. selenium - 弹出框操作

    # 6. 弹出框操作 # 6.1 页面弹出框操作# 页面弹出框 是一个html页面的元素,由用户在页面的操作触发弹出# (1)执行触发操作之后,等待弹出框出现之后,# (2)再定位弹出框中的元素并操作 ...

  8. day01_03.人人都会编程

    PHP if语句打招呼编程 <?php$gender = "man"; if($gender == "man"){ echo "you are ...

  9. Python学习-day10 进程

    学习完线程,学习进程 进程和线程的语法有很多一样的地方,不过在操作系统中的差别确实很大. 模块是threading 和 multiprocessing 多进程multiprocessing multi ...

  10. MySQL 8.* 账号密码管理

    修改密码: ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你的新密码' 通过以下命令可以查看修改的账号 ...