当前位置:网站首页>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;
}
边栏推荐
- It's too convenient. You can complete the code release and approval by nailing it!
- 【北京大学】Tensorflow2.0-1-开篇
- Is it safe to open an account in the College of Finance and economics? How to open an account?
- Solve the problem that the virtual machine cannot be remotely connected through SSH service
- Advanced template
- Netcore3.1 JSON web token Middleware
- Hong Kong Jewelry tycoon, 2.2 billion "bargain hunting" Giordano
- 端口映射和端口转发区别是什么
- Build your own minecraft server with fast parsing
- How long does it take to obtain a PMP certificate?
猜你喜欢
如何在外地外网电脑远程公司项目?
企业公司项目开发好一部分基础功能,重要的事保存到线上第一a
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
"Xiaodeng" domain password policy enhancer in operation and maintenance
How to effectively monitor the DC column head cabinet
香港珠宝大亨,22亿“抄底”佐丹奴
Significance of acrel EMS integrated energy efficiency platform in campus construction
Acrel-EMS综合能效平台在校园建设的意义
It's too convenient. You can complete the code release and approval by nailing it!
[paper reading] cavemix: a simple data augmentation method for brain vision segmentation
随机推荐
js正则表达式之中文验证(转)
Enterprise application business scenarios, function addition and modification of C source code
Summary of week 22-07-02
What is the difference between port mapping and port forwarding
快解析内网穿透帮助企业快速实现协同办公
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
Hologres Query管理及超时处理
[ODX studio edit PDX] -0.3- how to delete / modify inherited elements in variant variants
Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...
Go step on the pit - no required module provides package: go mod file not found in current directory or any parent
Expand your kubecl function
Why does infographic help your SEO
Parsing of XML
Continuous modification of business scenario functions
Hash table, hash function, bloom filter, consistency hash
人脸识别5- insight-face-paddle-代码实战笔记
【雅思阅读】王希伟阅读P3(Heading)
华泰证券低佣金的开户链接安全吗?
基于三维gis平台的消防系统运用
取得PMP证书需要多长时间?