切水题,写题解~

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. 被疯狂吐槽的iPhoneXR屏幕真如传言中的那么差吗?

    在双十一期间,最受果粉关注的电商平台不是天猫,也不是京东,而是拼多多!原因很简单,拼多多疯狂给出iPhone的各种超低价格,直接压制了竞争对手.以iPhone XR为例,依据容量不同,价格分别为558 ...

  2. OpenJudge - NOI - 1.1编程基础之输入输出(C语言 全部题解)

    01:Hello, World! #include <stdio.h> int main(void) { printf("Hello, World!"); return ...

  3. 136-PHP 使用类名访问static修饰的类方法

    <?php class test{ //定义一个类 public static function class_info(){ //定义类方法 return '这是一个用于测试的类.'; } } ...

  4. C#文本操作

    1.使用FileStream读写文件 文件头: 复制代码代码如下: using System;using System.Collections.Generic;using System.Text;us ...

  5. Node.js NPM 介绍

    章节 Node.js NPM 介绍 Node.js NPM 作用 Node.js NPM 包(Package) Node.js NPM 管理包 Node.js NPM Package.json NPM ...

  6. python 网页爬虫 基础篇

    首先要连接自己的数据库 import pymysql import requests #需要导入模块 db = pymysql.connect('localhost', 'root', '****** ...

  7. vant库在vue全局引入toast组件

    第一步: 在config中引入 // 全局引入vant的提示框 import { Toast } from "vant"; Vue.use(Toast); 第二步: 在组要的.vu ...

  8. VUE中常用的一些方法

    1.获取URL中的参数 export function getUrlKey(name) { return decodeURIComponent((new RegExp('[?|&]' + na ...

  9. 执行 composer update 命令的时候报 Your requirements could not be resolved to an installable set of packages. 错误

    Your requirements could not be resolved to an installable set of packages. 以上原因:不匹配composer.json要求的版 ...

  10. windows driver 创建线程

    VOID ThreadStart(_In_ PVOID StartContext) { PWCHAR str = (PWCHAR)StartContext; MySleep(10);//延时10ms ...