\(\color{#0066ff}{ 题目描述 }\)

给定一个有向图,改变其中某些边的方向,它将成为一个有向无环图。

现在求一个改变边方向的方案,使得所选边边权的最大值最小。

\(\color{#0066ff}{输入格式}\)

点数n,边数m,接下来是m条有向边

\(\color{#0066ff}{输出格式}\)

输出一个最大值,一个k

接下来一行k个数,表示那些边需要反向

\(\color{#0066ff}{输入样例}\)

5 6
2 1 1
5 2 6
2 3 2
3 4 3
4 5 5
1 5 4 5 7
2 1 5
3 2 3
1 3 3
2 4 1
4 3 5
5 4 1
1 5 3

\(\color{#0066ff}{输出样例}\)

2 2
1 3 3 3
3 4 7

\(\color{#0066ff}{数据范围与提示}\)

\(2 \leq n \leq 100000\), \(1 \leq m \leq 100000\)

\(\color{#0066ff}{ 题解 }\)

根据题目,显然要二分答案

考虑二分答案之后怎么做

对于比mid大的边,我们肯定是不能改变方向的

于是直接加入图中

然后只需看看有没有环就行了,因为比mid小的边我们可以任意更改

可以用拓扑排序做

因为它只让最大值最小,并没有说改变边的数量最小,所以小的边随便改

现在考虑输出方案

我们在拓扑排序的时候记一下每个点的拓扑序

考虑一条边x到y,如果x的拓扑序大于y,显然可能成环(不是一定成环)

但是如果x的拓扑序小于y,一定不会成环

题目有不限制改边数量,我们就将其反向即可

#include<bits/stdc++.h>
#define LL long long
LL in() {
char ch; LL x = 0, f = 1;
while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
return x * f;
}
const int maxn = 1e5 + 10;
struct node {
int x, y, z, id;
friend bool operator < (const node &a, const node &b) {
return a.z < b.z;
}
}e[maxn];
struct E {
int to;
E *nxt;
E(int to = 0, E *nxt = NULL): to(to), nxt(nxt) {}
}pool[maxn], *tail;
int du[maxn], top[maxn];
bool vis[maxn];
int n, m;
E *head[maxn];
void add(int from, int to) {
head[from] = new E(to, head[from]);
} bool ok(int mid) {
std::queue<int> q;
int cnt = 0;
tail = pool;
for(int i = 1; i <= n; i++) du[i] = 0, head[i] = NULL, top[i] = 0;
for(int i = 1; i <= m; i++) vis[i] = false;
for(int i = m; i >= 1; i--) {
if(e[i].z <= mid) break;
add(e[i].x, e[i].y);
du[e[i].y]++;
}
for(int i = 1; i <= n; i++) if(!du[i]) q.push(i);
while(!q.empty()) {
int tp = q.front(); q.pop();
top[tp] = ++cnt;
for(E *i = head[tp]; i; i = i->nxt) {
du[i->to]--;
if(!du[i->to]) q.push(i->to);
}
}
if(cnt != n) return false;
for(int i = 1; i <= m; i++) {
if(e[i].z > mid) break;
if(top[e[i].x] > top[e[i].y]) vis[e[i].id] = true;
}
return true;
} int main() {
n = in(), m = in();
for(int i = 1; i <= m; i++) e[i].x = in(), e[i].y = in(), e[i].z = in(), e[i].id = i;
std::sort(e + 1, e + m + 1);
int l = 0, r = 1e9;
int ans = 0;
while(l <= r) {
int mid = (l + r) >> 1;
if(ok(mid)) ans = mid, r = mid - 1;
else l = mid + 1;
}
ok(ans);
int tot = 0;
for(int i = 1; i <= m; i++) if(vis[i]) tot++;
printf("%d %d\n", ans, tot);
for(int i = 1; i <= m; i++) if(vis[i]) printf("%d ", i);
return 0;
}

CF1100E Andrew and Taxi 二分答案+拓扑排序的更多相关文章

  1. CF1100E Andrew and Taxi

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

  2. bzoj5280/luogu4376 MilkingOrder (二分答案+拓扑序)

    二分答案建图,然后判环,就可以了. 字典序输出的话,只要做拓扑序的时候用优先队列来维护就可以了. (其实判环也可以用拓扑序...) #include<cstdio> #include< ...

  3. CF-1100 E Andrew and Taxi

    CF-1100E Andrew and Taxi https://codeforces.com/contest/1100/problem/E 知识点: 二分 判断图中是否有环 题意: 一个有向图,每边 ...

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

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

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

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

  6. CF #CROC 2016 - Elimination Round D. Robot Rapping Results Report 二分+拓扑排序

    题目链接:http://codeforces.com/contest/655/problem/D 大意是给若干对偏序,问最少需要前多少对关系,可以确定所有的大小关系. 解法是二分答案,利用拓扑排序看是 ...

  7. 【CF645D】 Robot Rapping Results Report(拓扑排序,二分)

    题意:有一张N点M边的有向图,求最小的K使根据前K条边就能够确定图是否有唯一的拓扑序, 若没有唯一拓扑序输出-1 思路:二分答案再拓扑排序,以入度为0的节点作为新的一层,若某一层的节点个数<&g ...

  8. CROC 2016 - Elimination Round (Rated Unofficial Edition) D. Robot Rapping Results Report 拓扑排序+二分

    题目链接: http://www.codeforces.com/contest/655/problem/D 题意: 题目是要求前k个场次就能确定唯一的拓扑序,求满足条件的最小k. 题解: 二分k的取值 ...

  9. codeforces 645 D. Robot Rapping Results Report 二分+拓扑排序

    题目链接 我们可以发现, 这是一个很明显的二分+拓扑排序.... 如何判断根据当前的点, 是否能构造出来一个唯一的拓扑序列呢. 如果有的点没有出现, 那么一定不满足. 如果在加进队列的时候, 同时加了 ...

随机推荐

  1. Mongodb 5节点异地两中心故障转移恢复测试案例

    Mongodb5节点异地两中心故障转移恢复测试案例 架构方式:5节点,主中心(2数据1仲裁),备中心(1数据1仲裁) 1基本情况 操作系统:Red Hat Enterprise Linux Serve ...

  2. webbrowser和js交互小结

    一.实现WebBrowser内部跳转,阻止默认打开IE 1.引用封装好的WebBrowserLinkSelf.dll实现 public partial class MainWindow : Windo ...

  3. Socket中常见的几个转换函数(htonl,htons,ntohl,ntohs,inet_addr,inet_ntoa)

    Socket中常见的几个转换函数(htonl,htons,ntohl,ntohs,inet_addr,inet_ntoa) htonl() htons() ntohl() ntohs()及inet_n ...

  4. 在ACCESS中LIKE的用法

    Access里like的通配符用法是这样:     “?”表示任何单一字符: “*”表示零个或多个字符: “#”表示任何一个数字     所以应该是:     select * from databa ...

  5. 转载 : JSP取得绝对路径

    转自:https://www.aliyun.com/jiaocheng/770177.html 转自:http://www.cnblogs.com/xdp-gacl/p/3707243.html 在J ...

  6. delphi 蓝牙 TBluetoothLE

    delphi 蓝牙 TBluetoothLE.TBluetoothLEManager BLE http://docwiki.embarcadero.com/RADStudio/Seattle/en/U ...

  7. xcode编写c/c++静态库使用系统头文件问题

    c/c++编写的静态库中有引用ios系统头文件比如: #include <EGL/egl.h> 在xcode编译的时候需要设置静态库程序: Build Settings-Header Se ...

  8. django之admin组件

    一.面向对象复习 1.类的继承 class Base(object): def __init__(self,val): self.val = val def func(self): self.test ...

  9. java线程游戏之背景图片的移动

    package com.plane; import java.awt.Graphics; import java.awt.Image; import javax.swing.ImageIcon; im ...

  10. C++面向对象类的实例题目九

    题目描述: 编写一个学生和老师数据输入和显示程序,学生数据有编号.姓名.班号和成绩,教师数据有编号.姓名.职称和部门. 要求将编号.姓名.输入和显示设计成一个类person,并作为学生数据操作类stu ...