【题目链接】 http://poj.org/problem?id=3180

【题目大意】

  N头牛,M条有向绳子,能组成几个歌舞团?要求顺时针逆时针都能带动舞团内所有牛。

【题解】

  等价于求点数大于1的SCC数量。

【代码】

#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
const int MAX_V=10000;
int V; //顶点数
vector<int> G[MAX_V]; //图的邻接表表示
vector<int> rG[MAX_V]; //反向图
vector<int> vs; //后序遍历
bool used[MAX_V];
int cmp[MAX_V]; //所属强连通分量的拓扑序
void add_edge(int from,int to){
G[from].push_back(to);
rG[to].push_back(from);
}
void dfs(int v){
used[v]=1;
for(int i=0;i<G[v].size();i++){
if(!used[G[v][i]])dfs(G[v][i]);
}vs.push_back(v);
}
void rdfs(int v,int k){
used[v]=1;
cmp[v]=k;
for(int i=0;i<rG[v].size();i++){
if(!used[rG[v][i]])rdfs(rG[v][i],k);
}
}
int scc(){
memset(used,0,sizeof(used));
vs.clear();
for(int v=0;v<V;v++){if(!used[v])dfs(v);}
memset(used,0,sizeof(used));
int k=0;
for(int i=vs.size()-1;i>=0;i--){
if(!used[vs[i]])rdfs(vs[i],k++);
}return k;
}
const int MAX_M=50000;
int N,M;
int A[MAX_M],B[MAX_M];
int cnt[MAX_M];
void solve(){
V=N;
for(int i=0;i<M;i++){
add_edge(A[i]-1,B[i]-1);
}int n=scc();
int num=0;
memset(cnt,0,sizeof(cnt));
for(int i=0;i<V;i++)++cnt[cmp[i]];
for(int i=0;i<n;i++)if(cnt[i]>1)num++;
printf("%d\n",num);
}
int main(){
while(~scanf("%d%d",&N,&M)){
for(int i=0;i<M;i++)scanf("%d%d",&A[i],&B[i]);
solve();
}return 0;
}

POJ 3180 The Cow Prom(SCC)的更多相关文章

  1. poj 3180 The Cow Prom(强联通分量)

    http://poj.org/problem?id=3180 The Cow Prom Time Limit: 1000MS   Memory Limit: 65536K Total Submissi ...

  2. poj 3180 The Cow Prom(tarjan+缩点 easy)

    Description The N ( <= N <= ,) cows are so excited: it's prom night! They are dressed in their ...

  3. POJ 3268 Silver Cow Party (最短路径)

    POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...

  4. POJ 3617 Best Cow Line (模拟)

    题目链接 Description FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Yea ...

  5. POJ 1236 Network of Schools(SCC)

    [题目链接] http://poj.org/problem?id=1236 [题目大意] 给出一张有向图,问需要从几个起点出发才能遍历全图, 如果要求从任何一个点出发都能遍历全图,那么最少需要增加几条 ...

  6. POJ 3617 Best Cow Line (贪心)

    Best Cow Line   Time Limit: 1000MS      Memory Limit: 65536K Total Submissions: 16104    Accepted: 4 ...

  7. poj 3267 The Cow Lexicon(dp)

    题目:http://poj.org/problem?id=3267 题意:给定一个字符串,又给n个单词,求最少删除字符串里几个字母,能匹配到n个单词里 #include <iostream> ...

  8. POJ 3268 Silver Cow Party (Dijkstra)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions:28457   Accepted: 12928 ...

  9. 【POJ 3176】Cow Bowling(DP)

    题 Description The cows don't use actual bowling balls when they go bowling. They each take a number ...

随机推荐

  1. 【NOIP模拟赛】天神下凡 动态开点线段树

    这些圆一定是在同一水平面上的,由于他们没有相交,因此我们发现他们每个人与外界关系可以分为,1.存在并圈圈 2.存在圈圈并被割,因此我们把所有的圆都加1,把被割的在加1,就可以啦,因此我们开一个线段树, ...

  2. BZOJ3295: [Cqoi2011]动态逆序对 莫队

    这个用莫队做会被卡,但是我还是...... 收获在于树状数组维护后缀和以及双维排序...... 莫队的时间复杂度比想象中的要好一些.... 然而我还是被卡了...... #include<ios ...

  3. bzoj 4836 [Lydsy1704月赛]二元运算 分治FFT+生成函数

    [Lydsy1704月赛]二元运算 Time Limit: 8 Sec  Memory Limit: 128 MBSubmit: 577  Solved: 201[Submit][Status][Di ...

  4. C# new override

    A -> virtual Fun B : A -> override Fun C : B -> override Fun D : C -> new virtual Fun E ...

  5. 新手如何更换自己喜欢的背景以及此背景的css码

    以下内容为转载(对于css码可以自己写当然也可以去网上搜现成的): 更换背景教学:https://jingyan.baidu.com/album/fc07f9897c730412ffe519c0.ht ...

  6. 【BZOJ】1799: [Ahoi2009]self 同类分布

    [题意]给出a,b,求出[a,b]中各位数字之和能整除原数的数的个数.1 ≤ a ≤ b ≤ 10^18 [算法]数位DP [题解] 感觉这种方法很暴力啊. 枚举数位和1~162(不能枚举0,不然会模 ...

  7. 阶段性总结⓵触摸事件&手势识别⓶Quartz2D绘图⓷CALayer图层⓸CAAnimation⓹UIDynamic UI动力学⓺KVC&KVO

    知识点复习   1. 触摸事件&手势识别   1> 4个触摸事件,针对视图的 2> 6个手势识别(除了用代码添加,也可以用Storyboard添加)   附加在某一个特定视图上的, ...

  8. js事件中绑定另一事件导致事件多次执行

    1.html代码 <input type="button" value="add"> <input type="button&quo ...

  9. Linux ALSA介绍

    1. 介绍 ALSA(即Advanced Linux Sound Architecture), 是目前Linux的主流音频体系结构, 提供了音频和MIDI的支持, 其架构图如下所示 TIP: 笔者的代 ...

  10. 基于java容器注解---基于泛型的自动装配

    上面利用@Configuration和@Bean配置bean,中间利用@Autowired,指定s1和s2分别指向哪个实现类,下面利用@Autowired,指定s中只有Integer实现类 例子: 在 ...