题目地址: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的更多相关文章

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

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

  2. CF-1100 E Andrew and Taxi

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

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

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

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

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

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

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

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

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

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

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

  8. Codeforces Round #532

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

  9. Codeforces Round #532 (Div. 2) Solution

    A. Roman and Browser 签到. #include <bits/stdc++.h> using namespace std; ]; int get(int b) { ]; ...

随机推荐

  1. 根据指定的key,将二维数组的value转换为string,适用于mysql的in查询

    function array_unique_join($arr,$param){ $utm_source_arr = array_unique(array_column($arr,$param)); ...

  2. Event Recommendation Engine Challenge分步解析第一步

    一.简介 此项目来自kaggle:https://www.kaggle.com/c/event-recommendation-engine-challenge/ 数据集的下载需要账号,并且需要手机验证 ...

  3. LSTM时间序列预测及网络层搭建

    一.LSTM预测未来一年某航空公司的客运流量 给你一个数据集,只有一列数据,这是一个关于时间序列的数据,从这个时间序列中预测未来一年某航空公司的客运流量.数据形式: 二.实战 1)数据下载 你可以go ...

  4. 【.NET】using 语句中使用的类型必须可隐式转换为"System.IDisposable"

    #问题: 在使用EF开发中,出现如下错误:“using 语句中使用的类型必须可隐式转换为“System.IDisposable” #原因: 项目中没有引用 EntityFramework 这个程序集: ...

  5. eclipse设置是否自动跳转切换到debug视图模式

    之前一直用公司二次封装的eclipse,这几天用原生态的eclipse,刚开始使用eclipse进行调试时,会自动跳转到debug视图.后来不小心关闭了,就不会自动切换到debug视图. 这个小问题之 ...

  6. layui(九)——flow组件常见用法总结

    该模块包含 信息流加载 和  图片懒加载  两大核心支持,无论是对服务端.还是前端体验,都有非常大的性能帮助.下边分别给出了这两种技术的使用方法 一.信息流加载 信息流加载的核心方法时  flow.l ...

  7. springboot的lombok

    lombok概述 lombok简介 Lombok想要解决了的是在我们实体Bean中大量的Getter/Setter方法,以及toString, hashCode等可能不会用到,但是某些时候仍然需要复写 ...

  8. javascript&&jquery编写插件模板

    javascrpt插件编写模板 这里不分享如何编写插件,只留一个框架模板,使用面向对象的形式进行编写,方便管理 ;(function(window,document){ function FnName ...

  9. Fetch诞生记

    Fetch作用? https://developer.mozilla.org/zh-CN/docs/Web/API/Fetch_API/Using_Fetch Fetch API  提供了一个 Jav ...

  10. 大规模数据导入和导出(mysql)

    测试数据2.5G,共有数据9427567条.用的mysql的large服务器的配置.load 一次需要大概10分钟左右.建的表用的是MYISAM,调整了几个session的参数值 SET SESSIO ...