切水题,写题解~

tarjan缩一波点,然后

  • 只有一个出度为0的点:他的size就是答案

  • 有多个初度为0的点:无解,0个

因为是强联通分量,所以肯定有出度为0的点,否则——就是你tarjan写挂了~

\[Talk\;is\;free\;,\;show\;me\;the\;code
\]

#include<iostream>
#include<cstdio>
#include<vector>
#include<stack>
#include<algorithm>
#define MAXN 200000
using namespace std ;
inline void read(int &x) {
char ch=getchar();
int s=0,f=1;
while (!(ch>='0'&&ch<='9')) {
if (ch=='-') f=-1;
ch=getchar();
}
while (ch>='0'&&ch<='9') {
s=(s<<3)+(s<<1)+ch-'0';
ch=getchar();
}
x=s*f;
}
vector<int> edge[MAXN] ;
int ins[MAXN] , low[MAXN] , dfn[MAXN] ;
int tot , whr[MAXN] ;
vector<pair<vector<int>,vector<int> > > scc ;
int deg[MAXN] ;
int n , m ;
void add(int u,int v){
edge[u].push_back(v) ;
}
void print(stack<int> s){
vector<int> sns ; sns.clear() ;
while(!s.empty()){
cerr<<s.top()<<" " ; s.pop() ;
}
cerr<<endl ;
}
stack<int> idx ;
vector<int> ptpuntil(int v , int wr){
//print(idx) ;
vector<int> nodes ;
while((!idx.empty())&&idx.top()!=v)
nodes.push_back(idx.top()) , whr[idx.top()] = wr , idx.pop() ;
nodes.push_back(idx.top()) , whr[idx.top()] = wr , idx.pop() ;
return nodes ;
} int totally = 1 ;
void tarjan(int node){
dfn[node] = low[node] = ++totally ; ins[node] = 1 ; idx.push(node) ;
for(auto& i:edge[node]){
if(dfn[i]==0){
tarjan(i) ;
low[node] = min(low[node] , low[i]) ;
}
else if(ins[node]==1){
low[node] = min(low[node] , dfn[i]) ;
}
}
if(dfn[node]==low[node]){
//cout<<"NODE "<<node<<endl ;
scc.push_back(make_pair(ptpuntil(node,scc.size()),vector<int>())) ;
}
}
void _add(int a,int b){
if(a!=b) scc[a].second.push_back(b) ;
}
void join(){
//cerr<<"JOIN WORKS!"<<endl ;
//cerr<<"n = "<<n<<endl ;
for(int i=1;i<=n;++i)
for(auto& j : edge[i])
_add(whr[i],whr[j]) ;
for(auto& i : scc) sort(i.second.begin(),i.second.end()) , unique(i.second.begin(),i.second.end()) ;
}
void print(vector<int> V){
for(auto& i : V) cerr<<i<<" " ;
}
void print(vector<pair<vector<int> , vector<int> > > V){
int m = 0 ;
for(auto& i : V){
cerr<<"Vector No."<<++m<<":\n\tfirst :" ;
print(i.first) ;
cerr<<"\n\tsecond:" ;
print(i.second) ;
cerr<<endl ;
}
}
void answer(){
join() ;
//print(scc) ;
vector<int> zrs ;
zrs.clear() ;
for(int i=0;i<scc.size();++i){
if(!scc[i].second.size()) zrs.push_back(i) ;
//cout<<"PUSH "<<i<<endl ;
}
if(zrs.size()>1) printf("0\n") ;
else {
printf("%d\n",scc[*zrs.begin()].first.size()) ;
}
}
int main(){
read(n) , read(m) ;
for(int i=1;i<=m;++i){
int x , y ; read(x) , read(y) , add(x,y) ;
}
for(int i=1;i<=n;++i){
if(!dfn[i]) totally = 0 , tarjan(i) ;
}
answer() ;
}

