Codeforces Round #532 (Div. 2) E. Andrew and Taxi(二分+拓扑排序)
题目链接: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(二分+拓扑排序)的更多相关文章
- 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 ...
- Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)
D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: = 的情况我们用并查集把他们扔到一个集合,然后根据 > ...
- 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 ...
- Codeforces Round #532 (Div. 2) 题解
Codeforces Round #532 (Div. 2) 题目总链接:https://codeforces.com/contest/1100 A. Roman and Browser 题意: 给出 ...
- Codeforces Round #532 (Div. 2)
Codeforces Round #532 (Div. 2) A - Roman and Browser #include<bits/stdc++.h> #include<iostr ...
- Codeforces Round #365 (Div. 2) C - Chris and Road 二分找切点
// Codeforces Round #365 (Div. 2) // C - Chris and Road 二分找切点 // 题意:给你一个凸边行,凸边行有个初始的速度往左走,人有最大速度,可以停 ...
- Codeforces Round #532 (Div. 2) Solution
A. Roman and Browser 签到. #include <bits/stdc++.h> using namespace std; ]; int get(int b) { ]; ...
- 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 ...
- Codeforces Round #532 (Div. 2) F 线性基(新坑) + 贪心 + 离线处理
https://codeforces.com/contest/1100/problem/F 题意 一个有n个数组c[],q次询问,每次询问一个区间的子集最大异或和 题解 单问区间子集最大异或和,线性基 ...
随机推荐
- redis 那些事儿
1 我的数据存入redis了但是怎么不见了? redis的内存使用是有限的,一直向redis中写入数据(如果配置了allkeyLRU)就会触发内存淘汰机制,将最近没有访问过的的key,value删除掉 ...
- [转帖]Apache Kylin 概述
Apache Kylin 概述 https://www.cnblogs.com/xiaodf/p/11671095.html 1 Kylin是什么 今天,随着移动互联网.物联网.AI等技术的快速兴起, ...
- nginx location 路由的几个配置纪要
1:网上没有查到在线测试 nginx location 规则的网址 在服务器上可以通过 return 返回测试比如 把#号去掉 # location /admin\.php(.*) # { #def ...
- 【flume】5.采集日志进入hbase
设置我们的flume配置信息 # Licensed to the Apache Software Foundation (ASF) under one # or more contributor li ...
- PB自动换行
1.在DataWindow Painter中打开DataWindow; 2.在需设定自动折行的列上单击, 查看右侧的属性窗口: 3.点击Position标签, 选中Autosize Height 多选 ...
- Docker 学习笔记(三):数据、网络、系统权限、docker-compose
一.Docker 数据管理 Docker 持久化数据有两种方式: 使用数据卷:更安全,和主机耦合度低 将主机的目录挂载到容器中:更方便,主机和容器可以很方便地交换数据. 数据卷相关的命令: docke ...
- Django配置websocket请求接口
1.settings.py INSTALLED_APPS = [ '...', 'channels', '...', ] ASGI_APPLICATION = 'server.routing.appl ...
- FICO-Payment Terms 收付款条件和分期付款设置
转载:https://www.cnblogs.com/weichuo/p/3524278.html Payment Terms 收付款条件和分期付款设置 SAP Payment Terms 中文翻译为 ...
- CI,CD理解
一.什么是CI,CD 当我们在谈论现代的软件编译和发布流程的时候,经常会听到CI 和CD这样的缩写短语.CI很容易理解,就是持续集成. 但是CD既可以指代码持续交付,也可理解为代码持续部署.C ...
- CentOS 7中调整默认开启终端数量
#vim /etc/securetty #然后将不需要使用到的进行注释掉就可以了 就这么简单,在强大的百度上找了半天愣是没找到