【题目链接】 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. ionic2-键盘覆盖输入框和返回键问题解决方案

    http://blog.csdn.net/u012979009/article/details/52514892有遇到这个问题的去这个地址看

  2. HttpClient测试类请求端和服务端即可能出现乱码的解决

    junit HttpClient 请求端 代码: package com.taotao.httpclient; import java.util.ArrayList; import java.util ...

  3. IOI1998 Polygon [区间dp]

    [IOI1998]Polygon 题意翻译 题目可能有些许修改,但大意一致 多边形是一个玩家在一个有n个顶点的多边形上的游戏,如图所示,其中n=4.每个顶点用整数标记,每个边用符号+(加)或符号*(乘 ...

  4. hbase监控实现

    目前实现的监控概览

  5. python基础(3)_列表、元组、字典

    一.列表 定义:[ ] 内以逗号分隔,按照索引,存放各种数据类型,每个位置代表一个元素 特性: > 可存放多个值 > 可修改指定索引位置对应的值,可变 > 按照从左到右的顺序定义列表 ...

  6. HDFS之FileSystem

    package cn.hx.test; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; impo ...

  7. [bzoj3306]树——树上倍增+dfs序+线段树

    Brief Description 您需要写一种数据结构,支持: 更改一个点的点权 求一个子树的最小点权 换根 Algorithm Design 我们先忽略第三个要求. 看到要求子树的最小点权,我们想 ...

  8. Linux服务器中毒事件(libudev.so)

    今天机房管理人员反馈公司的某台服务器在防火墙上的连接数超限,登陆服务器时发现非常卡顿,远程登录后查看,CPU持续100%,且有一长度为10的随机字符串进程,kill掉,会重新生成另外长度为10的字符串 ...

  9. es修改数据

    # 官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html#bulk-routing * ...

  10. shell命令行混合进制计算器smartbc

    需要简单的计算的时候,不想用GUI的计算器,能在shell下直接计算就最好了 查了下,有个东西叫 bc,  具体的使用就不赘述了,可以运行bc,然后进去计算,也可以echo传递过去,大概是像这样 ec ...