D

Problem - D - Codeforces

题意:

  给定一个有向图,每个点有自己的点权,求一条长度为K的路径使得路径上的最大点权最小,输出该条路径上的最大点权。

思路:(二分+拓扑排序)

  最小值最大的题考虑二分解决。

  我们每次二分答案为mx,以此判断有没有某条路径的最大值为mx且长度为k。因为我们要保证二分出来的mx是该条路径上最大的点权,所以所有大于mx的点我们都要舍弃。

  其次如果路径中含环那必然是可以的,如果不含吗,就需要判断是否存在长度为k,最大值为mx的路径

  举个例子:

    最初的图       

  当k == 4时,当我们判断mid = 4(既mx=4)时,则存在这样一条路径

                 

    chick返回true

  判断环的时候可以通过拓扑排序来判断

  在chick的时候需要注意的是,因为我们每次取的是小于等于mx的点,所以每次每个点的入度都不同,都需要重新统计

 1 const int N = 2e5 + 10,INF = 0x3f3f3f3f3f;
2 int n, m, k, w[N];
3 vector<int> g[N];
4 int vis[N], deg[N], deep[N];/*vis判断是否小于等于mx,deg统计入度,deep统计路径长度*/
5 vector<pair<int, int> >e;
6
7 bool chick(int mx) {
8 for (int i = 1; i <= n; ++i) vis[i] = 0, deg[i] = 0, deep[i] = -INF;
9 for (int i = 1; i <= n; ++i) {
10 if (w[i] <= mx)vis[i] = 1;
11 }
12 for (auto it : e) {
13 if (w[it.first] <= mx && w[it.second] <= mx) {
14 deg[it.second]++;
15 }
16 }
17 queue<int> q;
18 for (int i = 1; i <= n; ++i ) {
19 if (vis[i] && !deg[i])q.push(i),deep[i] = 1;
20 }
21 while (q.size()) {
22 int u = q.front();
23 q.pop();
24 for (int s : g[u]) {
25 if (!vis[s]) continue;
26 deep[s] = max(deep[s], deep[u] + 1);
27 if (deep[s] >= k) return 1;
28 deg[s]--;
29 if (deg[s] == 0) q.push(s);
30 }
31 }
32 for (int i = 1; i <= n; ++i) {
33 if (vis[i] && deg[i] > 0)return 1;
34
35 }
36 return 0;
37 }
38
39 void solve() {
40 cin >> n >> m >> k;
41 for (int i = 1; i <= n; ++i) cin >> w[i];
42 for (int i = 1; i <= m; ++i) {
43 int x, y;
44 cin >> x >> y;
45 g[x].push_back(y);
46 e.push_back({x, y});
47 }
48 if (k == 1) {
49 cout << *max_element(w + 1, w + 1 + n) << endl;
50 return;
51 }
52 int ans = INF;
53 int l = 0, r = 1e9 + 1;
54 for (int i = 1; i <= 50; ++i) {
55 int mid = (l + r) >> 1;
56 if (chick(mid)) {
57 ans = min(ans, mid);
58 r = mid - 1;
59 } else l = mid + 1;
60 }
61 if (ans == INF) {
62 cout << -1 << endl;
63 } else cout << ans << endl;
64 }

Codeforces Round #791(Div 2)——D的更多相关文章

  1. Codeforces Round #791 (Div. 2) A-C

    Codeforces Round #791 (Div. 2) A-C A 题目 https://codeforces.com/contest/1679/problem/A 题解 思路 知识点:数学,暴 ...

  2. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  3. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  4. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  5. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  6. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  7. Codeforces Round #262 (Div. 2) 1003

    Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...

  8. Codeforces Round #262 (Div. 2) 1004

    Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...

  9. Codeforces Round #371 (Div. 1)

    A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...

随机推荐

  1. JZOJ 5796 划分 (容斥,数论,扩展CRT)

    题面 有一个未知的序列 x,长度为 n.它的 K-划分序列 y 指的是每连续 K 个数的和得到划 分序列,y[1]=x[1]+x[2]+....+x[K],y[2]=x[K+1]+x[K+2]+... ...

  2. 使用Python批量爬取美女图片

    运行截图 实列代码: from bs4 import BeautifulSoup import requests,re,os headers = { 'User-Agent': 'Mozilla/5. ...

  3. Spring 10: AspectJ框架 + @Before前置通知

    AspectJ框架 概述 AspectJ是一个优秀的面向切面编程的框架,他扩展了java语言,提供了强大的切面实现 本身是java语言开发的,可以对java语言面向切面编程进行无缝扩展 AOP常见术语 ...

  4. 【MySQL】从入门到精通9-数据库的备份(完结)

    上期:[MySQL]从入门到精通8-SQL数据库编程 第一章:数据的导出 回到我们的Workbench. 选择Data Export. 选择需要导出的数据库. 注意,如果选择"Export ...

  5. clipboard实现文本复制的方法

    1.下载地址: https://github.com/mo3408/clipboard 2.使用方法: 先引入js: <script src="dist/clipboard.min.j ...

  6. VSCODE 配置远程调试环境

    以下内容为本人的著作,如需要转载,请声明原文链接微信公众号「englyf」https://www.cnblogs.com/englyf/p/16691460.html 我的需求是,在Windows桌面 ...

  7. Nessus-8.11.1-x64.msi安装包

    希望能给那些和我一样迷茫受挫的小伙伴们一些帮助,这玩意儿下载挺慢的,我把安装包分享出来,如果有博客园账号的,点个赞呗,CSDN那些用着别人的软件还要积分,呸! 08-18更新,截止到现在,已更新到最新 ...

  8. css百叶窗

    效果图: css代码块: <style> *{//默认样式清除 margin: 0; padding: 0; } .content{//设置外层div的宽高,超出后隐藏 margin: 1 ...

  9. day10-习题

    习题 1.Homework01 (1) D -- 没有在别名上加引号(ps:别名的as可以省略) (2) B -- 判断null或非空不能用不等于号 (3) C 2.Homework02 写出查看de ...

  10. SpringBoot 自定义注解 实现多数据源

    SpringBoot自定义注解实现多数据源 前置学习 需要了解 注解.Aop.SpringBoot整合Mybatis的使用. 数据准备 基础项目代码:https://gitee.com/J_look/ ...