当前位置:网站首页>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;
}
边栏推荐
- QT addition calculator (simple case)
- Design of emergency lighting evacuation indication system for urban rail transit station
- Fast analysis -- easy to use intranet security software
- 如何在外地外网电脑远程公司项目?
- French scholars: the explicability of counter attack under optimal transmission theory
- 取得PMP證書需要多長時間?
- How to use fast parsing to make IOT cloud platform
- The pit of sizeof operator in C language
- PMP certificate renewal process
- [IELTS reading] Wang Xiwei reads P4 (matching2 paragraph information matching question [difficult])
猜你喜欢

QT addition calculator (simple case)

OSEK standard ISO_ 17356 summary introduction

Face recognition 5- insight face padding code practice notes

如何在外地外网电脑远程公司项目?

Microservice

【雅思阅读】王希伟阅读P4(matching1)

同事的接口文档我每次看着就头大,毛病多多。。。

【kotlin】第三天
![[论文阅读] TUN-Det: A Novel Network for Thyroid Ultrasound Nodule Detection](/img/25/e2366cabf00e55664d16455a6049e0.png)
[论文阅读] TUN-Det: A Novel Network for Thyroid Ultrasound Nodule Detection

French scholars: the explicability of counter attack under optimal transmission theory
随机推荐
Summary of week 22-07-02
C language to quickly solve the reverse linked list
JS convert pseudo array to array
实战模拟│JWT 登录认证
[paper reading] Tun det: a novel network for meridian ultra sound nodule detection
打新债开户注册安全吗?有没有风险的?靠谱吗?
Remember to build wheels repeatedly at one time (the setting instructions of obsidian plug-in are translated into Chinese)
Font design symbol combination multifunctional wechat applet source code
Face recognition 5- insight face padding code practice notes
[path planning] RRT adds dynamic model for trajectory planning
Illustrated network: what is gateway load balancing protocol GLBP?
Cross domain request
Skills in analyzing the trend chart of London Silver
挖财学院开户安全的吗?开户怎么开?
Financial markets, asset management and investment funds
Hong Kong Jewelry tycoon, 2.2 billion "bargain hunting" Giordano
QT addition calculator (simple case)
[IELTS reading] Wang Xiwei reading P4 (matching1)
[论文阅读] CarveMix: A Simple Data Augmentation Method for Brain Lesion Segmentation
Life is changeable, and the large intestine covers the small intestine. This time, I can really go home to see my daughter-in-law...