[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 ...
随机推荐
- object to primitive in javascript
例1: var a={}; alert(a); //[object Object]; 例2: var a={ toString:function(){ return 1; } } alert(a); ...
- [转载+原创]Emgu CV on C# (一) —— Emgu CV on Visual C# 2010
2014-08-16 最近要进行图像识别,准备利用几天的时间研究一下Emgu CV,花了一晚上功夫进行调试环境安装,期间遇到了不少问题,现梳理一下安装过程和调试过程中出现的问题. 中间有转载别人的部分 ...
- NPOI读取Excel数据应用
NPOI 是 POI 项目的 .NET 版本.使用 NPOI 你就可以在没有安装 Office 或者相应环境的机器上对 WORD/EXCEL 文档进行读写.NPOI是构建在POI 3.x版本之上的,它 ...
- shell 循环使用
问题描述: shell中for循环while循环的使用 问题解决: (1)for循环 (1.1)数 ...
- Matlab中数组下标是logical,如何处理?
K>> a = 10*ones(1,10); K>> b = [1 56 23 5 6 45 9 7 89 10]; K>> c = b<a c = 1 0 ...
- [转载]C#中int和IntPtr相互转换
方法一. int转IntPtr int i = 12; IntPtr p = new IntPtr(i); IntPtr转int int myi = (int)p; ...
- matlab字符串操作总结
matlab字符串操作总结 字符串操作总结 char(S1,S2,…)利用给定的字符串或单元数组创建字符数组double(S)将字符串转化成ASC码形式cellstr(S)利用的给定的字符数组创建字符 ...
- PHP读取xml方法讲解
一,什么是xml,xml有什么用途 XML(Extensible Markup Language)即可扩展标记语言,它与HTML一样,都是SGML(Standard Generalized Marku ...
- struct2访问或添加request/session/application
访问或添加request/session/application 1 通过ActionContext //这样放置 public String execute() { ActionConte ...
- linux下cat命令详解
简略版: cat主要有三大功能:1.一次显示整个文件.$ cat filename2.从键盘创建一个文件.$ cat > filename 只能创建新文件,不能编辑已有文件.3.将几个文 ...