POJ 3180 The Cow Prom(SCC)
【题目链接】 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)的更多相关文章
- poj 3180 The Cow Prom(强联通分量)
http://poj.org/problem?id=3180 The Cow Prom Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- poj 3180 The Cow Prom(tarjan+缩点 easy)
Description The N ( <= N <= ,) cows are so excited: it's prom night! They are dressed in their ...
- POJ 3268 Silver Cow Party (最短路径)
POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...
- 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 ...
- POJ 1236 Network of Schools(SCC)
[题目链接] http://poj.org/problem?id=1236 [题目大意] 给出一张有向图,问需要从几个起点出发才能遍历全图, 如果要求从任何一个点出发都能遍历全图,那么最少需要增加几条 ...
- POJ 3617 Best Cow Line (贪心)
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16104 Accepted: 4 ...
- poj 3267 The Cow Lexicon(dp)
题目:http://poj.org/problem?id=3267 题意:给定一个字符串,又给n个单词,求最少删除字符串里几个字母,能匹配到n个单词里 #include <iostream> ...
- POJ 3268 Silver Cow Party (Dijkstra)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions:28457 Accepted: 12928 ...
- 【POJ 3176】Cow Bowling(DP)
题 Description The cows don't use actual bowling balls when they go bowling. They each take a number ...
随机推荐
- 【NOIP模拟赛】beautiful 乱搞(平衡树)+ST
biubiu~~~ 我用平衡树处理的这道题,然而这种方法还是要看评测姬..... 正解是乱搞....就是枚举每一位数作为中位数,比他小的看做-1比他大的看做1,那么我们从一开始就有了一个绵延的山,我们 ...
- 如何获取iframe DOM的值
在Web开发时,很多时候会遇到一个问题.我在一个页面嵌入了iframe,并且我想获得这个iframe页面某个元素的值.那么该如何实现这个需求呢? 先来看下演示: 效果演示 iframe1中文本框的值: ...
- fastjson对json操作
fastjson对json字符串JSONObject和JSONArray互相转换操作示例 fastjson的方法: Fastjson API入口类是com.alibaba.fastjson.JSON ...
- Ubuntu1604 install netease-cloud music
Two issue: 1. There is no voice on my computer, and the system was mute and cannot unmute. eric@E641 ...
- MSTest DeploymentItemAttribute
该attribute可以把指定的文件拷贝到每次运行的Out目录下,比如有一个config文件,那么用下面的命令, [TestClass] [DeploymentItem("Default.c ...
- Java之戳中痛点 - (2)取余用偶判断,不要用奇判断
取余判断原则:取余用偶判断,不要用奇判断 先看一个 程序: package com.test; import java.util.Scanner; public class t1 { public s ...
- [06] JavaScript 类型
下面对知识点总结: 1.类型分类 a.原始类型:number, string, boolean, null, undefined b.对象类型:除了原始类型都是(例如:object,array, fu ...
- 【洛谷 P3629】 [APIO2010]巡逻 (树的直径)
题目链接 容易发现,当加一条边时,树上会形成一个环,这个环上的每个点都是只要走一次的,也就是说我们的答案减少了这个环上点的个数,要使答案最小,即要使环上的点最多,求出直径\(L\),则答案为\(2(n ...
- (转)Git冲突:commit your changes or stash them before you can merge. 解决办法
用git pull来更新代码的时候,遇到了下面的问题: error: Your local changes to the following files would be overwritten by ...
- Python 数据库连接池DButils
常规的数据库链接存在的问题: 场景一: 缺点:每次请求反复创建数据库连接,连接数太多 import pymysql def index(): conn = pymysql.connect() curs ...