当前位置:网站首页>武汉理工大学第三届程序设计竞赛 B-拯救DAG王国(拓扑性质处理可达性统计问题)
武汉理工大学第三届程序设计竞赛 B-拯救DAG王国(拓扑性质处理可达性统计问题)
2022-07-25 22:35:00 【阐上】
TP

题面没说一定是连通图,为此还去特判了下阴间数据
此类题可归类到 DAG的可达性统计。当数据小的时候比如 1 0 4 10^4 104 左右可以用 b i t s e t bitset bitset 压位运算,拓扑序 dp 统计;但此题的数据范围显然不允许这样做,所以就需要一些神仙结论了。
看注释吧,只能说神中神。
C o d e : Code: Code:
#include<bits/stdc++.h>
#include<unordered_map>
#define debug cout << "debug--- "
#define debug_ cout << "\n---debug---\n"
#define oper(a) operator<(const a& ee)const
#define forr(a,b,c) for(int a=b;a<=c;a++)
#define mem(a,b) memset(a,b,sizeof a)
#define cinios (ios::sync_with_stdio(false),cin.tie(0),cout.tie(0))
#define all(a) a.begin(),a.end()
#define sz(a) (int)a.size()
#define endl "\n"
#define ul (u << 1)
#define ur (u << 1 | 1)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 3e5 + 10, M = 2e6 + 10, mod = 1e9 + 7;
int INF = 0x3f3f3f3f; ll LNF = 0x3f3f3f3f3f3f3f3f;
int n, m, B = 10, ki;
struct edge
{
int a, b;
}ed[M];
vector<int> e[N];
int d[N], f[N];//f 记录任意点可到达的点数(但要符合一流城市定义)
void top_sort() {
int tot = n;//剩余城市数量
queue<int> q;
for (int i = 1; i <= n; i++)
if (d[i] == 0) {
tot--;//入队就减去
q.push(i);
}
while (q.size())
{
int t = q.front();
q.pop();
//根据拓扑序的性质,如果队列同层有多个点,这些点之间肯定不能抵达,如果能,则他们肯定不同层
//根据题意,当且仅当同层有两个点以内时:
if (q.size() <= 1) {
bool flag = true;
if (q.size()) {
for (int j : e[q.front()])
if (d[j] == 1)flag = false;
//需要特判这个额外点是否有单独的连接点,如果有则 t 点走不到他和他的点,不满足 B 类
//如果度数不为 1,显然 t 点也有连向那个点的边,不影响
}
if (flag)f[t] += tot;
//剩余城市都是由 t 拓扑推导出来的,肯定存在路径
}
for (int j : e[t])
if (--d[j] == 0) {
tot--;
q.push(j);
}
}
}
void solve() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
e[a].push_back(b);
d[b]++;
ed[i] = {
a,b };
}
top_sort();
forr(i, 1, n)e[i].clear(), d[i] = 0;
for (int i = 1; i <= m; i++) {
int a = ed[i].a, b = ed[i].b;
e[b].push_back(a);
d[a]++;
}
//反向也要跑一遍
top_sort();
int ans = 0;
for (int i = 1; i <= n; i++)
if (f[i] >= n - 2)ans++;
//除去自身和一个可以删去的点
cout << ans;
}
int main() {
cinios;
int T = 1;
for (int t = 1; t <= T; t++) {
solve();
}
return 0;
}
/* */
边栏推荐
- 【PMP学习笔记】第1章 PMP体系引论
- 3dslicer importing medical image data
- It's over. I went to work for three months and became bald
- 【集训DAY15】Boring【树形DP】
- Five constraints and three paradigms
- 数据质量:数据治理的核心
- IFLYTEK smart office book air e-book reader makes my work life healthier
- 三菱FX PLC自由口RS指令实现MODBUS通讯
- Learning orientation today
- [training Day11] Calc [mathematics]
猜你喜欢

3dslicer introduction and installation tutorial

分享两个音乐播放地址
![[C syntax] void*](/img/34/b29b7bbf8eae9f1730352cac1301a4.png)
[C syntax] void*

我们为什么要推出Getaverse?

力矩电机控制基本原理
![[training day15] paint road [minimum spanning tree]](/img/12/2d4ad1e2b8133b6c92875faa4b4182.png)
[training day15] paint road [minimum spanning tree]

xxl-job中 关于所有日志系统的源码的解读(一行一行源码解读)
![[training Day12] min ratio [DFS] [minimum spanning tree]](/img/f8/e37efd0d2aa0b3d79484b9ac42b7eb.png)
[training Day12] min ratio [DFS] [minimum spanning tree]

【集训DAY12】Bee GO!【动态规划】【数学】

Win10 set up a flutter environment to step on the pit diary
随机推荐
Xiaobai programmer's first day
Based on if nesting and function call
点亮字符串中所有需要点亮的位置,至少需要点几盏灯
[training day15] simple calculation [tree array] [mathematics]
【数据库学习】Redis 解析器&&单线程&&模型
[training Day12] min ratio [DFS] [minimum spanning tree]
Examples and points for attention about the use of getchar and scanf
英文术语对应的解释
Wkid in ArcGIS
[training day15] boring [tree DP]
IPv4地址已经完全耗尽,互联网还能正常运转,NAT是最大功臣!
【集训DAY13】Internet【并查集】
ThreadLocal summary (to be continued)
torchvision
3 词法分析
Can generic types be used in array
【集训DAY13】Backpack【动态规划】【贪心】
自媒体人必备的4个素材网站,再也不用担心找不到素材
Interpretation of English terms
Five constraints and three paradigms