当前位置:网站首页>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;
}
边栏推荐
- Using fast parsing intranet penetration to realize zero cost self built website
- Why does infographic help your SEO
- In the enterprise, win10 turns on BitLocker to lock the disk, how to back up the system, how to recover when the system has problems, and how to recover quickly while taking into account system securi
- Using the uniapp rich text editor
- 基于三维gis平台的消防系统运用
- 【路径规划】RRT增加动力模型进行轨迹规划
- uniapp 除了数字,其他输入无效
- The pit of sizeof operator in C language
- How to avoid arc generation—— Aafd fault arc detector solves the problem for you
- Font design symbol combination multifunctional wechat applet source code
猜你喜欢

PMP certificate renewal process
![[IELTS reading] Wang Xiwei reads P4 (matching2 paragraph information matching question [difficult])](/img/83/63296108b47eda37c19b9ff9deb5ec.png)
[IELTS reading] Wang Xiwei reads P4 (matching2 paragraph information matching question [difficult])

「运维有小邓」域密码策略强化器

人脸识别5- insight-face-paddle-代码实战笔记

取得PMP證書需要多長時間?

A new method for analyzing the trend chart of London Silver

青海省国家湿地公园功能区划数数据、全国湿地沼泽分布数据、全国省市县自然保护区

Why does infographic help your SEO
![[IELTS reading] Wang Xiwei reading P3 (heading)](/img/19/40564f2afc18fe3e34f218b7b44681.png)
[IELTS reading] Wang Xiwei reading P3 (heading)

Tester's algorithm interview question - find mode
随机推荐
[path planning] RRT adds dynamic model for trajectory planning
PaddleOCR教程
用快解析内网穿透实现零成本自建网站
Design of emergency lighting evacuation indication system for urban rail transit station
圖解網絡:什麼是網關負載均衡協議GLBP?
JS how to realize array to tree
Continuous modification of business scenario functions
S32 design studio for arm 2.2 quick start
模板的进阶
「运维有小邓」域密码策略强化器
How to save your code works quickly to better protect your labor achievements
[Peking University] tensorflow2.0-1-opening
C language to quickly solve the reverse linked list
如何用快解析自制IoT云平台
取得PMP證書需要多長時間?
[论文阅读] TUN-Det: A Novel Network for Thyroid Ultrasound Nodule Detection
业务实现-日志写到同一个行数据里面
[ODX studio edit PDX] -0.3- how to delete / modify inherited elements in variant variants
Go step on the pit - no required module provides package: go mod file not found in current directory or any parent
Significance of acrel EMS integrated energy efficiency platform in campus construction