当前位置:网站首页>例题 可达性统计+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;
}
边栏推荐
- What is SPL?
- 【翻译】混沌网+SkyWalking:为混沌工程提供更好的可观察性
- 【 temperature warning program DE development 】 event driven model instance
- linux下oracle常见操作以及日常积累知识点(函数、定时任务)
- Chapter 4: activiti RuntimeService settings get and get process variables, and the difference from taskService, set process variables when starting and completing tasks [easy to understand]
- 什么是 DevOps?看这一篇就够了!
- Huawei's lightweight neural network architecture GhostNet has been upgraded again, and G-GhostNet (IJCV22) has shown its talents on the GPU
- Technical dry goods | Hausdorff distance for image segmentation based on MindSpore
- SMB + SMB2: Accessing shares return an error after prolonged idle period
- Ali's new launch: Microservices Assault Manual, all operations are written out in PDF
猜你喜欢
多线程(进阶) - 2.5w字总结
Voice-based social software development - making the most of its value
Opencv算术操作
The host computer develops C# language: simulates the STC serial port assistant to receive the data sent by the microcontroller
电气工程的标准是什么
Opencv图像缩放和平移
基于MindSpore高效完成图像分割,实现Dice!
语音社交软件开发——充分发挥其价值
Technical dry goods | Hausdorff distance for image segmentation based on MindSpore
Jenkins manual (2) - software configuration
随机推荐
SkiaSharp 之 WPF 自绘 投篮小游戏(案例版)
[Office] Collection of Microsoft Office download addresses (offline installation and download of Microsoft's official original version)
七夕来袭!还要做CDH数据迁移怎么办?来看看DistCp
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
告白数字化转型时代:麦聪软件以最简单的方式让企业把数据用起来
FPGA: Basic Getting Started Button Controlling LED Lights
2022华数杯数学建模A题环形振荡器的优化设计思路思路代码分享
阿里顶级架构师多年总结的JVM宝典,哪里不会查哪里!
012_SSS_ Improving Diffusion Model Efficiency Through Patching
单片机:温度控制DS18B20
数据可视化(二)
Chapter 4: In the activiti process, variable transmission and acquisition process variables, setting and acquiring multiple process variables, setting and acquiring local process variables "recommende
Use KUSTO query statement (KQL) to query LOG on Azure Data Explorer Database
Chapter 5: Multithreaded Communication—wait and notify
Opencv算术操作
第五章:多线程通信—wait和notify
Chapter 4: activiti RuntimeService settings get and get process variables, and the difference from taskService, set process variables when starting and completing tasks [easy to understand]
攻防世界-PWN-new_easypwn
第五章:redis持久化,包括rdb和aof两种方式[通俗易懂]
Score interview (1)----related to business