当前位置:网站首页>例题 可达性统计+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;
}
边栏推荐
- Is digital transformation a business buy-in?
- Complete image segmentation efficiently based on MindSpore and realize Dice!
- Create a Dapp, why choose Polkadot?
- Brief Analysis of WSGI Protocol
- Opencv图像缩放和平移
- 第三章 : redis数据结构种类
- 2022华数杯数学建模思路分析交流
- How to choose coins and determine the corresponding strategy research
- [Android] How to use RecycleView in Kotlin project
- 百年北欧奢华家电品牌ASKO智能三温区酒柜臻献七夕,共品珍馐爱意
猜你喜欢

012年通过修补_sss_提高扩散模型效率

MySQL transactions

Confessing in the era of digital transformation: Mai Cong Software allows enterprises to use data in the easiest way

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

多线程(进阶) - 2.5w字总结

High-quality DeFi application building guide to help developers enjoy DeFi Summer

还在找网盘资源吗?快点收藏如下几个值得收藏的网盘资源搜索神器吧!

NowCoderTOP35-40——持续更新ing

Huawei's lightweight neural network architecture GhostNet has been upgraded again, and G-GhostNet (IJCV22) has shown its talents on the GPU

012_SSS_ Improving Diffusion Model Efficiency Through Patching
随机推荐
[Unity] [UGUI] [Display text on the screen]
MySQL事务
SMB + SMB2: Accessing shares return an error after prolonged idle period
2022 Huashu Cup Mathematical Modeling Question A Optimization Design Ideas for Ring Oscillators Code Sharing
百年北欧奢华家电品牌ASKO智能三温区酒柜臻献七夕,共品珍馐爱意
three objects are arranged in a spherical shape around the circumference
攻防世界-PWN-new_easypwn
创建一个 Dapp,为什么要选择波卡?
In-depth understanding of timeout settings for Istio traffic management
Our Web3 Entrepreneurship Project, Yellow
企业的数字化转型到底是否可以买来?
E-sports, convenience, efficiency, security, key words for OriginOS functions
[Strong Net Cup 2022] WP-UM
JS逆向入门学习之回收商网,手机号码简易加密解析
Dynamics 365Online PDF导出及打印
如何修改管理工具client_encoding
【 temperature warning program DE development 】 event driven model instance
Microcontroller: temperature control DS18B20
Import Excel/CSV from Sub Grid within Dynamics 365
高质量 DeFi 应用构建指南,助力开发者玩转 DeFi Summer