BZOJ1654 [Usaco2006 Jan]The Cow Prom 奶牛舞会

看不懂题,蒟蒻中文英文都太差了。。。

于是Orz itwiiioi巨巨!

结果终于理解了:就是求有向图非单点的强连通分量个数。

tarjan妥妥的。。。(板子*1 get√)

 1 /**************************************************************
 2     Problem: 1654
 3     User: rausen
 4     Language: C++
 5     Result: Accepted
 6     Time:32 ms
 7     Memory:1476 kb
 8 ****************************************************************/
 9
10 #include <cstdio>
11 #include <cstring>
12 #include <algorithm>
13
14 using namespace std;
15 const int N = 10005;
16 const int M = 50005;
17 struct edges{
18     int next, to;
19 }e[M];
20 int n, m;
21 int cnt, top, num, tot, ans, sz;
22 int dfn[N], low[N], vis[N], s[N], first[N];
23
24 inline int read(){
25     int x = 0, sgn = 1;
26     char ch = getchar();
27     while (ch < ‘0‘ || ch > ‘9‘){
28         if (ch == ‘-‘) sgn = -1;
29         ch = getchar();
30     }
31     while (ch >= ‘0‘ && ch <= ‘9‘){
32         x = x * 10 + ch - ‘0‘;
33         ch = getchar();
34     }
35     return sgn * x;
36 }
37
38 inline void add_edge(int x, int y){
39     e[++tot].next = first[x];
40     first[x] = tot;
41     e[tot].to = y;
42 }
43
44 void DFS(int p){
45     dfn[p] = low[p] = ++cnt;
46     s[++top] = p, vis[p] = 1;
47     int x, y;
48     for (x = first[p]; x; x = e[x].next){
49         y = e[x].to;
50         if (!vis[y]) DFS(y);
51         if (vis[y] < 2)
52             low[p] = min(low[p], low[y]);
53     }
54     if (dfn[p] == low[p]){
55         ++num, sz = 0;
56         while (s[top + 1] != p){
57             y = s[top--];
58             vis[y] = 2;
59             ++sz;
60         }
61         if (sz != 1) ++ ans;
62     }
63 }
64
65 inline void tarjan(){
66     cnt = top = num = 0;
67     memset(vis, sizeof(vis), 0);
68     for (int i = 1; i <= n; ++i)
69         if (!vis[i]) DFS(i);
70 }
71
72 int main(){
73     n = read(), m = read();
74     int x, y;
75     for (int i = 1; i <= m; ++i){
76         x = read(), y = read();
77         add_edge(x, y);
78     }
79     tarjan();
80     printf("%d\n", ans);
81     return 0;
82 }

(p.s. 为什么改进版比不改进版要慢。。。这不科学的T T)

时间: 2024-08-28 22:21:39

BZOJ1654 [Usaco2006 Jan]The Cow Prom 奶牛舞会的相关文章

【强连通分量】Bzoj1654 [Usaco2006 Jan]The Cow Prom 奶牛舞会

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

1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会

1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 471  Solved: 339[Submit][Status][Discuss] Description The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in their finest gowns, complet

【BZOJ1654】[Usaco2006 Jan]The Cow Prom 奶牛舞会 赤果果的tarjan

Description 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

bzoj:1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会

Description 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

P1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会

裸的强连通 1 const maxe=50001; 2 type 3 node=record 4 f,t:longint; 5 end; 6 var n,m,dgr,i,u,v,num,ans:longint; 7 bfsdgr,low,head,f:array[0..maxe] of longint; 8 b:array[0..maxe] of node; 9 p:array[0..maxe] of boolean; 10 procedure insert(u,v:longint); 11 b

bzoj 1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会【tarjan】

几乎是板子,求有几个size>1的scc 直接tarjan即可 #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int N=10005; int n,m,h[N],cnt,ans,tmp,dfn[N],low[N],s[N],top; bool v[N]; struct qwe { int ne,to; }e[N*10]; int read() { i

[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

[BZOJ1648][Usaco2006 Dec]Cow Picnic 奶牛野餐

1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 781  Solved: 483 [Submit][Status][Discuss] Description The cows are having a picnic! Each of Farmer John's K (1 <= K <= 100) cows is grazing in one of N (1 <= N &