The Cow Prom(POJ-3180)
Problem 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 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.
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.
Output
Line 1: A single line with a single integer that is the number of groups successfully dancing the Round Dance.
Sample Input
5 4
2 4
3 5
1 2
4 1Sample Output
1
題意:有編號從 1-N 的牛,給出 M 組有向的連線方案,當能組成一個環時(連線數大於等於2的閉環)被視為連線成功,求連線成功的組數
思路:實質為求大於等於2的強連通分量個數,用 Kosaraju 演算法,然後縮點染色,最後判斷每種顏色的個數,如果大於1,最後輸出個數+1
Source Program
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<ctime>
#include<vector>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define N 200001
#define MOD 16007
#define E 1e-6
#define LL long long
using namespace std;
int n,m;
vector<int> g[N];
vector<int> gt[N];
bool vis[N];
int color[N];
int num[N];
int cnt[N];
int sig;
void dfs1(int x){
vis[x]=true;
for(int i=0;i<g[x].size();i++){
int y=g[x][i];
if(vis[y]==false)
dfs1(y);
}
num[sig++]=x;
}
void dfs2(int x){
vis[x]=true;
color[x]=sig;
for(int i=0;i<gt[x].size();i++){
int y=gt[x][i];
if(vis[y]==false)
dfs2(y);
}
}
void Kosaraju(){
sig=1;
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)//對原圖搜尋
if(vis[i]==false)
dfs1(i);
sig=0;
memset(vis,0,sizeof(vis));
for(int i=n;i>=1;i--)//對反圖搜尋
if(vis[num[i]]==0){
sig++;
dfs2(num[i]);
}
memset(cnt,0,sizeof(cnt));
for(int i=1;i<=n;i++)//縮點,統計顏色個數
cnt[color[i]]++;
int res=0;
for(int i=1;i<=sig;i++)
if(cnt[i]>1)
res++;
printf("%d\n",res);
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=1;i<=n;i++){
g[i].clear();
gt[i].clear();
}
for(int i=0;i<m;i++){
int x,y;
scanf("%d%d",&x,&y);
g[x].push_back(y);
gt[y].push_back(x);
}
Kosaraju();
}
return 0;
}
相關文章
- PCIe Tandem PROM 方法
- POJ 3267 The Cow Lexicon(dp)
- POJ3278 Catch That Cow
- 什麼是 Dirty COW 漏洞
- [譯]Prometheus監控NodeJS SDK(prom-client)使用說明PrometheusNodeJSclient
- 【貪心】POJ 3617:Best Cow Line
- Cow Marathon(BFS求數的直徑)
- POJ 3360-Cow Contest(傳遞閉包)
- P3045 【USACO12FEB】 Cow Coupons G
- COW奶牛!CopyOnWrite機制瞭解一下
- Java 中的寫時複製 (Copy on Write, COW)Java
- P3067 [USACO12OPEN] Balanced Cow Subsets G
- 洛谷 P3034 Cow Photography G/S——題解
- P2344 [USACO11FEB] Generic Cow Protests G
- P2854 [USACO06DEC] Cow Roller Coaster SAST
- P2853 [USACO06DEC]Cow Picnic S 【DFS】
- 【LuoguP3611 】[USACO17JAN]Cow Dance Show S
- COW奶牛!Copy On Write機制瞭解一下
- P8271 [USACO22OPEN] COW Operations S (思維)
- POJ3268 Silver Cow Party【Dijkstra演算法+思維】演算法
- POJ3660 Cow Contest【Floyd演算法 傳遞閉包】演算法
- POJ 3613 Cow Relays 矩陣乘法Floyd+矩陣快速冪矩陣
- 「日常訓練」「小專題·圖論」 Cow Contest (1-3)圖論
- Linux--寫時複製(Copy-On-Write,COW)技術簡述Linux
- P3045 [USACO12FEB] Cow Coupons G (用堆實現反悔貪心)