题目地址: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. Collection中的迭代器

    迭代器:boolean hasNext() 判断集合中是否还有没有被取出数据nexe() 取出集合中下一个元素package cn.lijun.demo4; import java.util.Arra ...

  2. Linux系统IO分析工具之iotstat常用参数介绍

    Linux系统IO分析工具之iotstat常用参数介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 1>.安装iostat [root@flume115 ~]# yum - ...

  3. nginx跨域的简单应用

    nginx跨域的简单应用 要求:1.浏览器访问print.qianbaihe.wang/zt 直接调转至 www.flybirdprint.com/zt,浏览器显示域名不变. server { lis ...

  4. 16.观察者模式(Observer Pattern)

    动机(Motivate):     在软件构建 过程中,我们需要为某些对象建立一种“通知依赖关系” --------一个对象(目标对象)的状态发生改变,所有的依赖对象(观察者对象)都将得到通知.如果这 ...

  5. Hbase记录-备份与恢复方案推荐

    热备份和冷备份参考方案,如在生产环境,请结合业务情况考虑

  6. 【leetcode70】【动态规划】 爬楼梯

    (1 pass 一维动态规划) 爬楼梯(easy) 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数 ...

  7. 虚拟机 the image's hash and certificate are not allowed 解决方案

    根据计划,需要在虚拟机上安装一个linux系统,用作web架构学习的服务器. 公司项目的服务器用的是linux系统,具体版本未知.虽然我们开发不用关注最后的部署,但多少也接触了一些,算是有一定的了解, ...

  8. SQL的六种约束

    https://blog.csdn.net/z120270662/article/details/79501621

  9. 使用java poi解析表格

    @Test public void poi() throws Exception { InputStream inputStream=new FileInputStream("C:\\Use ...

  10. C# WinForm开发系列 - Crystal Report水晶报表

    转自:ttp://www.cnblogs.com/peterzb/archive/2009/07/11/1521325.html 水晶报表(Crystal Report)是业内最专业.功能最强的报表系 ...