当前位置:网站首页>Acwing164. Accessibility Statistics (topological sorting +bitset)
Acwing164. Accessibility Statistics (topological sorting +bitset)
2022-07-05 00:05:00 【eva_ can(not)survive】
164. Accessibility Statistics - AcWing Question bank High quality algorithm question bank https://www.acwing.com/problem/content/166/ For the first time bitset What a magical container , It is very convenient for us to calculate subsets .
This question also directly shows that there is a DAG So we can find the topological sequence directly , Then traverse in reverse order and record the states that can be reached at each point .
#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;
}
边栏推荐
- 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
- js如何实现数组转树
- P4408 [NOI2003] 逃学的小孩(树的直径)
- [Peking University] tensorflow2.0-1-opening
- 用快解析内网穿透实现零成本自建网站
- Is the account opening link of Huatai Securities with low commission safe?
- It's too convenient. You can complete the code release and approval by nailing it!
- 人生无常,大肠包小肠, 这次真的可以回家看媳妇去了。。。
- Cross domain request
- 巩固表达式C# 案例简单变量运算
猜你喜欢
Power operation and maintenance cloud platform: open the new mode of "unattended and few people on duty" of power system
多回路仪表在基站“转改直”方面的应用
图解网络:什么是网关负载均衡协议GLBP?
How many triangles are there in the golden K-line diagram?
A new method for analyzing the trend chart of London Silver
Design of emergency lighting evacuation indication system for urban rail transit station
abc 258 G - Triangle(bitset)
城市轨道交通站应急照明疏散指示系统设计
Face recognition 5- insight face padding code practice notes
IELTS examination process, what to pay attention to and how to review?
随机推荐
认识ThreadPoolExecutor
Best practice case of enterprise digital transformation: introduction and reference of cloud based digital platform system security measures
PermissionError: [Errno 13] Permission denied: ‘data. csv‘
"Xiaodeng" domain password policy enhancer in operation and maintenance
[论文阅读] CarveMix: A Simple Data Augmentation Method for Brain Lesion Segmentation
uniapp上传头像
[monitoring] ZABBIX
Fast analysis -- easy to use intranet security software
Financial markets, asset management and investment funds
Date time type and format in MySQL
[paper reading] Tun det: a novel network for meridian ultra sound nodule detection
Robot reinforcement learning synergies between pushing and grassing with self supervised DRL (2018)
Five papers recommended for the new development of convolutional neural network in deep learning
Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...
22-07-02周总结
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
业务场景功能的继续修改
JS convert pseudo array to array
Summer challenge brings you to play harmoniyos multi terminal piano performance
微服务(Microservice)那点事儿