当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

Selected cutting-edge technical articles of Bi Ren Academy of science and technology

How to avoid arc generation—— Aafd fault arc detector solves the problem for you

C language to quickly solve the reverse linked list

Skills in analyzing the trend chart of London Silver

多回路仪表在基站“转改直”方面的应用

Jar batch management gadget

Pytoch --- use pytoch to realize linknet for semantic segmentation

Hash table, hash function, bloom filter, consistency hash

高配笔记本使用CAD搬砖时卡死解决记录

Fs8b711s14 electric wine bottle opener MCU IC scheme development special integrated IC
随机推荐
业务实现-日志写到同一个行数据里面
How to save your code works quickly to better protect your labor achievements
Is the account opening link of Huatai Securities with low commission safe?
Réseau graphique: Qu'est - ce que le Protocole d'équilibrage de charge de passerelle glbp?
公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!
C language to quickly solve the reverse linked list
【监控】zabbix
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
Specification for fs4061a boost 8.4v charging IC chip and fs4061b boost 12.6V charging IC chip datasheet
解决无法通过ssh服务远程连接虚拟机
Fast analysis -- easy to use intranet security software
Combien de temps faut - il pour obtenir un certificat PMP?
机器人强化学习——Learning Synergies between Pushing and Grasping with Self-supervised DRL (2018)
Chinese verification of JS regular expressions (turn)
Application of fire fighting system based on 3D GIS platform
【雅思阅读】王希伟阅读P3(Heading)
Basic points of the game setup of the points mall
How to reduce the stock account Commission and stock speculation commission? Is it safe to open an online account
如何用快解析自制IoT云平台
The input of uniapp is invalid except for numbers