洛谷P2863 [USACO06JAN]牛的舞会The Cow Prom
2018-06-17 21:41:36来源:未知 阅读 ()
ng 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 …
输入输出格式
输入格式:
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.
输出格式:
Line 1: A single line with a single integer that is the number of groups successfully dancing the Round Dance.
输入输出样例
5 4 2 4 3 5 1 2 4 1
1
说明
Explanation of the sample:
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____/ /
\_______/
Cows 1, 2, and 4 are properly connected and form a complete Round Dance group. Cows 3 and 5 don't have the second rope they'd need to be able to pull both ways, thus they can not properly perform the Round Dance.
强联通分量的裸题
直接输出强联通分量的个数就好
1 #include<cstdio> 2 #include<cstring> 3 #include<stack> 4 #include<queue> 5 #include<map> 6 #include<stack> 7 #define ls k<<1 8 #define rs k<<1|1 9 #define LL long long 10 using namespace std; 11 const int MAXN=100002; 12 inline int read() 13 { 14 char c=getchar();int x=0,flag=1; 15 while(c<'0'||c>'9') {if(c=='-') flag=-1;c=getchar();} 16 while(c>='0'&&c<='9') x=x*10+c-48,c=getchar();return x*flag; 17 } 18 struct node 19 { 20 int u,v,nxt; 21 }edge[MAXN]; 22 int head[MAXN]; 23 int num=1; 24 inline void add_edge(int x,int y) 25 { 26 edge[num].u=x; 27 edge[num].v=y; 28 edge[num].nxt=head[x]; 29 head[x]=num++; 30 } 31 int low[MAXN]; 32 int dfn[MAXN];//时间戳 33 int tot=0; 34 stack<int>s; 35 int ans=0; 36 int vis[MAXN]; 37 int color[MAXN],colornum=0; 38 int happen[MAXN]; 39 inline void Tarjan(int node) 40 { 41 low[node]=dfn[node]=++tot; 42 vis[node]=1; 43 s.push(node); 44 for(int i=head[node];i!=-1;i=edge[i].nxt) 45 { 46 if(!dfn[edge[i].v]) 47 Tarjan(edge[i].v),low[node]=min(low[node],low[edge[i].v]); 48 else if(vis[edge[i].v]) 49 low[node]=min(low[node],dfn[edge[i].v]); 50 } 51 if(dfn[node]==low[node]) 52 { 53 colornum++; 54 int h; 55 do 56 { 57 h=s.top(); 58 if(color[s.top()]==0) color[s.top()]=colornum; 59 vis[s.top()]=0; 60 s.pop(); 61 }while(node!=h); 62 } 63 } 64 int main() 65 { 66 memset(head,-1,sizeof(head)); 67 int n=read(),m=read(); 68 for(int i=1;i<=m;i++) 69 { 70 int x=read(),y=read(); 71 add_edge(x,y); 72 } 73 for(int i=1;i<=n;i++) 74 if(color[i]==0) 75 Tarjan(i); 76 for(int i=1;i<=n;i++) 77 happen[color[i]]++; 78 int ans=0; 79 for(int i=1;i<=colornum;i++) 80 if(happen[i]>1) 81 ans++; 82 printf("%d",ans); 83 return 0; 84 }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:洛谷P2818 天使的起誓
下一篇:什么是数组?
- 洛谷P1164->小A点菜 2020-05-18
- 洛谷P1907口算练习题 2020-03-24
- 结题报告--P5551洛谷--Chino的树学 2020-03-13
- 结题报告--洛谷P3915 2020-03-13
- 洛谷P1034 矩形覆盖 2020-03-10
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash