Tarjan

一、tarjan求强连通分量

例题一:P2863 [USACO06JAN]牛的舞会The Cow Prom

输出强联通分量的个数

#include<bits/stdc++.h>
#define rint register int
#define deb(x) cerr<<#x<<" = "<<(x)<<'\n';
using namespace std;
typedef long long ll;
using pii = pair <int,int>;
const int maxn = 1e4 + 5;
int n, m, tot, top, ans, numc;
int dfn[maxn], low[maxn], st[maxn];
int vis[maxn], col[maxn], cnt[maxn];
vector <int> g[maxn];

void tarjan(int u){
	dfn[u] = low[u] = ++tot;
	st[++top] = u;
	vis[u] = 1;
	for(auto v : g[u]){
		if(!dfn[v]){
			tarjan(v);
			low[u] = min(low[u], low[v]);
		} else if(vis[v]) low[u] = min(low[u], dfn[v]);
	}
	if(dfn[u] == low[u]){
		++numc;
		while(st[top+1] != u){
			col[st[top]] = numc;
			vis[st[top--]] = 0;
		}
	}
}

int main() {
	scanf("%d%d", &n, &m);
	for(int i=1, u, v; i<=m; i++) {
		scanf("%d%d", &u, &v);
		g[u].push_back(v);
	}
	for(int i=1; i<=n; i++)
		if(!dfn[i]) tarjan(i);
	for(int i=1; i<=n; i++)
		cnt[col[i]]++;
	for(int i=1; i<=numc; i++)
		if(cnt[i] > 1) ans++;
	printf("%d\n", ans);
}

二、tarjan缩点

例题二:P2341 受欢迎的牛

大意: n n n 头牛,崇拜关系具有传递性,问有多少头牛被所有牛崇拜
题解:缩点后计算出度为 0 0 0 的点的大小

#include<bits/stdc++.h>
#define rint register int
#define deb(x) cerr<<#x<<" = "<<(x)<<'\n';
using namespace std;
typedef long long ll;
const int maxn = 1e4 + 5;
int n, m, tot, top, ans, numc;
int dfn[maxn], low[maxn], st[maxn];
int vis[maxn], col[maxn], cnt[maxn], du[maxn];
vector <int> g[maxn];

void tarjan(int u){
	dfn[u] = low[u] = ++tot;
	st[++top] = u;
	vis[u] = 1;
	for(auto v : g[u]){
		if(!dfn[v]){
			tarjan(v);
			low[u] = min(low[u], low[v]);
		} else if(vis[v]) low[u] = min(low[u], dfn[v]);
	} 
	if(dfn[u] == low[u]){
		numc++;
		while(st[top+1] != u){
			col[st[top]] = numc;
			vis[st[top--]] = 0;
		}
	}
}

int main() {
	scanf("%d%d", &n, &m);
	for(int i=1, u, v; i<=m; i++) {
		scanf("%d%d", &u, &v);
		g[u].push_back(v);
	}
	for(int i=1; i<=n; i++)
		if(!dfn[i]) tarjan(i);
	for(int i=1; i<=n; i++){
		for(auto v : g[i])
			if(col[i] ^ col[v])
				du[col[i]]++;
		cnt[col[i]]++;
	}
	int num = 0;
	for(int i=1; i<=numc; i++)
		if(du[i] == 0){
			num++;
			ans = cnt[i];
		}
	if(num>1 || num==0) puts("0");
	else printf("%d\n", ans);
}

例题三:P2812 校园网络

大意: n n n 所学校的网络,求至少选多少学校为母鸡,以及至少增加几条线路,使所有学校使用上软件
题解:缩点后计算入度为 0 0 0 和出度为 0 0 0 的点,同时注意特判

#include<bits/stdc++.h>
#define rint register int
#define deb(x) cerr<<#x<<" = "<<(x)<<'\n';
using namespace std;
typedef long long ll;
const int maxn = 1e4 + 5;
int n, tot, top, ans, numc;
int dfn[maxn], low[maxn], st[maxn], in[maxn];
int vis[maxn], col[maxn], cnt[maxn], out[maxn];
int ans1, ans2;
vector <int> g[maxn];

void tarjan(int u){
	dfn[u] = low[u] = ++tot;
	st[++top] = u;
	vis[u] = 1;
	for(auto v : g[u]){
		if(!dfn[v]){
			tarjan(v);
			low[u] = min(low[u], low[v]);
		} else if(vis[v]) low[u] = min(low[u], dfn[v]);
	} 
	if(dfn[u] == low[u]){
		numc++;
		while(st[top+1] != u){
			col[st[top]] = numc;
			vis[st[top--]] = 0;
		}
	}
}

