CF1100E Andrew and Taxi
题目地址:CF1100E Andrew and Taxi
二分,每次取到一个 \(mid\) ,只保留长度 \(>mid\) 的边
dfs判环,若有环,说明 \(ans>mid\) ,否则 \(ans≤mid\)
找到 \(ans\) 后,对长度 \(>ans\) 的边进行一次拓扑排序,对多余的点直接接在拓扑排序序列后面即可
对从 \(x\) 到 \(y\) 的长度 \(≤ans\) 的边,如果 \(x\) 在拓扑排序序列中的位置比 \(y\) 后,则这条边需取反
时间复杂度 \(O(n\ log\ C)\)
#include <bits/stdc++.h>
using namespace std;
const int N = 100006;
int n, m, mx = 0, d[N], b[N], t;
int Head[N], Edge[N], Leng[N], Next[N], Pose[N];
vector<int> ans;
bool v[N], w[N];
queue<int> q;
inline void add(int x, int y, int z, int i) {
Edge[i] = y;
Leng[i] = z;
Next[i] = Head[x];
Head[x] = i;
Pose[i] = x;
}
bool dfs(int x, int now) {
v[x] = 1;
w[x] = 1;
for (int i = Head[x]; i; i = Next[i]) {
int y = Edge[i], z = Leng[i];
if (z <= now) continue;
if (w[y] || !dfs(y, now)) return 0;
}
w[x] = 0;
return 1;
}
inline bool pd(int now) {
memset(v, 0, sizeof(v));
memset(w, 0, sizeof(w));
for (int i = 1; i <= n; i++)
if (!v[i] && !dfs(i, now)) return 0;
return 1;
}
void topsort(int now) {
for (int i = 1; i <= n; i++)
if (!d[i]) q.push(i);
while (q.size()) {
int x = q.front();
q.pop();
b[x] = ++t;
for (int i = Head[x]; i; i = Next[i]) {
int y = Edge[i], z = Leng[i];
if (z > now && !--d[y]) q.push(y);
}
}
}
unsigned int work(int now) {
for (int i = 1; i <= m; i++) {
int y = Edge[i], z = Leng[i];
if (z > now) ++d[y];
}
topsort(now);
for (int i = 1; i <= n; i++)
if (!b[i]) b[i] = ++t;
for (int i = 1; i <= m; i++) {
int x = Pose[i], y = Edge[i], z = Leng[i];
if (z <= now && b[x] > b[y]) ans.push_back(i);
}
return ans.size();
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int x, y, z;
scanf("%d %d %d", &x, &y, &z);
add(x, y, z, i);
mx = max(mx, z);
}
int l = 0, r = mx;
while (l < r) {
int mid = (l + r) >> 1;
if (pd(mid)) r = mid;
else l = mid + 1;
}
cout << l << " " << work(l) << endl;
for (unsigned int i = 0; i < ans.size(); i++)
printf("%d ", ans[i]);
return 0;
}
CF1100E Andrew and Taxi的更多相关文章
- CF1100E Andrew and Taxi 二分答案+拓扑排序
\(\color{#0066ff}{ 题目描述 }\) 给定一个有向图,改变其中某些边的方向,它将成为一个有向无环图. 现在求一个改变边方向的方案,使得所选边边权的最大值最小. \(\color{#0 ...
- CF-1100 E Andrew and Taxi
CF-1100E Andrew and Taxi https://codeforces.com/contest/1100/problem/E 知识点: 二分 判断图中是否有环 题意: 一个有向图,每边 ...
- CF 1100E Andrew and Taxi(二分答案)
E. Andrew and Taxi time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- E. Andrew and Taxi(二分+拓扑判环)
题目链接:http://codeforces.com/contest/1100/problem/E 题目大意:给你n和m,n代表有n个城市,m代表有m条边,然后m行输入三个数,起点,终点,花费.,每一 ...
- Andrew and Taxi CodeForces - 1100E (思维,拓扑)
大意: 给定有向图, 每条边有一个权值, 假设你有$x$个控制器, 那么可以将所有权值不超过$x$的边翻转, 求最少的控制器数, 使得翻转后图无环 先二分转为判定问题. 每次check删除能动的边, ...
- Codeforces Round #532 (Div. 2) E. Andrew and Taxi(二分+拓扑排序)
题目链接:https://codeforces.com/contest/1100/problem/E 题意:给出 n 个点 m 条边的有向图,要翻转一些边,使得有向图中不存在环,问翻转的边中最大权值最 ...
- E - Andrew and Taxi-二分答案-topo判环
E - Andrew and Taxi 思路 :min max 明显二分答案,二分需要破坏的那些边的中机器人数量最多的那个. check 过程建边时直接忽略掉小于 mid 的边,这样去检验有无环存 ...
- Codeforces Round #532
以后不放水题了 C.NN and the Optical Illusion 复习一下高中数学即可 $\frac{ans}{ans+r}=\sin \frac{\pi}{n}$ 解方程 #include ...
- Codeforces Round #532 (Div. 2) Solution
A. Roman and Browser 签到. #include <bits/stdc++.h> using namespace std; ]; int get(int b) { ]; ...
随机推荐
- C#语法糖(Csharp Syntactic sugar)
目录 一.C#语法糖大汇总 1. 经过简化的Property2. 经过两次变异的委托写法3. 集合类的声明4. 集合类各个项的操作5. using == try finally6. 可爱的var7. ...
- centos7下安装pip以及mysql等软件
1.安装pip 安装失败了的提示: No package pip available.Error: Nothing to do 解决方法: 需要先安装扩展源EPEL. EPEL(http://fedo ...
- Linux基础入门教程
Linux基础入门教程 --------- Linux学习路径 Linux学习者,常常不知道自己改怎么学习linux:Linux初级,也就是入门linux前提是需要有一些计算机硬件相关的知识或是有一下 ...
- How-to: Do Statistical Analysis with Impala and R
sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&a ...
- MySQL内存占用计算
##MySQL 最大可使用内存( M ): SELECT ( @@key_buffer_size + @@innodb_buffer_pool_size + @@query_cache_size + ...
- 人工神经网络入门(4) —— AFORGE.NET简介
范例程序下载:http://files.cnblogs.com/gpcuster/ANN3.rar如果您有疑问,可以先参考 FAQ 如果您未找到满意的答案,可以在下面留言:) 0 目录人工神经网络入门 ...
- vue-router路由
Vue Router 是 Vue.js 官方的路由管理器 自动全局安装: vue create 项目名称 手动配置: 1.安装 在app项目文件夹里面 npm i vue-router 2.在min. ...
- mosh
mosh 是一款使用 UDP 连接 C/S 的终端工具, 服务器只需安装好 mosh 套件, 并启动 SSH 服务, 等待 Client 连接即可. Client (mosh-client) 连接时, ...
- 50个最常用的Linux命令
转载至:http://gywbd.github.io/posts/2014/8/50-linux-commands.html tar grep find ssh sed awk vim diff so ...
- build script和all projects作用和区别
buildscript中的声明是gradle脚本自身需要使用的资源.可以声明的资源包括依赖项.第三方插件.maven仓库地址等.而在build.gradle文件中直接声明的依赖项.仓库地址等信息是项目 ...