[POJ3694]Network(LCA, 割边, 桥)
题目链接:http://poj.org/problem?id=3694
题意:给一张图,每次加一条边,问割边数量。
tarjan先找出所有割边,并且记录每个点的父亲和来自于哪一条边,然后询问的时候从两个点向上找lca,沿途更新割边数量和割边状态即可。
AC代码
/*
━━━━━┒ギリギリ♂ eye!
┓┏┓┏┓┃キリキリ♂ mind!
┛┗┛┗┛┃\○/
┓┏┓┏┓┃ /
┛┗┛┗┛┃ノ)
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┃┃┃┃┃┃
┻┻┻┻┻┻
*/
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
using namespace std;
#define fr first
#define sc second
#define cl clear
#define BUG puts("here!!!")
#define W(a) while(a--)
#define pb(a) push_back(a)
#define Rint(a) scanf("%d", &a)
#define Rll(a) scanf("%lld", &a)
#define Rs(a) scanf("%s", a)
#define Cin(a) cin >> a
#define FRead() freopen("in", "r", stdin)
#define FWrite() freopen("out", "w", stdout)
#define Rep(i, len) for(int i = 0; i < (len); i++)
#define For(i, a, len) for(int i = (a); i < (len); i++)
#define Cls(a) memset((a), 0, sizeof(a))
#define Clr(a, x) memset((a), (x), sizeof(a))
#define Full(a) memset((a), 0x7f7f, sizeof(a))
#define lp p << 1
#define rp p << 1 | 1
#define pi 3.14159265359
#define RT return
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
typedef map<string, int> msi;
typedef vector<int> vi;
typedef vector<LL> vl;
typedef vector<vl> vvl;
typedef vector<bool> vb; inline bool scan_d(int &num) {
char in;bool IsN=false;
in=getchar();
if(in==EOF) return false;
while(in!='-'&&(in<''||in>'')) in=getchar();
if(in=='-'){ IsN=true;num=;}
else num=in-'';
while(in=getchar(),in>=''&&in<=''){
num*=,num+=in-'';
}
if(IsN) num=-num;
return true;
} const int maxn = ;
const int maxm = ; typedef struct Edge {
int idx, v;
Edge() {}
Edge(int vv, int ii) : v(vv), idx(ii) {}
}Edge; int n, m, q, cnt, b;
int depth[maxn], fa[maxn], vis[maxn];
int dfn[maxn], low[maxn], pbr[maxm];
vector<Edge> G[maxn];
bool bri[maxm];
void dfs(int u, int p, int d) {
fa[u] = p; depth[u] = d;
Rep(i, G[u].size()) {
int v = G[u][i].v;
if(!vis[v]) {
vis[v] = ;
dfs(v, u, d+);
}
}
} void tarjan(int u, int p, int d, int pe) {
low[u] = dfn[u] = d;
pbr[u] = pe;
Rep(i, G[u].size()) {
int idx = G[u][i].idx;
int v = G[u][i].v;
if(!dfn[v]) {
tarjan(v, u, d+, idx);
low[u] = min(low[u], low[v]);
if(low[v] > dfn[u]) bri[idx] = ;
}
else if(v != p) low[u] = min(low[u], dfn[v]);
}
} void lca(int u, int v) {
while(depth[u] > depth[v]) {
if(bri[pbr[u]]) {
bri[pbr[u]] = ; b--;
}
u = fa[u];
}
while(depth[v] > depth[u]) {
if(bri[pbr[v]]) {
bri[pbr[v]] = ; b--;
}
v = fa[v];
}
while(u != v) {
if(bri[pbr[u]]) {
bri[pbr[u]] = ; b--;
}
u = fa[u];
if(bri[pbr[v]]) {
bri[pbr[v]] = ; b--;
}
v = fa[v];
}
} int main() {
// FRead();
int u, v, _ = ;
while(~scan_d(n) && ~scan_d(m) && n + m) {
Cls(depth); Cls(vis); Cls(fa); Cls(pbr);
Cls(dfn); Cls(low); Cls(bri); b = ;
Rep(i, n+) G[i].cl();
Rep(i, m) {
scan_d(u); scan_d(v);
G[u].pb(Edge(v, cnt++)); G[v].pb(Edge(u, cnt++));
}
dfs(, , ); tarjan(, , , );
scan_d(q);
printf("Case %d:\n", _++);
For(i, , cnt+) if(bri[i]) b++;
W(q) {
scan_d(u); scan_d(v);
lca(u, v);
printf("%d\n", b);
}
}
RT ;
}
第一次TLE了,因为窝把erase的复杂度想象成了O(lgn)…
/*
━━━━━┒ギリギリ♂ eye!
┓┏┓┏┓┃キリキリ♂ mind!
┛┗┛┗┛┃\○/
┓┏┓┏┓┃ /
┛┗┛┗┛┃ノ)
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┃┃┃┃┃┃
┻┻┻┻┻┻
*/
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
using namespace std;
#define fr first
#define sc second
#define cl clear
#define BUG puts("here!!!")
#define W(a) while(a--)
#define pb(a) push_back(a)
#define Rint(a) scanf("%d", &a)
#define Rll(a) scanf("%lld", &a)
#define Rs(a) scanf("%s", a)
#define Cin(a) cin >> a
#define FRead() freopen("in", "r", stdin)
#define FWrite() freopen("out", "w", stdout)
#define Rep(i, len) for(int i = 0; i < (len); i++)
#define For(i, a, len) for(int i = (a); i < (len); i++)
#define Cls(a) memset((a), 0, sizeof(a))
#define Clr(a, x) memset((a), (x), sizeof(a))
#define Full(a) memset((a), 0x7f7f, sizeof(a))
#define lp p << 1
#define rp p << 1 | 1
#define pi 3.14159265359
#define RT return
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
typedef map<string, int> msi;
typedef vector<int> vi;
typedef vector<LL> vl;
typedef vector<vl> vvl;
typedef vector<bool> vb; inline bool scan_d(int &num) {
char in;bool IsN=false;
in=getchar();
if(in==EOF) return false;
while(in!='-'&&(in<''||in>'')) in=getchar();
if(in=='-'){ IsN=true;num=;}
else num=in-'';
while(in=getchar(),in>=''&&in<=''){
num*=,num+=in-'';
}
if(IsN) num=-num;
return true;
} const int maxn = ;
const int maxm = ;
typedef struct Bridge {
int u, v;
Bridge() {}
Bridge(int uu, int vv) : u(uu), v(vv) { if(u > v) swap(u, v); }
bool operator<(Bridge y) {
if(u == y.u) return v < y.v;
return u < y.u;
}
}Bridge; int n, m, q;
int ufs[maxn];
int depth[maxn], fa[maxn], vis[maxn];
int dfn[maxn], low[maxn];
vi G[maxn];
vector<Bridge> b;
vector<Bridge>::iterator it; int find(int x) {
return x == ufs[x] ? x : ufs[x] = find(ufs[x]);
} void unite(int x, int y) {
x = find(x);
y = find(y);
if(x != y) ufs[y] = x;
} void dfs(int u, int p, int d) {
fa[u] = p; depth[u] = d;
Rep(i, G[u].size()) {
int v = G[u][i];
if(!vis[v]) {
vis[v] = ;
dfs(v, u, d+);
}
}
} void tarjan(int u, int p, int d) {
low[u] = dfn[u] = d;
Rep(i, G[u].size()) {
int v = G[u][i];
if(!dfn[v]) {
tarjan(v, u, d+);
low[u] = min(low[u], low[v]);
if(low[v] > dfn[u]) b.pb(Bridge(u, v));
}
else if(v != p) low[u] = min(low[u], dfn[v]);
}
} bool cmp(Bridge x, Bridge y) {
if(x.u == y.u) return x.v < y.v;
return x.u < y.u;
} int bs(Bridge x) {
int lo = , hi = b.size();
while(lo <= hi) {
int mi = (lo + hi) >> ;
if(b[mi].u == x.u && b[mi].v == x.v) return mi;
if(cmp(x, b[mi]) > ) hi = mi - ;
else lo = mi + ;
}
if(b[lo].u == x.u && b[lo].v == x.v) return lo;
if(b[hi].u == x.u && b[hi].v == x.v) return hi;
return -;
} void lca(int u, int v) {
while(depth[u] > depth[v]) {
Bridge tmp = Bridge(u, fa[u]);
it = lower_bound(b.begin(), b.end(), tmp);
if(it != b.end() && it->u == tmp.u && it->v == tmp.v) b.erase(it);
u = fa[u];
}
while(depth[v] > depth[u]) {
Bridge tmp = Bridge(v, fa[v]);
it = lower_bound(b.begin(), b.end(), tmp);
if(it != b.end() && it->u == tmp.u && it->v == tmp.v) b.erase(it);
v = fa[v];
}
while(u != v) {
Bridge tmp = Bridge(u, fa[u]);
it = lower_bound(b.begin(), b.end(), tmp);
if(it != b.end() && it->u == tmp.u && it->v == tmp.v) b.erase(it);
tmp = Bridge(v, fa[v]);
it = lower_bound(b.begin(), b.end(), tmp);
if(it != b.end() && it->u == tmp.u && it->v == tmp.v) b.erase(it);
u = fa[u];
v = fa[v];
}
} int main() {
// FRead();
int u, v, _ = ;
while(~scan_d(n) && ~scan_d(m) && n + m) {
Cls(depth); Cls(vis); Cls(fa);
Cls(dfn); Cls(low); b.cl();
Rep(i, n+) G[i].cl(), ufs[i] = i;
Rep(i, m) {
scan_d(u); scan_d(v);
G[u].pb(v); G[v].pb(u);
}
dfs(, , ); tarjan(, , );
sort(b.begin(), b.end(), cmp);
scan_d(q);
printf("Case %d:\n", _++);
W(q) {
scan_d(u); scan_d(v);
Bridge tmp = Bridge(u, v);
it = lower_bound(b.begin(), b.end(), tmp);
if(it != b.end() && it->u == tmp.u && it->v == tmp.v) {
b.erase(it);
printf("%d\n", b.size());
continue;
}
else {
lca(u, v);
printf("%d\n", b.size());
}
}
}
RT ;
}
[POJ3694]Network(LCA, 割边, 桥)的更多相关文章
- D - Network - poj3694(LCA求桥)
题意:有一个网络有一些边相互连接,现在有Q次操作,求每次操作后的桥的个数 分析:开始竟然不知道还有LCA这么个东西....... *********************************** ...
- 【POJ 3694】 Network(割边<桥>+LCA)
[POJ 3694] Network(割边+LCA) Network Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7971 ...
- [POJ3694]Network(Tarjan,LCA)
[POJ3694]Network Description A network administrator manages a large network. The network consists o ...
- HDU 4738——Caocao's Bridges——————【求割边/桥的最小权值】
Caocao's Bridges Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- POJ3694 Network(Tarjan双联通分图 LCA 桥)
链接:http://poj.org/problem?id=3694 题意:给定一个有向连通图,每次增加一条边,求剩下的桥的数量. 思路: 给定一个无向连通图,添加一条u->v的边,求此边对图剩余 ...
- POJ3694 Network —— 边双联通分量 + 缩点 + LCA + 并查集
题目链接:https://vjudge.net/problem/POJ-3694 A network administrator manages a large network. The networ ...
- Network POJ - 3694 (LCA+tarjan+桥)
题目链接:https://vjudge.net/problem/POJ-3694 具体思路:首先可以通过缩点的方式将整个图变成一个树,并且树的每条边是桥,但是我们可以利用dfn数组将整个图变成树,这样 ...
- POJ 3694——Network——————【连通图,LCA求桥】
Network Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Stat ...
- POJ 3694 Network (求桥,边双连通分支缩点,lca)
Network Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5619 Accepted: 1939 Descripti ...
随机推荐
- substr,mb_substr,iconv_substr,array_slice
通过一个例子来看其关系 /** +---------------------------------------------------------- * 字符串截取,支持中文和其他编码 +----- ...
- java 泛型通配符 extends, super
引自:http://sharewind.iteye.com/blog/1622164 关键字说明 ? 通配符类型 <? extends T> 表示类型的上界,表示参数化类型的可能是T 或是 ...
- Wireshark 入门
1.过滤目的地是百度的IP包. 百度的ip: 命令:ip.src eq 61.135.169.125 过滤ip来源是61.135.169.125 ip.dst eq 61.135.169.125 过滤 ...
- 自定义的你的ubuntu鼠标右键
首先看下效果图: 好,接下来讲下如何实现,“下一个桌面”和”在终端打开“,首先是安装必要软件 sudo apt-get -y install nautilus-open-terminal nautil ...
- [转载]C# FTP操作工具类
本文转载自<C# Ftp操作工具类>,仅对原文格式进行了整理. 介绍了几种FTP操作的函数,供后期编程时查阅. 参考一: using System; using System.Collec ...
- python SendMail 发送邮件
最近在学习python 时,用到了发送邮件的操作,通过整理总结如下: 1.普通文本邮件 普通文本邮件发送的实现,关键是要将MIMEText中_subtype设置为plain,首先导入smtplib和m ...
- Sql例子Sp_ExecuteSql 带参数
Declare @i int, @projectCount int ) --参数 ) ) ) ) ) ) --循环变量起始 --得到所有的项目 select @projectCount = count ...
- SQLServer 语句-创建索引【转】
语法:CREATE [索引类型] INDEX 索引名称ON 表名(列名)WITH FILLFACTOR = 填充因子值0~100GO /*实例*/USE 库名GOIF EXISTS (SELECT * ...
- apache nginx php不显示版本号
apache 不显示版本号 http.conf 中的 修改为 ServerTokens ProdServerSignature Off 有的版本没有,在最后添加即可 php php.ini 中的 修改 ...
- Matlab中find函数的使用
一.问题来源 看到了 min_score_pos = find(A0_scores==min(A0_scores), 1); [r,c] = find(X,k),返回X中第k个非零元素的行列位置. 二 ...