洛谷P2341 [HAOI2006]受欢迎的牛 (Tarjan,SCC缩点)
P2341 [HAOI2006]受歡迎的牛|【模板】強(qiáng)連通分量
https://www.luogu.org/problem/P2341
題目描述
每頭奶牛都夢想成為牛棚里的明星。被所有奶牛喜歡的奶牛就是一頭明星奶牛。所有奶
牛都是自戀狂,每頭奶??偸窍矚g自己的。奶牛之間的“喜歡”是可以傳遞的——如果A喜
歡B,B喜歡C,那么A也喜歡C。牛欄里共有N 頭奶牛,給定一些奶牛之間的愛慕關(guān)系,請你
算出有多少頭奶??梢援?dāng)明星。
輸入格式
? 第一行:兩個用空格分開的整數(shù):N和M
? 第二行到第M + 1行:每行兩個用空格分開的整數(shù):A和B,表示A喜歡B
輸出格式
? 第一行:單獨(dú)一個整數(shù),表示明星奶牛的數(shù)量
輸入輸出樣例
輸入 #1復(fù)制
輸出 #1復(fù)制
說明/提示
只有 3 號奶??梢宰雒餍?/p>
【數(shù)據(jù)范圍】
10%的數(shù)據(jù)N<=20, M<=50
30%的數(shù)據(jù)N<=1000,M<=20000
70%的數(shù)據(jù)N<=5000,M<=50000
100%的數(shù)據(jù)N<=10000,M<=50000
思路:
使用Tarjan算法把SCC縮成點(diǎn)后,在DAG中判斷是否有一個節(jié)點(diǎn),使所有其他節(jié)點(diǎn)都可以到達(dá)它。
判斷方法:
DAG中有且僅有一個節(jié)點(diǎn)出度為0,那么這個點(diǎn)就滿足所有其他節(jié)點(diǎn)都可以到達(dá)它。
代碼:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #include <iomanip> #define ALL(x) (x).begin(), (x).end() #define sz(a) int(a.size()) #define rep(i,x,n) for(int i=x;i<n;i++) #define repd(i,x,n) for(int i=x;i<=n;i++) #define pii pair<int,int> #define pll pair<long long ,long long> #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define MS0(X) memset((X), 0, sizeof((X))) #define MSC0(X) memset((X), '\0', sizeof((X))) #define pb push_back #define mp make_pair #define fi first #define se second #define eps 1e-6 #define gg(x) getInt(&x) #define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl #define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c)) #define du2(a,b) scanf("%d %d",&(a),&(b)) #define du1(a) scanf("%d",&(a)); using namespace std; typedef long long ll; ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;} ll lcm(ll a, ll b) {return a / gcd(a, b) * b;} ll powmod(ll a, ll b, ll MOD) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;} void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}} void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}inline void getInt(int *p); const int maxn = 100010; const int inf = 0x3f3f3f3f; /*** TEMPLATE CODE * * STARTS HERE ***/int From[maxn], Laxt[maxn], To[maxn << 2], Next[maxn << 2], cnt; int low[maxn], dfn[maxn], times, q[maxn], head, scc_cnt, scc[maxn]; vector<int>G[maxn]; int dis[maxn], S, T, ans; int num[maxn]; void add(int u, int v) {Next[++cnt] = Laxt[u]; From[cnt] = u;Laxt[u] = cnt; To[cnt] = v; } void tarjan(int u, int fa) {dfn[u] = low[u] = ++times;q[++head] = u;for (int i = Laxt[u]; i; i = Next[i]) { // if (To[i] == fa) { continue; }if (!dfn[To[i]]) {tarjan(To[i], u);low[u] = min(low[u], low[To[i]]);} else { low[u] = min(low[u], dfn[To[i]]); }}if (low[u] == dfn[u]) {scc_cnt++; while (true) {int x = q[head--];scc[x] = scc_cnt;num[scc_cnt]++;if (x == u) { break; }}} } void init() {memset(Laxt, 0, sizeof(Laxt));cnt = 0; } int main() {init();int N, M, u, v, i, j;scanf("%d%d", &N, &M);for (i = 1; i <= M; i++) {scanf("%d%d", &u, &v);add(u, v);}repd(i, 1, N) {if (!dfn[i]) {tarjan(i, 0);}}for (i = 1; i <= N; i++) {for (j = Laxt[i]; j; j = Next[j]) {if (scc[i] != scc[To[j]]) {G[scc[i]].push_back(scc[To[j]]);}}}int id;int out = 0;repd(i, 1, scc_cnt) {if (sz(G[i]) == 0) {out++;id = i;}}if (out == 1) {printf("%d\n", num[id] );} else {printf("0\n");}return 0; } inline void getInt(int *p) {char ch;do {ch = getchar();} while (ch == ' ' || ch == '\n');if (ch == '-') {*p = -(getchar() - '0');while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 - ch + '0';}} else {*p = ch - '0';while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 + ch - '0';}} }轉(zhuǎn)載于:https://www.cnblogs.com/qieqiemin/p/11585285.html
總結(jié)
以上是生活随笔為你收集整理的洛谷P2341 [HAOI2006]受欢迎的牛 (Tarjan,SCC缩点)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 高并发 高负载 网站系统架构 !深入讨论
- 下一篇: [转贴]怎样规划你毕业以后的人生