bzoj 1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会 -- Tarjan
1654: [Usaco2006 Jan]The Cow Prom 奶牛舞會(huì)
Time Limit:?5 Sec??Memory Limit:?64 MBDescription
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 Dance which requires a set of ropes and a circular stock tank. To begin, the cows line up around a circular stock tank and number themselves in clockwise order consecutively from 1..N. Each cow faces the tank so she can see the other dancers. They then acquire a total of M (2 <= M <= 50,000) ropes all of which are distributed to the cows who hold them in their hooves. Each cow hopes to be given one or more ropes to hold in both her left and right hooves; some cows might be disappointed. For the Round Dance to succeed for any given cow (say, Bessie), the ropes that she holds must be configured just right. To know if Bessie's dance is successful, one must examine the set of cows holding the other ends of her ropes (if she has any), along with the cows holding the other ends of any ropes they hold, etc. When Bessie dances clockwise around the tank, she must instantly pull all the other cows in her group around clockwise, too. Likewise, if she dances the other way, she must instantly pull the entire group counterclockwise (anti-clockwise in British English). Of course, if the ropes are not properly distributed then a set of cows might not form a proper dance group and thus can not succeed at the Round Dance. One way this happens is when only one rope connects two cows. One cow could pull the other in one direction, but could not pull the other direction (since pushing ropes is well-known to be fruitless). Note that the cows must Dance in lock-step: a dangling cow (perhaps with just one rope) that is eventually pulled along disqualifies a group from properly performing the Round Dance since she is not immediately pulled into lockstep with the rest. Given the ropes and their distribution to cows, how many groups of cows can properly perform the Round Dance? Note that a set of ropes and cows might wrap many times around the stock tank.
約翰的N(2≤N≤10000)只奶牛非常興奮,因?yàn)檫@是舞會(huì)之夜!她們穿上禮服和新鞋子,別上鮮花,她們要表演圓舞. 只有奶牛才能表演這種圓舞.圓舞需要一些繩索和一個(gè)圓形的水池.奶牛們圍在池邊站好,順時(shí)針順序由1到N編號.每只奶牛都面對水池,這樣她就能看到其他的每一只奶牛.為了跳這種圓舞,她們找了M(2≤M≤50000)條繩索.若干只奶牛的蹄上握著繩索的一端,繩索沿順時(shí)針方繞過水池,另一端則捆在另一些奶牛身上.這樣,一些奶牛就可以牽引另一些奶牛.有的奶牛可能握有很多繩索,也有的奶牛可能一條繩索都沒有對于一只奶牛,比如說貝茜,她的圓舞跳得是否成功,可以這樣檢驗(yàn):沿著她牽引的繩索,找到她牽引的奶牛,再沿著這只奶牛牽引的繩索,又找到一只被牽引的奶牛,如此下去,若最終能回到貝茜,則她的圓舞跳得成功,因?yàn)檫@一個(gè)環(huán)上的奶牛可以逆時(shí)針牽引而跳起旋轉(zhuǎn)的圜舞.如果這樣的檢驗(yàn)無法完成,那她的圓舞是不成功的. 如果兩只成功跳圓舞的奶牛有繩索相連,那她們可以同屬一個(gè)組合. 給出每一條繩索的描述,請找出,成功跳了圓舞的奶牛有多少個(gè)組合?Input
* Line 1: Two space-separated integers: N and M
* Lines 2..M+1: Each line contains two space-separated integers A and B that describe a rope from cow A to cow B in the clockwise direction.
第1行輸入N和M,接下來M行每行兩個(gè)整數(shù)A和B,表示A牽引著B.Output
* Line 1: A single line with a single integer that is the number of groups successfully dancing the Round Dance.
成功跳圓舞的奶牛組合數(shù).?
Sample Input
5 42 4
3 5
1 2
4 1
INPUT DETAILS:
ASCII art for Round Dancing is challenging. Nevertheless, here is a
representation of the cows around the stock tank:
_1___
/**** \
5 /****** 2
/ /**TANK**|
\ \********/
\ \******/ 3
\ 4____/ /
\_______/
Sample Output
1HINT
?
1,2,4這三只奶牛同屬一個(gè)成功跳了圓舞的組合.而3,5兩只奶牛沒有跳成功的圓舞
?
?
Source
#include<cstdio> #include<iostream> #define N 10010 #define M 50010 using namespace std; inline int min(int a,int b){return a<b?a:b;} inline int read() {int x=0;char ch=getchar();while(ch<'0'||ch>'9') ch=getchar();while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x; } int dfn[N],low[N],tp,a,b,n,m,lj[N],to[M],fro[M],cnt,q[N],tq,ans; void add(int u,int v){cnt++;to[cnt]=v;fro[cnt]=lj[u];lj[u]=cnt;} bool vs[N]; void tj(int k) {dfn[k]=low[k]=++tp;q[++tq]=k;vs[k]=1;for(int i=lj[k];i;i=fro[i]){if(!dfn[to[i]]){tj(to[i]);low[k]=min(low[k],low[to[i]]);}else if(vs[to[i]]) low[k]=min(low[k],low[to[i]]);}if(dfn[k]==low[k]){int d=0;while(q[tq]!=k){vs[q[tq--]]=0;d++;}vs[q[tq--]]=0;d++;if(d>1) ans++;} } int main() {n=read(); m=read();for(int i=0;i<m;i++){a=read();b=read();add(a,b);}for(int i=1;i<=n;i++) if(!vs[i]) tj(i);printf("%d\n",ans); }?
轉(zhuǎn)載于:https://www.cnblogs.com/lkhll/p/6408816.html
總結(jié)
以上是生活随笔為你收集整理的bzoj 1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会 -- Tarjan的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。