当前位置:网站首页>AcWing164. 可达性统计(拓扑排序+bitset)
AcWing164. 可达性统计(拓扑排序+bitset)
2022-07-05 00:02:00 【eva_can(not)survive】
164. 可达性统计 - AcWing题库高质量的算法题库https://www.acwing.com/problem/content/166/第一次用到bitset真是一个神奇的容器啊,让我们计算子集非常方便。
这个题也直接给出了是有个DAG所以我们可以直接求拓扑序列,然后倒序遍历将其每个点能触及到的状态记录。
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
#include <bitset>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MN = 65005;
const int MAXN = 1e6 + 10;
const int INF = 0x3f3f3f3f;
#define IOS ios::sync_with_stdio(false)
#define lowbit(x) ((x)&(-x))
using P = pair<int, int>;
int ver[MAXN];
int head[MAXN];
int nxt[MAXN];
int cnt;
int in[MAXN];
int tuop[MAXN];
int cnt1;
void add(int x, int y) {
ver[++cnt] = y;
nxt[cnt] = head[x];
head[x] = cnt;
in[y]++;
}
int n, m;
const int N = 3e4 + 5;
bitset<N> rec[N];
void topsort() {
queue<int> q;
map<int, int> mp;
for (int i = 1; i <= n; i++) {
if (in[i])
continue;
q.push(i);
}
while (!q.empty()) {
int t = q.front();
q.pop();
tuop[++cnt1] = t;
for (int i = head[t]; i; i = nxt[i]) {
int v = ver[i];
in[v]--;
if (!in[v])
q.push(v);
}
}
}
int main() {
scanf("%d %d", &n, &m);
int x, y;
for (int i = 1; i <= m; i++) {
scanf("%d %d", &x, &y);
add(x, y);
}
topsort();
for (int i = n; i >= 1; i--) {
int j = tuop[i];
rec[j][j] = 1;
for (int k = head[j]; k; k = nxt[k]) {
int v = ver[k];
rec[j] |= rec[v];
}
}
for (int i = 1; i <= n; i++) {
printf("%d\n", rec[i].count());
}
return 0;
}
边栏推荐
- A new method for analyzing the trend chart of London Silver
- 他做国外LEAD,用了一年时间,把所有房贷都还清了
- How to save your code works quickly to better protect your labor achievements
- JS 将伪数组转换成数组
- PMP certificate renewal process
- Tester's algorithm interview question - find mode
- Hong Kong Jewelry tycoon, 2.2 billion "bargain hunting" Giordano
- C语言中sizeof操作符的坑
- Application of multi loop instrument in base station "switching to direct"
- 如何避免电弧产生?—— AAFD故障电弧探测器为您解决
猜你喜欢
企业公司项目开发好一部分基础功能,重要的事保存到线上第一a
微服务(Microservice)那点事儿
如何有效对直流列头柜进行监测
Jar batch management gadget
ICML 2022 | 3dlinker: e (3) equal variation self encoder for molecular link design
【kotlin】第三天
Design of emergency lighting evacuation indication system for urban rail transit station
机器人强化学习——Learning Synergies between Pushing and Grasping with Self-supervised DRL (2018)
基于三维gis平台的消防系统运用
高配笔记本使用CAD搬砖时卡死解决记录
随机推荐
PMP证书续证流程
go踩坑——no required module provides package : go.mod file not found in current directory or any parent
Cross domain request
认识ThreadPoolExecutor
企业公司项目开发好一部分基础功能,重要的事保存到线上第一a
Summary of week 22-07-02
Hong Kong Jewelry tycoon, 2.2 billion "bargain hunting" Giordano
Using the uniapp rich text editor
Why does infographic help your SEO
Power operation and maintenance cloud platform: open the new mode of "unattended and few people on duty" of power system
Parsing of XML
圖解網絡:什麼是網關負載均衡協議GLBP?
Illustrated network: what is gateway load balancing protocol GLBP?
如何避免电弧产生?—— AAFD故障电弧探测器为您解决
快解析——好用的内网安全软件
解决无法通过ssh服务远程连接虚拟机
图解网络:什么是网关负载均衡协议GLBP?
Using fast parsing intranet penetration to realize zero cost self built website
Jar batch management gadget
Meet ThreadPoolExecutor