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. linux 搭建unixODBC ,并对接 PostgreSQL 9.3.4

    环境:suse 11 ,64位的操作系统 unixODBC 版本:2.3.2 PostgreSQL 9.3.4 1 编译安装 unixODBC 下载 unixODBC :http://www.unix ...

  2. Windows服务使用Windsor容器

    该文章是系列文章 基于.NetCore和ABP框架如何让Windows服务执行Quartz定时作业 的其中一篇. Windsor是ABP框架自带的IOC容器. 关于什么是IOC,你可以Bing或者Go ...

  3. 如何利用python制作微信好友头像照片墙?

    这个不难,主要用到itchat和pillow这2个库,其中itchat用于获取微信好友头像照片,pillow用于拼接头像生成一个照片墙,下面我简单介绍一下实现过程,代码量不多,也很好理解,实验环境wi ...

  4. 解读ping -n 4 127.1 >nul 2>nul

    命令解读 ping是Windows.Unix和Linux系统下的一个命令.ping也属于一个通信协议,是TCP/IP协议的一部分.利用"ping"命令可以检查网络是否连通,可以很好 ...

  5. [筆記]catalan卡特蘭數

    前言:希望自己每個星期能發一篇文章,提升一下寫文章的能力?雖然對語文作文毫無幫助但是總比玩遊戲強 所以不務正業的東西就不放在首頁了,有興趣的可以點分類去看 來源:https://www.cnblogs ...

  6. mongodb的基本命令操作

    mongodb在已经存在管理员的情况下,需要创建一个库 使用管理员进入mongodb的命令行界面 mongo admin -u 管理员名 -p 管理员密码 创建库(进入库) use 库名 创建当前库的 ...

  7. MDX之Case When用法

    with member [Measures].[终端销售数量总计] as sum(ytd([日期].[年月].CurrentMember),[Measures].[终端销售数量]) member [M ...

  8. I/O————缓存流

    为什么要使用缓存流? 当对磁盘访问次数多的时候,字节流和字符流就会感觉性能不佳,速度较慢. 缓存流,一次会读取很多的数据到缓存中,以后每一次读取都是从缓存中读取,直到缓存中数据读取完,这样就减少了io ...

  9. ubuntu中wine下安装QQ

    原文:http://jingyan.baidu.com/article/359911f55da27057fe0306d8.html 可以把win改成最新版

  10. IE6常见CSS解析Bug和hack

    第一:图片间隙 a:div中的图片间隙: 描述:在div中插入图片时,图片会将div下方撑大3像素 hack1:将<div>和<img>写在一行 hack2:将<img& ...