int main() {
	scanf("%d", &n);
	for(int i=1, v; i<=n; i++) {
		while(scanf("%d", &v) && v)
			g[i].push_back(v);
	}
	for(int i=1; i<=n; i++)
		if(!dfn[i]) tarjan(i);
	for(int i=1; i<=n; i++)
		for(auto v : g[i])
			if(col[i] ^ col[v])
				out[col[i]]++, in[col[v]]++;
	for(int i=1; i<=numc; i++){
		if(in[i] == 0) ans1++;
		if(out[i] == 0) ans2++;
	}
	ans2 = max(ans1, ans2);
	if(numc == 1) ans1 = 1, ans2 = 0;
	printf("%d\n%d", ans1, ans2);
}

例题四:P3387 【模板】缩点

大意:求一条路径,点权和最大
题解:缩点后DP,也可以DFS

#include<bits/stdc++.h>
#define rint register int
#define deb(x) cerr<<#x<<" = "<<(x)<<'\n';
using namespace std;
typedef long long ll;
const int maxn = 1e4 + 5;
int n, m, tot, top, ans, numc;
int dfn[maxn], low[maxn], st[maxn], a[maxn];
int vis[maxn], col[maxn], cnt[maxn], sum[maxn];
vector <int> g1[maxn], g2[maxn];

void tarjan(int u){
	dfn[u] = low[u] = ++tot;
	st[++top] = u;
	vis[u] = 1;
	for(auto v : g1[u]){
		if(!dfn[v]){
			tarjan(v);
			low[u] = min(low[u], low[v]);
		} else if(vis[v]) low[u] = min(low[u], dfn[v]);
	} 
	if(dfn[u] == low[u]){
		numc++;
		while(st[top+1] != u){
			col[st[top]] = numc;
			sum[numc] += a[st[top]];
			vis[st[top--]] = 0;
		}
	}
}

int dfs(int u, int fa){
	int ret = sum[u], mx = 0;
	for(auto v : g2[u]){
		if(v == fa) continue;
		mx = max(mx, dfs(v, u));
	}
	return ret + mx;
}

int main() {
	scanf("%d%d", &n, &m);
	for(int i=1; i<=n; i++) scanf("%d", a+i);
	for(int i=1, u, v; i<=m; i++) {
		scanf("%d%d", &u, &v);
		g1[u].push_back(v);
	}
	for(int i=1; i<=n; i++)
		if(!dfn[i]) tarjan(i);
	for(int i=1; i<=n; i++)
		for(auto v : g1[i])
			if(col[i] ^ col[v])
				g2[col[i]].push_back(col[v]);
	for(int i=1; i<=numc; i++)
		ans = max(ans, dfs(i, 0));
	printf("%d\n", ans);
}

三、tarjan求割点、桥

例题五:P3388 【模板】割点(割顶) —— 割点

例题六:Critical Links —— 桥

#include<bits/stdc++.h>
#define rint register int
#define deb(x) cerr<<#x<<" = "<<(x)<<'\n';
using namespace std;
typedef long long ll;
using pii = pair <int,int>;
const int maxn = 1e4 + 5;
int n, m, tot, ans;
int dfn[maxn], low[maxn], iscut[maxn];
vector <int> g[maxn];
vector <pii> bri;

void tarjan(int u, int fa){
	dfn[u] = low[u] = ++tot;
	int child = 0;
	for(auto v : g[u]){
		if(v == fa) continue;
		if(!dfn[v]){
			child++;
			tarjan(v, u);
			if(low[v] >= dfn[u]) iscut[u] = 1;
			if(low[v] > dfn[u]) bri.push_back({min(u, v), max(u, v)});
			low[u] = min(low[u], low[v]);
		} else low[u] = min(low[u], dfn[v]);
	}
	if(!fa && child==1) iscut[u] = 0;
}

int main() {
	scanf("%d%d", &n, &m);
	for(int i=1, u, v; i<=m; i++){
		scanf("%d%d", &u, &v);
		g[u].push_back(v);
		g[v].push_back(u);		
	}
	for(int i=1; i<=n; i++)
		if(!dfn[i]) tarjan(i, 0);
	for(int i=1; i<=n; i++)
		if(iscut[i]) ans++;
	printf("%d\n", ans);
	for(int i=1; i<=n; i++)
		if(iscut[i]) printf("%d ", i);
//	printf("%d\n", bri.size());
//	sort(bri.begin(), bri.end());
//	for(auto i : bri) 
//		printf("%d %d\n", i.first, i.second);
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值