当前位置:网站首页>例题 可达性统计+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;
}
边栏推荐
- Meteorological data processing example - matlab string cutting matching and R language date matching (data splicing)
- 导火索:OAuth 2.0四种授权登录方式必读
- static linking and dynamic linking
- 入门 Polkadot 平行链开发,看这一篇就够了
- 还在找网盘资源吗?快点收藏如下几个值得收藏的网盘资源搜索神器吧!
- In-depth understanding of timeout settings for Istio traffic management
- 第四章:activiti流程中,变量的传递和获取流程变量 ,设置和获取多个流程变量,设置和获取局部流程变量「建议收藏」
- GPU-CUDA-图形渲染分析
- 数据可视化(二)
- 我们的Web3创业项目,黄了
猜你喜欢
What is SPL?
SQL Outer Join Intersection, Union, Difference Query
JS逆向入门学习之回收商网,手机号码简易加密解析
深入理解 Istio 流量管理的超时时间设置
【温度预警程序de开发】事件驱动模型实例运用
Leetcode刷题——623. 在二叉树中增加一行
MySQL事务
Our Web3 Entrepreneurship Project, Yellow
技术干货 | 基于 MindSpore 实现图像分割之豪斯多夫距离
First Decentralized Heist?Loss of nearly 200 million US dollars: analysis of the attack on the cross-chain bridge Nomad
随机推荐
three.js debugging tool dat.gui use
[Android] How to use RecycleView in Kotlin project
第四章:activiti RuntimeService设置获和取流程变量,及与taskService的区别,开始和完成任务时设置流程变量[通俗易懂]
How does the official account operate and maintain?Public account operation and maintenance professional team
poj2287 Tian Ji -- The Horse Racing(2016xynu暑期集训检测 -----C题)
Opencv算术操作
【Unity】【UGUI】【在屏幕上显示文本】
The query that the user's test score is greater than the average score of a single subject
力扣(LeetCode)216. 组合总和 III(2022.08.04)
Is digital transformation a business buy-in?
Confessing in the era of digital transformation: Mai Cong Software allows enterprises to use data in the easiest way
用KUSTO查询语句(KQL)在Azure Data Explorer Database上查询LOG实战
What are the standards for electrical engineering
高质量 DeFi 应用构建指南,助力开发者玩转 DeFi Summer
Where is your most secretive personality?
攻防世界-PWN-new_easypwn
Huawei's lightweight neural network architecture GhostNet has been upgraded again, and G-GhostNet (IJCV22) has shown its talents on the GPU
MySQL data view
SQL外连接之交集、并集、差集查询
Dynamics 365Online PDF导出及打印