Codeforces Global Round 8 E. Ski Accidents(拓扑排序)
题目链接:https://codeforces.com/contest/1368/problem/E
题意
给出一个 $n$ 点 $m$ 边的有向图,每条边由编号较小的点通向编号较大的点,每个点的出度不大于 $2$,删掉一些点,使得图中不存在长度大于等于 $2$ 的路径。(最多删掉 $\frac{4}{7}n$ 个点)
题解
删除所有拓扑排序中深度为 $3$ 的倍数的顶点,由于每次删掉了一层,所以把所有点深度置为 $1$,只删除深度为 $3$ 的顶点即可。
证明
删除点占比最大的情况是该有向图为满二叉树且深度为 $3$ 的倍数,要删除的点即深度为 $3$ 的倍数每层结点,删除结点的个数与满二叉树结点的总个数之比为:
\begin{equation} \frac{2^2 + 2^5 + \dots + 2^{3n - 1}}{2^0 + 2^1 + 2^2 + \dots + 2^{3n - 1}} \end{equation}
分子分母等比数列求和得:
\begin{equation} \frac{ \frac{4(1 - 8^n)}{1 - 8} }{ \frac{(1 - 2^{3n})}{1 - 2} } \end{equation}
化简得:
\begin{equation} \frac{4}{7} \end{equation}
即最多删除 $\frac{4}{7}n$ 个结点。
代码
#include <bits/stdc++.h>
using namespace std; void solve() {
int n, m; cin >> n >> m;
vector<int> G[n + 1];
for (int i = 0; i < m; i++) {
int u, v; cin >> u >> v;
if (u == v) continue;
G[u].push_back(v);
}
vector<int> ans;
vector<int> dis(n + 1, 1);
for (int u = 1; u <= n; u++) {
if (dis[u] == 3) {
ans.push_back(u);
continue;
}
for (auto v : G[u])
dis[v] = max(dis[v], dis[u] + 1);
}
cout << ans.size() << "\n";
for (auto i : ans) cout << i << " \n"[i == ans.back()];
} int main() {
int t; cin >> t;
while (t--) solve();
}
Codeforces Global Round 8 E. Ski Accidents(拓扑排序)的更多相关文章
- CodeForces Global Round 1
CodeForces Global Round 1 CF新的比赛呢(虽然没啥区别)!这种报名的人多的比赛涨分是真的快.... 所以就写下题解吧. A. Parity 太简单了,随便模拟一下就完了. B ...
- Codeforces Global Round 1 - D. Jongmah(动态规划)
Problem Codeforces Global Round 1 - D. Jongmah Time Limit: 3000 mSec Problem Description Input Out ...
- Codeforces Global Round 2 题解
Codeforces Global Round 2 题目链接:https://codeforces.com/contest/1119 A. Ilya and a Colorful Walk 题意: 给 ...
- Codeforces Global Round 1 (A-E题解)
Codeforces Global Round 1 题目链接:https://codeforces.com/contest/1110 A. Parity 题意: 给出{ak},b,k,判断a1*b^( ...
- Codeforces Global Round 3
Codeforces Global Round 3 A. Another One Bites The Dust 有若干个a,有若干个b,有若干个ab.你现在要把这些串拼成一个串,使得任意两个相邻的位置 ...
- Codeforces Global Round 1 (CF1110) (未完结,只有 A-F)
Codeforces Global Round 1 (CF1110) 继续补题.因为看见同学打了这场,而且涨分还不错,所以觉得这套题目可能会比较有意思. 因为下午要开学了,所以恐怕暂时不能把这套题目补 ...
- 【手抖康复训练1 】Codeforces Global Round 6
[手抖康复训练1 ]Codeforces Global Round 6 总结:不想复习随意打的一场,比赛开始就是熟悉的N分钟进不去时间,2333,太久没写题的后果就是:A 题手抖过不了样例 B题秒出思 ...
- Codeforces Global Round 11 个人题解(B题)
Codeforces Global Round 11 1427A. Avoiding Zero 题目链接:click here 待补 1427B. Chess Cheater 题目链接:click h ...
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
随机推荐
- 【Mysql】[Err] 1153 - Got a packet bigger than 'max_allowed_packet' bytes
今天用Navicat导入的时候报错 [Err] 1153 - Got a packet bigger than 'max_allowed_packet' bytes 原因是数据库默认是16M的数据,这 ...
- 使用 gRPCurl 调试.NET 5的gPRC服务
介绍 你用过 Curl 吗?这个工具允许你通过 http 来发送数据,现在有一个适用于gGRPC的工具,gRPCurl,在本文中,我将介绍如何下载安装这个工具,然后通过这个工具调试我们.NET 5上面 ...
- library cache pin解决方法
library cache pin大部分都是因为编译存储过程造成的 查找造成问题的数据库对象(一般为存储过程) SELECT * FROM v$session_wait WHERE event = ' ...
- 天天用SpringBoot居然还不知道它的自动装配的原理?
引言 最近有个读者在面试,面试中被问到了这样一个问题"看你项目中用到了springboot,你说下springboot的自动配置是怎么实现的?"这应该是一个springboot里面 ...
- centos7 开放指定端口
centos7 开放指定端口 #开放8080端口 firewall-cmd --zone=public --add-port=8080/tcp --permanent #重载防火墙 firewall- ...
- Ubuntu20.04安装Typora
Ubuntu20.04安装Typora 安装方法 # optional, but recommended sudo apt-key adv --keyserver keyserver.ubuntu.c ...
- Python数据模型与Python对象模型
数据模型==对象模型 Python官方文档说法是"Python数据模型",大多数Python书籍作者说法是"Python对象模型",它们是一个意思,表示&quo ...
- Ansible自动化运维工具的使用
Ansible自动化运维工具的使用 host lnventory 管理主机 ip root账号密码 ssh端口 core mod ...
- CentOS7.9静默安装Oracle19C软件
CentOS7.9静默安装Oracle19C软件 Oracle发布了支持的版本.可以看到了Oracle11gR2和Oracle12C.一直到2022年就不支持patch和服务.(感慨Oracle 11 ...
- 打开APP 04 | 网络通信:RPC框架在网络通信上更倾向于哪种网络IO模型? 2020-02-26 何小锋
打开APP 04 | 网络通信:RPC框架在网络通信上更倾向于哪种网络IO模型? 2020-02-26 何小锋