一、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縮點
例題二: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;
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);
}
例題三: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;
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++;
}
if(numc == 1) ans1 = 1, ans2 = 0;
ans2 = max(ans1, ans2);
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);
}