题解【[HAOI2006]受欢迎的牛】的更多相关文章

  1. BZOJ 1051: [HAOI2006]受欢迎的牛 缩点

    1051: [HAOI2006]受欢迎的牛 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/ ...

  2. 【BZOJ1051】[HAOI2006]受欢迎的牛

    [BZOJ1051][HAOI2006]受欢迎的牛 题面 bzoj 洛谷 题解 假如\(A\)喜欢\(B\)就连一条\(A\)到\(B\)的边 然后缩点,如果图不连通就\(Impossible\) 否 ...

  3. bzoj1051: [HAOI2006]受欢迎的牛(强联通)

    1051: [HAOI2006]受欢迎的牛 题目:传送门 题解: 今天又做一道水题... 强联通啊很明显 水个模板之后统计一下每个强联通分量中点的个数,再统计一下出度... 不难发现:缩点之后当且仅当 ...

  4. BZOJ 1051: [HAOI2006]受欢迎的牛(SCC)

    1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 8172  Solved: 4470[Submit][Sta ...

  5. P2341 [HAOI2006]受欢迎的牛(更完)

    P2341 [HAOI2006]受欢迎的牛 题解 tarjan 缩点板子题 如果 A 稀饭 B,那就 A 向 B 连边,构造出一个有向图 如果这个有向图里有强连通分量,也就说明这个强连通分量里的所有奶 ...

  6. bzoj1051 [HAOI2006]受欢迎的牛

    1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4773  Solved: 2541[Submit][Sta ...

  7. 【bzoj1051】 [HAOI2006]受欢迎的牛 tarjan缩点判出度算点数

    [bzoj1051] [HAOI2006]受欢迎的牛 2014年1月8日7450 Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B ...

  8. 1051: [HAOI2006]受欢迎的牛

    1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2276  Solved: 1190[Submit][Sta ...

  9. 【BZOJ】1051: [HAOI2006]受欢迎的牛

    [HAOI2006]受欢迎的牛 Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这种关系是具有传递性的,如果A认为B受欢 ...

  10. bzoj 1051: [HAOI2006]受欢迎的牛 tarjan缩点

    1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2092  Solved: 1096[Submit][Sta ...

随机推荐

  1. CAN通讯基本设置

    A节点pelican协议下,扩展帧 单滤波方式 A节点的接收滤波器 ID号 设置为 0x19881205 设置过程 (1) pelican模式设置 设置时钟分频寄存器CDR.7 =1 使SJA1000 ...

  2. ping不通www.baidu.com,但可以访问www.baidu.com网页

    https://blog.csdn.net/stpeace/article/details/45116425 了解网络的人, 基本上都用过ping命令, 这个优秀的小工具通常能非常靠谱地检测网络的连通 ...

  3. GNS3 模拟icmp禁止不可达

    R1 : conf t int f0/0 no shutdown ip add 192.168.1.1 255.255.255.0 no ip routing end R2 f0/0: conf t ...

  4. C++学习链表

    #include"pch.h" #include<iostream> #include<string> using namespace std; struc ...

  5. eclipse 安装spring tools suite插件

    之前使用idea进行springboot项目的开发学习,但是由于idea是收费的,总是用着用着说验证码到期之类的,总之还是很不爽,于是就想重新采用eclipse开发springboot项目,为了方便s ...

  6. maven集成SSM项目,Tomcat部署运行——SSM整合框架搭建(二)之问题

    问题一.当放开controller中的方法,出现如下问题 ### Error querying database. Cause: org.springframework.jdbc.CannotGetJ ...

  7. BMP字节流转成CBitmap类

    BYTE* m_pBmpData = NULL; BITMAPINFO* m_pBmpInfo = new BITMAPINFO[sizeof(BITMAPINFOHEADER)+sizeof(RGB ...

  8. django 中从外界借助多个网站时 static 的存放和整理

    在 模板之家中  前端页面直接上去抓取  可是遇到重复  或者 版本不统一  所以 在每个app下面建立自己的 stastic 在制作的html  页面上方 导入静态页面 {% load static ...

  9. R语言 线性回归分析实例 《回归分析与线性统计模型》page72

    y,X1,X2,X3 分别表示第 t 年各项税收收入(亿元),某国生产总值GDP(亿元),财政支出(亿元)和商品零售价格指数(%). (1) 建立线性模型: ① 自己编写函数: > librar ...

  10. 基础语法-for循环的嵌套

    基础语法-for循环的嵌套 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.for循环嵌套概述 说白了就是在for循环中再嵌套一层for循环. 二.for循环嵌套案例 1> ...