题目链接:https://codeforces.com/contest/1100/problem/E

题意:给出 n 个点 m 条边的有向图,要翻转一些边,使得有向图中不存在环,问翻转的边中最大权值最小是多少。

题解:首先要二分权值,假设当前最大权值是 w,那么大于 w 的边一定不能翻转,所以大于 w 所组成的有向图不能有环,而剩下小于等于 w 的边一定可以构造出一个没有环的图(因为如果一条边正反插入都形成环的话,那么图里之前已经有环了),构造方法是把大于 w 的边跑拓扑序,然后小于等于 w 的边序号小的连向序号大的。

 #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define mst(a,b) memset((a),(b),sizeof(a))
#define mp(a,b) make_pair(a,b)
#define pi acos(-1)
#define pii pair<int,int>
#define pb push_back
#define lowbit(x) ((x)&(-x))
const int INF = 0x3f3f3f3f;
const double eps = 1e-;
const int maxn = 1e5 + ;
const int maxm = 1e6 + ;
const ll mod = 1e9 + ; int n,m; struct edge {
int from,to,w;
}e[maxn]; vector<int>vec[maxn];
int vis[maxn];
bool flag; void dfs(int u,int mid) {
vis[u] = ;
for(int i = ; i < vec[u].size(); i++) {
int x = vec[u][i];
if(e[x].w <= mid) continue;
if(vis[e[x].to] == ) {
flag = false;
continue;
} else if(vis[e[x].to] == ) continue;
dfs(e[x].to,mid);
}
vis[u] = ;
} bool check(int mid) {
mst(vis, );
flag = true;
for(int i = ; i <= n; i++) {
if(vis[i] == ) dfs(i,mid);
}
return flag;
} vector<int>out;
int top[maxn], du[maxn]; int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
scanf("%d%d",&n,&m);
for(int i = ; i <= m; i++) {
scanf("%d%d%d",&e[i].from,&e[i].to,&e[i].w);
vec[e[i].from].push_back(i);
}
int l = , r = 1e9, ans = ;
while(l <= r) {
int mid = (l + r) >> ;
if(check(mid)) ans = mid, r = mid - ;
else l = mid + ;
}
for(int i = ; i <= m; i++)
if(e[i].w > ans) du[e[i].to]++;
queue<int>q;
int tot = ;
for(int i = ; i <= n; i++)
if(du[i] == ) q.push(i);
while(!q.empty()) {
int u = q.front();
q.pop();
top[u] = ++tot;
for(int i = ; i < vec[u].size(); i++) {
int v = vec[u][i];
if(e[v].w <= ans) continue;
du[e[v].to]--;
if(du[e[v].to] == ) q.push(e[v].to);
}
}
for(int i = ; i <= m; i++)
if(e[i].w <= ans && top[e[i].from] > top[e[i].to]) out.push_back(i);
printf("%d %d\n",ans,out.size());
for(int i = ; i < out.size(); i++) printf("%d ",out[i]);
return ;
}

Codeforces Round #532 (Div. 2) E. Andrew and Taxi(二分+拓扑排序)的更多相关文章

  1. Codeforces Round #285 (Div. 2) A B C 模拟 stl 拓扑排序

    A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  2. Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)

    D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: =  的情况我们用并查集把他们扔到一个集合,然后根据 > ...

  3. Codeforces Round #285 (Div. 2)C. Misha and Forest(拓扑排序)

    传送门 Description Let's define a forest as a non-directed acyclic graph (also without loops and parall ...

  4. Codeforces Round #532 (Div. 2) 题解

    Codeforces Round #532 (Div. 2) 题目总链接:https://codeforces.com/contest/1100 A. Roman and Browser 题意: 给出 ...

  5. Codeforces Round #532 (Div. 2)

    Codeforces Round #532 (Div. 2) A - Roman and Browser #include<bits/stdc++.h> #include<iostr ...

  6. Codeforces Round #365 (Div. 2) C - Chris and Road 二分找切点

    // Codeforces Round #365 (Div. 2) // C - Chris and Road 二分找切点 // 题意:给你一个凸边行,凸边行有个初始的速度往左走,人有最大速度,可以停 ...

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

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

  8. Codeforces Round #285 (Div. 2) A, B , C 水, map ,拓扑

    A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  9. Codeforces Round #532 (Div. 2) F 线性基(新坑) + 贪心 + 离线处理

    https://codeforces.com/contest/1100/problem/F 题意 一个有n个数组c[],q次询问,每次询问一个区间的子集最大异或和 题解 单问区间子集最大异或和,线性基 ...

随机推荐

  1. Mysql之rpm安装5.7版本遇见的问题

    前言:环境是centos7.5的系统,用rpm方式安装mysql5.7 1.由于是centos7.5 所以需要将默认的mariadb给卸载 rpm -qa | grep mariadb 查看下是否有m ...

  2. Scala 面向对象编程之Trait

    将trait作为接口使用 // Scala中的Triat是一种特殊的概念 // 首先我们可以将Trait作为接口来使用,此时的Triat就与Java中的接口非常类似 // 在triat中可以定义抽象方 ...

  3. 解决batik使用JScrollPane显示svg图滚动条不显示的问题

    // 必须使用batik提供的JSVGScrollPane,使用swing自己的组件JScrollPane初始化时滚动条不会显示. JSVGScrollPane svgJScrollPane = ne ...

  4. Synergy 一套键鼠同时控制多台电脑

    百度云(1.5是最后一个免费的版本,linux,mac,windows全有): 链接: https://pan.baidu.com/s/1A_qR8FHd8a0ILQXsSqjfCQ 密码: 9h8c

  5. Python之(scikit-learn)机器学习

    一.机器学习(Machine Learning, ML)是一门多领域交叉学科,涉及概率论.统计学.逼近论.凸分析.算法复杂度理论等多门学科.专门研究计算机怎样模拟或实现人类的学习行为,以获取新的知识或 ...

  6. ASP.NET Core中防跨站点请求伪造

    CSRF(Cross-site request forgery)利用了web中用户身份验证的一个漏洞:简单的身份验证只能保证请求发自某个用户的浏览器,却不能保证请求本身是用户自愿发出的. 例子 在某个 ...

  7. pfSense QoS IDS

    pfSense QoS IDS 来源 https://blanboom.org/2018/pfsense-setup/ 之前我使用的无线路由器是 RT1900ac,其内置了 QoS 和 IDS/IPS ...

  8. JavaScript中对null和undefined的理解

    前沿: 今天工作中遇到了监视一个变量是undefined,结果判断写的是==null 返回值是true,这个结果引起了我对这两个东西的兴趣. 查询了相关的文章理解并测试了.发现有以下特点: 1.广义上 ...

  9. 使用angularJS设置复选框的回显状态

    思路分析: 在angularJS中,我们可以使用ng-checked="expression()"来设置复选框的状态:当expression()返回true时,该复选框为选择中状态 ...

  10. html5细线表格制作

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...