当前位置:网站首页>例题 可达性统计+bitset的使用
例题 可达性统计+bitset的使用
2022-08-05 10:28:00 【一条小小yu】
给定一张 NN 个点 MM 条边的有向无环图,分别统计从每个点出发能够到达的点的数量。
输入格式
第一行两个整数 N,MN,M,接下来 MM 行每行两个整数 x,yx,y,表示从 xx 到 yy 的一条有向边。
输出格式
输出共 NN 行,表示每个点能够到达的点的数量。
数据范围
1≤N,M≤300001≤N,M≤30000
输入样例:
10 10 3 8 2 3 2 5 5 9 5 9 2 3 3 9 4 8 2 10 4 9输出样例:
1 6 3 3 2 1 1 1 1 1
#include<bits/stdc++.h>
using namespace std;
int n,m,tot=0,cnt=0;
typedef long long ll;
const int maxn=3e4+5;
int a[maxn],in[maxn],is[maxn],head[maxn];
struct point
{
int to;
int nxt;
} edge[maxn*4];
inline long long read()
{
long long Sum=0;
int F=1;
char c=getchar();
while(c>'9' || c<'0')
{
if(c=='-')
F=-1;
c=getchar();
}
while(c>='0' && c<='9')
{
Sum=Sum*10+c-'0';
c=getchar();
}
return Sum*F;
}
void add(int u,int v)
{
tot++;
edge[tot].to=v;
edge[tot].nxt=head[u];
head[u]=tot;
in[v]++;
}
void Topo_sort()
{
queue<int> q;
for(int i=1; i<=n; i++)
if(in[i]==0)
{
q.push(i);
}
while(!q.empty())
{
int tt=q.front();
q.pop();
a[++cnt]=tt;
for(int i=head[tt]; i; i=edge[i].nxt)
{
int v=edge[i].to;
in[v]--;
if(in[v]==0)
q.push(v);
}
}
}
bitset<maxn>bs[maxn];
int main()
{
n=read(),m=read();
for(int i=1; i<=m; i++)
{
int u,v;
u=read();
v=read();
add(u,v);
}
Topo_sort();
for(int i=cnt; i>0; --i)
{
int x = a[i];
bs[x][x]=1;
for(int j=head[x]; j; j=edge[j].nxt)
{
int y=edge[j].to;
bs[x]=bs[x]|bs[y];
}
}
for(int i=1; i<=n; i++)
{
printf("%d\n",bs[i].count());
}
return 0;
}
边栏推荐
- 用户考试分数大于单科科目平均分的查询
- PHP operation mangoDb
- Jenkins使用手册(2) —— 软件配置
- [Office] Collection of Microsoft Office download addresses (offline installation and download of Microsoft's official original version)
- The JVM collection that Alibaba's top architects have summarized for many years, where can't I check it!
- The fuse: OAuth 2.0 four authorized login methods must read
- RT-Thread记录(一、RT-Thread 版本、RT-Thread Studio开发环境 及 配合CubeMX开发快速上手)
- Opencv算术操作
- The century-old Nordic luxury home appliance brand ASKO smart wine cabinet in the three-temperature area presents the Chinese Valentine’s Day, and tastes the love of the delicacy
- 012_SSS_ Improving Diffusion Model Efficiency Through Patching
猜你喜欢

Opencv算术操作

字节一面:TCP 和 UDP 可以使用同一个端口吗?

【温度预警程序de开发】事件驱动模型实例运用

This notebook of concurrent programming knowledge points strongly recommended by Ali will be a breakthrough for you to get an offer from a big factory

Our Web3 Entrepreneurship Project, Yellow

这份阿里强推的并发编程知识点笔记,将是你拿大厂offer的突破口

Voice-based social software development - making the most of its value

登录功能和退出功能(瑞吉外卖)

技术干货 | 基于 MindSpore 实现图像分割之豪斯多夫距离

In-depth understanding of timeout settings for Istio traffic management
随机推荐
Introduction to SD NAND Flash!
three objects are arranged in a spherical shape around the circumference
我们的Web3创业项目,黄了
2022华数杯数学建模思路分析交流
[Android]如何使用RecycleView in Kotlin project
js hijacks the array push method
2022华数杯数学建模A题环形振荡器的优化设计思路思路代码分享
首次去中心化抢劫?近2亿美元损失:跨链桥Nomad 被攻击事件分析
PHP operation mangoDb
Dynamics 365Online PDF导出及打印
FPGA: Basic Getting Started LED Lights Blinking
Where is your most secretive personality?
What are the standards for electrical engineering
第五章:activiti流程分流判断,判断走不同的任务节点
A small test of basic grammar, Go lang1.18 introductory refining tutorial, from Bai Ding to Hongru, basic grammar of go lang and the use of variables EP02
How does the official account operate and maintain?Public account operation and maintenance professional team
012_SSS_ Improving Diffusion Model Efficiency Through Patching
教你本地编译运行一个IDEA插件,在IDEA里聊天、下棋、斗地主!
第四章:redis 数组结构的set和一些通用命令「建议收藏」
R语言使用yardstick包的pr_curve函数评估多分类(Multiclass)模型的性能、查看模型在多分类每个分类上的ROC曲线(precision(精准率),R代表的是recall(召回率)