[luoguP2863] [USACO06JAN]牛的舞会The Cow Prom(Tarjan)

传送门

有向图,找点数大于1的强连通分量个数

——代码

 1 #include <stack>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5
 6 const int MAXN = 50001;
 7 int n, m, cnt, idx, size, ans;
 8 int head[MAXN], to[MAXN << 1], next[MAXN << 1];
 9 int dfn[MAXN], low[MAXN], belong[MAXN], tot[MAXN];
10 bool ins[MAXN];
11 std::stack <int> s;
12
13 inline int read()
14 {
15     int x = 0, f = 1;
16     char ch = getchar();
17     for(; !isdigit(ch); ch = getchar()) if(ch == ‘-‘) f = -1;
18     for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - ‘0‘;
19     return x * f;
20 }
21
22 inline int min(int x, int y)
23 {
24     return x < y ? x : y;
25 }
26
27 inline void add(int x, int y)
28 {
29     to[cnt] = y;
30     next[cnt] = head[x];
31     head[x] = cnt++;
32 }
33
34 inline void dfs(int u)
35 {
36     int i, v;
37     s.push(u);
38     ins[u] = 1;
39     dfn[u] = low[u] = ++idx;
40     for(i = head[u]; i ^ -1; i = next[i])
41     {
42         v = to[i];
43         if(!dfn[v])
44         {
45             dfs(v);
46             low[u] = min(low[u], low[v]);
47         }
48         else if(ins[v]) low[u] = min(low[u], dfn[v]);
49     }
50     if(low[u] == dfn[u])
51     {
52         size++;
53         do
54         {
55             v = s.top();
56             s.pop();
57             ins[v] = 0;
58             belong[v] = size;
59         }
60         while(u ^ v);
61     }
62 }
63
64 int main()
65 {
66     int i, x, y;
67     n = read();
68     m = read();
69     memset(head, -1, sizeof(head));
70     for(i = 1; i <= m; i++)
71     {
72         x = read();
73         y = read();
74         add(x, y);
75     }
76     for(i = 1; i <= n; i++)
77         if(!dfn[i])
78             dfs(i);
79     for(i = 1; i <= n; i++) tot[belong[i]]++;
80     for(i = 1; i <= size; i++)
81         if(tot[i] > 1)
82             ans++;
83     printf("%d\n", ans);
84     return 0;
85 }

时间: 2024-10-15 13:23:33

[luoguP2863] [USACO06JAN]牛的舞会The Cow Prom(Tarjan)的相关文章

[USACO06JAN]牛的舞会The Cow Prom

[USACO06JAN]牛的舞会The Cow Prom 题目描述 The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in their finest gowns, complete with corsages and new shoes. They know that tonight they will each try to perform the Round Dance. Only

P2863 [USACO06JAN]牛的舞会The Cow Prom

洛谷——P2863 [USACO06JAN]牛的舞会The Cow Prom 题目描述 The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in their finest gowns, complete with corsages and new shoes. They know that tonight they will each try to perform the Round D

luogu P2863 [USACO06JAN]牛的舞会The Cow Prom

https://www.luogu.org/problem/show?pid=2863#sub 题目描述 The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in their finest gowns, complete with corsages and new shoes. They know that tonight they will each try to perform th

洛谷——P2863 [USACO06JAN]牛的舞会The Cow Prom

https://www.luogu.org/problem/show?pid=2863#sub 题目描述 The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in their finest gowns, complete with corsages and new shoes. They know that tonight they will each try to perform th

[USACO06JAN] 牛的舞会 The Cow Prom

题目描述 The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in their finest gowns, complete with corsages and new shoes. They know that tonight they will each try to perform the Round Dance. Only cows can perform the Round D

【luogu P2863 [USACO06JAN]牛的舞会The Cow Prom】 题解

题目链接:https://www.luogu.org/problemnew/show/P2863 求强连通分量大小>自己单个点的 #include <stack> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int maxn = 100000 + 10; struct edge

【USACO06JAN】牛的舞会The Cow Prom

题目描述 约翰的N (2 <= N <= 10,000)只奶牛非常兴奋,因为这是舞会之夜!她们穿上礼服和新鞋子,别 上鲜花,她们要表演圆舞. 只有奶牛才能表演这种圆舞.圆舞需要一些绳索和一个圆形的水池.奶牛们围在池边站好, 顺时针顺序由1到N编号.每只奶牛都面对水池,这样她就能看到其他的每一只奶牛. 为了跳这种圆舞,她们找了 M(2<M< 50000)条绳索.若干只奶牛的蹄上握着绳索的一端, 绳索沿顺时针方绕过水池,另一端则捆在另一些奶牛身上.这样,一些奶牛就可以牵引另一些奶 牛.

牛的舞会The Cow Prom

题面 tarjan模板题 1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cmath> 5 #include<cstring> 6 #include<stack> 7 using namespace std; 8 const int M=500005; 9 10 stack < int > q; 11 int n,m,tot,

USACO06JAN The Cow Prom /// tarjan求强联通分量 oj24219

题目大意: n个点 m条边的图 求大小大于1的强联通分量的个数 https://www.cnblogs.com/stxy-ferryman/p/7779347.html tarjan求完强联通分量并染色后 计算一下每种颜色的个数 就是每个强联通块的大小 #include <stdio.h> #include <cstring> #include <algorithm> #include <stack> using namespace std; const i