CF-1100E Andrew and Taxi

https://codeforces.com/contest/1100/problem/E

知识点:

  • 二分
  • 判断图中是否有环

题意: 一个有向图,每边有一条边权,对于一个值x,可以使得边权小于等于x的边反转,求最小的x,使得某些边反转之后图中没有环

分析:Suppose we have k traffic controllers. They can turn all edges whose weight is less than or equal to kk. Then let's remove all these edges from the graph, make a topological sorting of the remaining graph, and orient the other edges in the order of topological sorting. If there are cycles left in the graph after removing the edges, then we cannot get rid of them, having k traffic controllers. Otherwise, by adding edges we will not add new loops. The parameter k can be iterated through a binary search. Also in binary search, you can go through not all possible values of k, but only the values that are on the edges.

Complexity — O((n+m)logC)or O((n+m)logm

假如我们有k个交通管理员(即x),它们可以反转所有边权小于等于x的边,然后先把这些边从图中移除,拓扑排序这个图,如果这个图中还有环,那么我们应该使k变大。我们可以二分判定这个k,或者直接遍历所有边权即可。

下面方面使用dfs获得每个结点被访问的时间戳(不是dfs序列),因为这个题我们可以通过检测每个边的两端点的时间戳是否和原来方向相反来判断是否需要翻转。而一般的我们确实需要用拓扑排序来看是否有边没有遍历从而得到是否有环存在。(由于拓扑排序必须以入读为0的点开始)

#include <bits/stdc++.h>
using namespace std;
int n,m,a[100005],b[100005],c[100005],pos[100005],cur;
vector<int> v[100005],e;
//获得每个结点的时间戳
void dfs(int node)
{
pos[node]=1;
for(int u:v[node])
if(!pos[u])
dfs(u);
pos[node]=cur--;;
}
bool check(int x)
{
//清空处理
for(int i=1;i<=n;i++) v[i].clear();
for(int i=0;i<=n;i++)pos[i]=0;
e.clear();
//移除翻转的边
for(int i=0;i<m;i++)
if(c[i]>x)
v[a[i]].push_back(b[i]);
cur=m;
for (int i=1;i<=n;i++)
if(!pos[i])
dfs(i);
for(int i=0;i<m;i++)
{
//判断是否有环
if(pos[a[i]]>pos[b[i]])
{
if(c[i]>x) return false;
e.push_back(i+1);
}
}
return true;
}
int main()
{
ios_base::sync_with_stdio(false);
cin>>n>>m;
for(int i=0;i<m;i++)
cin>>a[i]>>b[i]>>c[i];
//二分答案
int st=0,en=1e9;
while(st!=en)
{
int mid=(st+en)/2;
if(check(mid)) en=mid;
else st=mid+1;
}
check(st);
cout<<st<<" "<<e.size()<<endl;
for(int i:e) cout<<i<<" ";
return 0;
}

CF-1100 E Andrew and Taxi的更多相关文章

  1. CF 1100E Andrew and Taxi(二分答案)

    E. Andrew and Taxi time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. CF1100E Andrew and Taxi

    题目地址:CF1100E Andrew and Taxi 二分,每次取到一个 \(mid\) ,只保留长度 \(>mid\) 的边 dfs判环,若有环,说明 \(ans>mid\) ,否则 ...

  3. E. Andrew and Taxi(二分+拓扑判环)

    题目链接:http://codeforces.com/contest/1100/problem/E 题目大意:给你n和m,n代表有n个城市,m代表有m条边,然后m行输入三个数,起点,终点,花费.,每一 ...

  4. Codeforces Round #532 (Div. 2) E. Andrew and Taxi(二分+拓扑排序)

    题目链接:https://codeforces.com/contest/1100/problem/E 题意:给出 n 个点 m 条边的有向图,要翻转一些边,使得有向图中不存在环,问翻转的边中最大权值最 ...

  5. Andrew and Taxi CodeForces - 1100E (思维,拓扑)

    大意: 给定有向图, 每条边有一个权值, 假设你有$x$个控制器, 那么可以将所有权值不超过$x$的边翻转, 求最少的控制器数, 使得翻转后图无环 先二分转为判定问题. 每次check删除能动的边, ...

  6. CF1100E Andrew and Taxi 二分答案+拓扑排序

    \(\color{#0066ff}{ 题目描述 }\) 给定一个有向图,改变其中某些边的方向,它将成为一个有向无环图. 现在求一个改变边方向的方案,使得所选边边权的最大值最小. \(\color{#0 ...

  7. E - Andrew and Taxi-二分答案-topo判环

    E - Andrew and Taxi 思路 :min max   明显二分答案,二分需要破坏的那些边的中机器人数量最多的那个. check 过程建边时直接忽略掉小于 mid 的边,这样去检验有无环存 ...

  8. Codeforces Round #532 (Div. 2) 题解

    Codeforces Round #532 (Div. 2) 题目总链接:https://codeforces.com/contest/1100 A. Roman and Browser 题意: 给出 ...

  9. Codeforces Round #532

    以后不放水题了 C.NN and the Optical Illusion 复习一下高中数学即可 $\frac{ans}{ans+r}=\sin \frac{\pi}{n}$ 解方程 #include ...

随机推荐

  1. [Xcode 实际操作]八、网络与多线程-(19)使用RunLoop使PerformSelector方法延迟动作的执行

    目录:[Swift]Xcode实际操作 本文将演示使用RunLoop使PerformSelector方法延迟动作的执行. 在项目导航区,打开视图控制器的代码文件[ViewController.swif ...

  2. C++中各种简写及全称的库

    ATL(Active TEmplate Library)活动模板库 RPC(Remote Procedure Call Protocol)远程过程调用协议 DCE(Distributed Comput ...

  3. Gitlab备份,Crontab定时备份

    1:Gitlab备份非常简单,只需要一条命令就可以创建完整的备份 gitlab-rake gitlab:backup:create 使用以上命令,就相当于在/var/opt/gitlab/backup ...

  4. VLAN-5-802.1Q-in-Q隧道

    Q-in-Q允许SP在跨越WAN服务时,保留802.1Q VLAN标签.由此,VLAN可以被拓展到多个地理分散的站点上.     入向SP交换机收到802.1Q数据帧,使用额外的802.1Q头部来标记 ...

  5. python模块之hmac

    # hmac模块使用步骤: # hmac模块模块的使用步骤与hashlib模块的使用步骤基本一致,只是在第1步获取hmac对象时,只能使用hmac.new()函数, # 因为hmac模块没有提供与具体 ...

  6. chrome浏览器历史版本

    持续更新中······ google-chrome 浏览器 win64位 版本号 大小 官网更新日期 66.0.3359.181 48.58 MB 2018年5月16日 65.0.3325.181 4 ...

  7. how browser handler file:/// link

    1. why browser can only open .txt file directly, pop up open or save dialog for others? 2. html cann ...

  8. Unity加载AssetBundle的方法

    using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; usin ...

  9. php __autoload函数 加载类文件

    面向对象的开发时,大家肯定都会遇到这样的问题,就是加载文件,一般都是加文件的头部inclue_once,require一大堆,看着很让人烦.当然你可以自己写程序来加载.php5以后引入了__autol ...

  10. docker命令自动安装

    docker命令自动安装 操作 操作就是执行两句脚本 curl -fsSL get.docker.com -o get-docker.sh 这句命令会在当前文件夹下下载一个get-docker.sh的 ...