题目地址: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. Session兑现的一级缓存

    快照机制:

  2. Java案例整理

    1.随机点名器案例 1.1      案例介绍 随机点名器,即在全班同学中随机的找出一名同学,打印这名同学的个人信息. 此案例在我们昨天课程学习中,已经介绍,现在我们要做的是对原有的案例进行升级,使用 ...

  3. Maven Tomcat7+ 实现自动化部署

    首先在Tomcat里配置deploy的用户(tomcat根目录/conf/tomcat-users.xml): <role rolename="tomcat"/> &l ...

  4. [DUBBO] Unexpected error occur at send statistic, cause: Forbid consumer 192.168.3.151 access servic

    [DUBBO] Unexpected error occur at send statistic, cause: Forbid consumer 192.168.3.151 access servic ...

  5. 2017-12-15python全栈9期第二天第五节之格式化输出补充之想要在格式化输出中表示单纯的%号就加%

    #!/user/bin/python# -*- coding:utf-8 -*-name = input('姓名:')age = input('年龄:')height = input('身高:')ms ...

  6. Rancher之Pipeline JAVA demo

    Rancher Pipeline Pipeline,简单来说,就是一套运行于Rancher上的工作流框架,将原本独立运行于单个或者多个节点的任务连接起来,实现单个任务难以完成的复杂发布流程. Ranc ...

  7. wordcloud制作logo

    准备工作: 1.txt文本(ASCII) 2.参照图(色差大或自行调整扫描参数) 3.pycharm安装wordcloud 源码: from os import path from PIL impor ...

  8. bzoj千题计划321:bzoj5251: [2018多省省队联测]劈配(网络流 + 二分)

    https://www.lydsy.com/JudgeOnline/problem.php?id=5251 第一问: 左边一列点代表学生,右边一列点代表导师 导师向汇点连流量为 人数限制的 边 然后从 ...

  9. vue使用vue-awesome-swiper及一些问题

    vue-awesome-swiper是基于swiper的一个轮播图插件,使用非常方便. 首先安装下 npm install vue-awesome-swiper --save 然后在入口文件main. ...

  10. HTTP Method小记

    HTTP 0.9 这个版本只有GET方法 HTTP 1.0 这个版本有GET HEAD POST这3个方法 HTTP 1.1 这个版本是当前版本,包含GET HEAD POST OPTIONS PUT ...