分量"/>
关于强连通分量
首先要明白什么是强连通分量?
强连通分量实际上指的是一些点的集合,而这个集合的定义就是:任意集合中的点都能到达其他所有同样在集合中的点
然后就是强连通分量的意义,或者说是作用:
就目前而言,我认为他的作用就是缩点。
什么叫做缩点呢?
就是当把整个图中的点分成多个不同的集合以后,每个集合都可以看成一个点。然后每个集合之间的连线就像这些超级点的连线一样。
为什么要缩点?
这有点像物理中的整体法,就是当这些集合中的点具有等价性,或者说可以看成一个整体的时候就可以缩点了(这几句话比较绕,而且没什么卵用。。。具体方法可以看例题)
然后先是强连通分量的求法:
(先贴的是我自己脑补的板子,目测应该是正确的)
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cmath>
#include<stack>using namespace std;const int maxn = 1e3 + 5;
bool vis[maxn] = {false};
int n, m;
int dfs[maxn] = {0}, dfn[maxn] = {0}, low[maxn] = {0}, num[maxn] = {0};
int cnt = 0, tnt = 0;
vector<int> edge[maxn];
stack<int> s;inline void putit()
{int a, b; // a to bscanf("%d%d", &n, &m);for(int i = 1; i <= m; ++i){scanf("%d%d", &a, &b);edge[a].push_back(b);}
}void tarjan(int t)
{if(dfn[t] == 0){tnt++; dfn[t] = tnt; low[t] = tnt; vis[t] = true;s.push(t);}for(int i = 0; i < edge[t].size(); ++i){int now = edge[t][i];if(!vis[now]) tarjan(now);low[t] = min(low[t], low[now]);} if(low[t] == dfn[t]){cnt++;while(!s.empty()){int now = s.top();s.pop();num[now] = cnt;printf("%d ", now);if(dfn[now] == low[now]) break;}printf("\n");}
}inline void print()
{for(int i = 1; i <= n; ++i)printf("i == %d dfn[i] == %d low[i] == %d num[i] == %d\n", i, dfn[i], low[i], num[i]);
}inline void workk()
{for(int i = 1; i <= n; ++i)if(!vis[i]) tarjan(i);
}int main()
{putit();workk();print();
}
这个板子应该可以直接用吧。
例题:
T1:
题目描述
在幻想乡,上白泽慧音是以知识渊博闻名的老师。春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄。因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点。人间之里由N个村庄(编号为1..N)和M条道路组成,道路分为两种一种为单向通行的,一种为双向通行的,分别用1和2来标记。如果存在由村庄A到达村庄B的通路,那么我们认为可以从村庄A到达村庄B,记为(A,B)。当(A,B)和(B,A)同时满足时,我们认为A,B是绝对连通的,记为<A,B>。绝对连通区域是指一个村庄的集合,在这个集合中任意两个村庄X,Y都满足<X,Y>。现在你的任务是,找出最大的绝对连通区域,并将这个绝对连通区域的村庄按编号依次输出。若存在两个最大的,输出字典序最小的,比如当存在1,3,4和2,5,6这两个最大连通区域时,输出的是1,3,4。
输入输出格式
输入格式:第1行:两个正整数N,M
第2..M+1行:每行三个正整数a,b,t, t = 1表示存在从村庄a到b的单向道路,t = 2表示村庄a,b之间存在双向通行的道路。保证每条道路只出现一次。
第1行: 1个整数,表示最大的绝对连通区域包含的村庄个数。
第2行:若干个整数,依次输出最大的绝对连通区域所包含的村庄编号。
说明
对于60%的数据:N <= 200且M <= 10,000
对于100%的数据:N <= 5,000且M <= 50,000
输入输出样例
5 5 1 2 1 1 3 2 2 4 2 5 1 2 3 5 1
3
1 3 5
显然一看就是一道板子题,为什么要贴呢?
因为我这次的板子是用的是蓝书上的板子(道理和我基本上差不多)
(PS :我并没有排字典序,因为洛谷数据很水的嘛~)
贴代码:
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cmath>
#include<stack>using namespace std;const int maxn = 5e3 + 5;
int pre[maxn] = {0}, lowlink[maxn] = {0}, sccno[maxn] = {0}, dfs_clock = 0, scc_cnt = 0;
int num[maxn] = {0};
int n, m, maxx = 0, ans = 0;
vector<int>edge[maxn];
stack<int> s;inline void putit()
{int a, b, t;scanf("%d%d", &n, &m);for(int i = 1; i <= m; ++i){scanf("%d%d%d", &a, &b, &t);edge[a].push_back(b);if(t == 2) edge[b].push_back(a);}
}void tarjan(int t)
{++dfs_clock;pre[t] = lowlink[t] = dfs_clock;s.push(t);for(int i = 0; i < edge[t].size(); ++i){int now = edge[t][i];if(lowlink[now] == 0){tarjan(now);lowlink[t] = min(lowlink[t], lowlink[now]);}else if(sccno[now] == 0){lowlink[t] = min(lowlink[t], lowlink[now]);}}if(lowlink[t] == pre[t]){scc_cnt++;for(;;){int now = s.top();s.pop();sccno[now] = scc_cnt;num[scc_cnt]++;if(now == t) break;}
// printf("%d %d ", scc_cnt, num[scc_cnt]);if(maxx < num[scc_cnt])maxx = num[scc_cnt], ans = scc_cnt;//printf("maxx == %d\n", maxx);
// printf("%d maxx == %d\n", num[scc_cnt], maxx);}
}inline void workk()
{for(int i = 1; i <= n; ++i)if(sccno[i] == 0) tarjan(i);
// for(int i = 1; i <= n; ++i)
// {
// printf("i == %d %d\n", i, sccno[i]);
// }printf("%d\n", maxx);for(int i = 1; i <= n; ++i){if(sccno[i] == ans){printf("%d ", i);}}
}int main()
{putit();workk();return 0;
}
题目描述
每头奶牛都梦想成为牛棚里的明星。被所有奶牛喜欢的奶牛就是一头明星奶牛。所有奶
牛都是自恋狂,每头奶牛总是喜欢自己的。奶牛之间的“喜欢”是可以传递的——如果A喜
欢B,B喜欢C,那么A也喜欢C。牛栏里共有N 头奶牛,给定一些奶牛之间的爱慕关系,请你
算出有多少头奶牛可以当明星。
输入输出格式
输入格式:第一行:两个用空格分开的整数:N和M
第二行到第M + 1行:每行两个用空格分开的整数:A和B,表示A喜欢B
第一行:单独一个整数,表示明星奶牛的数量
说明
只有 3 号奶牛可以做明星
【数据范围】
10%的数据N<=20, M<=50
30%的数据N<=1000,M<=20000
70%的数据N<=5000,M<=50000
100%的数据N<=10000,M<=50000
输入输出样例
3 3 1 2 2 1 2 3
1
基本上求了强连通分量后在一顿瞎操作就好了呀(操作方法真的太多了,大家可以奇思妙想一下)
代码奉上:
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cmath>
#include<stack>
#include<set>using namespace std;const int maxn = 1e5 + 5;
int n, m;
int cnt = 0, tnt = 0;
int lin, mm = 0;
int tar[maxn] = {0};
int num[maxn] = {0}, low[maxn] = {0}, dfn[maxn] = {0};
bool vis[maxn] = {false};
bool flag[maxn] = {false};
vector<int> edge1[maxn];
vector<int> zero;
stack<int> s;
set<int> ss[maxn];inline void putit()
{int a, b;scanf("%d%d", &n, &m);for(int i = 1; i <= m; ++i){scanf("%d%d", &a, &b); // a to bedge1[a].push_back(b);}
}void tarjan(int t)
{if(dfn[t] == 0){tnt++;dfn[t] = tnt;low[t] = tnt;vis[t] = true;s.push(t);}for(int i = 0; i < edge1[t].size(); ++i){int now = edge1[t][i];if(!vis[now]) tarjan(now);low[t] = min(low[now], low[t]);}if(low[t] == dfn[t]){cnt++;while(!s.empty()){int now = s.top(); s.pop();num[now] = cnt;tar[cnt]++;//printf("%d ", now);if(dfn[now] == low[now]) break;}//printf("\n");}
}inline void workk()
{for(int i = 1; i <= n; ++i){for(int j = edge1[i].size() - 1; j >= 0; --j){if(num[i] != num[edge1[i][j]]){flag[num[i]] = true;break;}}}for(int i = 1; i <= cnt; ++i){if(!flag[i]){mm++;lin = i;if(mm == 2){printf("0");exit(0);}}}printf("%d ", tar[lin]);
}int main()
{putit();for(int i = 1; i <= n; ++i)if(!vis[i]) tarjan(i);
// for(int i = 1; i <= n; ++i)
// printf("num[%d] == %d\n", i, num[i]) ;workk();
}
更多推荐
关于强连通分量
发布评论