tarjan1
基础
||scc缩点|并查集缩点|edcc缩点| |:-:|:-:|:-:|:-:| |适用的图|有向图|无向图|无向图| |缩点之后得到的图的类型|DAG|无向森林|连通图之中直接变成一棵树| |一般求解的问题|拓扑+DP,2-SAT,环路分析|欧拉回路,最小生成树|树形DP|
强连通分量当做一个合并之后的点缩完之后,变成 DAG。
例题
B3609 [图论与代数结构 701] 强连通分量
强连通的板子,把每个点都标记颜色即可。
//Code by hhy
#include<bits/stdc++.h>
#define F(i , a , b , c) for( int i = (a) ; ((c > 0) ? i <= (b) : i >= (b)) ; i += c )
#define T(i , root , b , c) for( int i = root ; b ; c )
#define int long long
#define W(f) while(f)
#define ull unsigned long long
#define pb push_back
#define fi first
#define se second
#define ll long long
#define debug(...){\
cout<<"debug in function "<<__FUNCTION__<<",line "<<__LINE__<<"\n";\
string s=#__VA_ARGS__,s2="";\
vector<string>v;\
for(auto i:s){\
if(i==','){\
v.push_back(s2);\
s2="";\
}else{\
s2+=i;\
}\
}\
v.push_back(s2);\
vector<int>v2={__VA_ARGS__};\
for(int i=0;i<v.size()-1;i++){\
cout<<v[i]<<"="<<v2[i]<<"\n";\
}\
cout<<v[v.size()-1]<<"="<<v2[v2.size()-1]<<"\n\n";\
}
using namespace std ;
const int kMaxN = 2e5 + 5 , MOD = 998244353 , INF = 1e18 ;
struct Edgepr
{
int u , w ;
};
struct Edgeve
{
int v , w ;
};
struct Edge
{
int v , nxt ;
}edge[100005];
struct node
{
int x , y , id ;
}Node[kMaxN] ;
int n , m , scc , cnt , tim ;
int dfn[100005] , low[100005] , head[100005] , color[100005] ;
bool vis[100005] , res[100005] ;
stack<int> st ;
vector<int> op[100005] ;
inline ll ksm(ll a , ll b)
{
ll mul = 1 ;
while(b)
{
if(b & 1) mul *= a , mul %= MOD ;
a *= a ;
a %= MOD ;
b >>= 1 ;
}
return mul ;
}
inline int read()
{
int x = 0 , f = 1 ;
char ch = getchar() ;
while(ch < '0' || ch > '9')
{
if(ch == '-') f = -1 ;
ch = getchar() ;
}
while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0' , ch = getchar() ;
return x * f ;
}
void write(int x)
{
if(x < 0) putchar('-') , x = -x ;
if(x > 9) write(x / 10) ;
putchar(x % 10 + '0') ;
}
void add_edge(int u , int v)
{
edge[++cnt] = {v , head[u]} ;
head[u] = cnt ;
}
void tanjan(int u)
{
st.push(u) ;
vis[u] = 1 ;
dfn[u] = low[u] = ++tim ;
for( int i = head[u] ; i ; i = edge[i].nxt )
{
int v = edge[i].v ;
if(!dfn[v])
{
tanjan(v) ;
low[u] = min(low[u] , low[v]) ;
}
else if(vis[v] == 1)
{
low[u] = min(dfn[v] , low[u]) ;
}
}
if(dfn[u] == low[u])
{
scc++ ;
while(!st.empty())
{
color[st.top()] = scc ;
vis[st.top()] = 0 ;
op[scc].push_back(st.top()) ;
if(u == st.top())
{
st.pop() ;
break ;
}
st.pop() ;
}
}
}
void Read()
{
}
void work()
{
Read() ;
cin >> n >> m ;
for( int i = 1 ; i <= m ; i++ )
{
int u , v ;
cin >> u >> v ;
add_edge(u , v) ;
}
for( int i = 1 ; i <= n ; i++ )
{
if(dfn[i] == 0)
{
tanjan(i) ;
}
}
cout << scc << "\n" ;
for( int i = 1 ; i <= n ; i++ )
{
if(!res[color[i]])
{
res[color[i]] = 1 ;
sort(op[color[i]].begin() , op[color[i]].end()) ;
for( int j = 0 ; j < op[color[i]].size() ; j++ )
{
cout << op[color[i]][j] << " " ;
}
cout << "\n" ;
}
}
}
signed main()
{
// freopen(".in" , "r" , stdin) ;
// freopen(".out" , "w" , stdout) ;
ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
int t = 1 ;
// cin >> t ;
while(t--) work() ;
return 0 ;
}
P2661 [NOIP 2015 提高组] 信息传递
观察题目,题目就是让我们求强连通的点大于一的强连通块个数。
//Code by hhy
#include<bits/stdc++.h>
#define F(i , a , b , c) for( int i = (a) ; ((c > 0) ? i <= (b) : i >= (b)) ; i += c )
#define T(i , root , b , c) for( int i = root ; b ; c )
#define int long long
#define W(f) while(f)
#define ull unsigned long long
#define pb push_back
#define fi first
#define se second
#define ll long long
#define debug(...){\
cout<<"debug in function "<<__FUNCTION__<<",line "<<__LINE__<<"\n";\
string s=#__VA_ARGS__,s2="";\
vector<string>v;\
for(auto i:s){\
if(i==','){\
v.push_back(s2);\
s2="";\
}else{\
s2+=i;\
}\
}\
v.push_back(s2);\
vector<int>v2={__VA_ARGS__};\
for(int i=0;i<v.size()-1;i++){\
cout<<v[i]<<"="<<v2[i]<<"\n";\
}\
cout<<v[v.size()-1]<<"="<<v2[v2.size()-1]<<"\n\n";\
}
using namespace std ;
const int kMaxN = 2e5 + 5 , MOD = 998244353 , INF = 1e18 ;
struct Edgepr
{
int u , w ;
};
struct Edgeve
{
int v , w ;
};
struct Edge
{
int v , nxt ;
}edge[kMaxN];
struct node
{
int x , y , id ;
}Node[kMaxN] ;
int n , m , scc , cnt , tim ;
int dfn[kMaxN] , low[kMaxN] , head[kMaxN] , color[kMaxN] ;
bool vis[kMaxN] , res[kMaxN] ;
stack<int> st ;
vector<int> op[kMaxN] ;
inline ll ksm(ll a , ll b)
{
ll mul = 1 ;
while(b)
{
if(b & 1) mul *= a , mul %= MOD ;
a *= a ;
a %= MOD ;
b >>= 1 ;
}
return mul ;
}
inline int read()
{
int x = 0 , f = 1 ;
char ch = getchar() ;
while(ch < '0' || ch > '9')
{
if(ch == '-') f = -1 ;
ch = getchar() ;
}
while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0' , ch = getchar() ;
return x * f ;
}
void write(int x)
{
if(x < 0) putchar('-') , x = -x ;
if(x > 9) write(x / 10) ;
putchar(x % 10 + '0') ;
}
void add_edge(int u , int v)
{
edge[++cnt] = {v , head[u]} ;
head[u] = cnt ;
}
void tanjan(int u)
{
st.push(u) ;
vis[u] = 1 ;
dfn[u] = low[u] = ++tim ;
for( int i = head[u] ; i ; i = edge[i].nxt )
{
int v = edge[i].v ;
if(!dfn[v])
{
tanjan(v) ;
low[u] = min(low[u] , low[v]) ;
}
else if(vis[v] == 1)
{
low[u] = min(dfn[v] , low[u]) ;
}
}
if(dfn[u] == low[u])
{
scc++ ;
while(!st.empty())
{
color[st.top()] = scc ;
vis[st.top()] = 0 ;
op[scc].push_back(st.top()) ;
if(u == st.top())
{
st.pop() ;
break ;
}
st.pop() ;
}
}
}
void Read()
{
}
void work()
{
Read() ;
cin >> n ;
for( int i = 1 ; i <= n ; i++ )
{
int x ;
cin >> x ;
add_edge(i , x) ;
}
for( int i = 1 ; i <= n ; i++ )
{
if(dfn[i] == 0)
{
tanjan(i) ;
}
}
int minn = 1e9 ;
for( int i = 1 ; i <= scc ; i++ )
{
if(op[i].size() >= 2) minn = min(minn , (int)(op[i].size())) ;
}
cout << minn ;
}
signed main()
{
// freopen(".in" , "r" , stdin) ;
// freopen(".out" , "w" , stdout) ;
ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
int t = 1 ;
// cin >> t ;
while(t--) work() ;
return 0 ;
}
P3387 【模板】缩点
对于每个强连通图,计算每个强连通的点的权值。
然后对于每个强连通进行建边,跑一个树上 DP,然后求最大值即可。
//Code by hhy
#include<bits/stdc++.h>
#define F(i , a , b , c) for( int i = (a) ; ((c > 0) ? i <= (b) : i >= (b)) ; i += c )
#define T(i , root , b , c) for( int i = root ; b ; c )
#define int long long
#define W(f) while(f)
#define ull unsigned long long
#define pb push_back
#define fi first
#define se second
#define ll long long
#define debug(...){\
cout<<"debug in function "<<__FUNCTION__<<",line "<<__LINE__<<"\n";\
string s=#__VA_ARGS__,s2="";\
vector<string>v;\
for(auto i:s){\
if(i==','){\
v.push_back(s2);\
s2="";\
}else{\
s2+=i;\
}\
}\
v.push_back(s2);\
vector<int>v2={__VA_ARGS__};\
for(int i=0;i<v.size()-1;i++){\
cout<<v[i]<<"="<<v2[i]<<"\n";\
}\
cout<<v[v.size()-1]<<"="<<v2[v2.size()-1]<<"\n\n";\
}
using namespace std ;
const int kMaxN = 2e5 + 5 , MOD = 998244353 , INF = 1e18 ;
struct Edgepr
{
int u , w ;
};
struct Edgeve
{
int v , w ;
};
struct Edge
{
int v , nxt ;
}edge[100005];
struct node
{
int x , y ;
}Node[kMaxN] ;
int n , m , scc , cnt , tim ;
int dfn[100005] , low[100005] , head[100005] , color[100005] , dp[100005] , a[100005] , siz[100005] ;
bool vis[100005] , res[100005] ;
stack<int> st ;
inline ll ksm(ll a , ll b)
{
ll mul = 1 ;
while(b)
{
if(b & 1) mul *= a , mul %= MOD ;
a *= a ;
a %= MOD ;
b >>= 1 ;
}
return mul ;
}
inline int read()
{
int x = 0 , f = 1 ;
char ch = getchar() ;
while(ch < '0' || ch > '9')
{
if(ch == '-') f = -1 ;
ch = getchar() ;
}
while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0' , ch = getchar() ;
return x * f ;
}
void write(int x)
{
if(x < 0) putchar('-') , x = -x ;
if(x > 9) write(x / 10) ;
putchar(x % 10 + '0') ;
}
void add_edge(int u , int v)
{
edge[++cnt] = {v , head[u]} ;
head[u] = cnt ;
}
void tanjan(int u)
{
st.push(u) ;
vis[u] = 1 ;
dfn[u] = low[u] = ++tim ;
for( int i = head[u] ; i ; i = edge[i].nxt )
{
int v = edge[i].v ;
if(!dfn[v])
{
tanjan(v) ;
low[u] = min(low[u] , low[v]) ;
}
else if(vis[v] == 1)
{
low[u] = min(dfn[v] , low[u]) ;
}
}
if(dfn[u] == low[u])
{
scc++ ;
while(st.top() != u)
{
color[st.top()] = scc ;
vis[st.top()] = 0 ;
siz[scc] += a[st.top()] ;
st.pop() ;
}
color[st.top()] = scc ;
vis[st.top()] = 0 ;
siz[scc] += a[st.top()] ;
st.pop() ;
}
}
void dfs(int u)
{
if(dp[u] != -1) return ;
dp[u] = siz[u] ;
int maxn = 0 ;
for( int i = head[u] ; i ; i = edge[i].nxt )
{
int v = edge[i].v ;
if(dp[v] == -1) dfs(v) ;
maxn = max(maxn , dp[v]) ;
}
dp[u] += maxn ;
}
void Read()
{
}
void work()
{
Read() ;
cin >> n >> m ;
for( int i = 1 ; i <= n ; i++ ) cin >> a[i] ;
for( int i = 1 ; i <= m ; i++ )
{
int u , v ;
cin >> u >> v ;
add_edge(u , v) ;
Node[i] = {u , v} ;
}
for( int i = 1 ; i <= n ; i++ )
{
if(dfn[i] == 0)
{
tanjan(i) ;
}
}
for( int i = 1 ; i <= n ; i++ ) head[i] = 0 ;
cnt = 0 ;
for( int i = 1 ; i <= m ; i++ )
{
if(color[Node[i].x] != color[Node[i].y])
{
add_edge(color[Node[i].x] , color[Node[i].y]) ;
}
}
memset(dp , -1 , sizeof dp) ;
int ans = 0 ;
for( int i = 1 ; i <= scc ; i++ )
{
if(dp[i] == -1)
{
dfs(i) ;
ans = max(ans , dp[i]) ;
}
}
cout << ans ;
}
signed main()
{
// freopen(".in" , "r" , stdin) ;
// freopen(".out" , "w" , stdout) ;
ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
int t = 1 ;
// cin >> t ;
while(t--) work() ;
return 0 ;
}