题目链接: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. go零碎总结

    1.go里通过首字母大小写来区分它是私有的还是公有的,比如对于一个结构体属性一般就以大写开头(和Java不一样,不需要什么getter,setter方法):而对于方法而言,它是隶属于包(包名一定是小写 ...

  2. 哈希--Hash,“散列”/“哈希”

    哈希 Hash,翻译“散列”,音译为“哈希”,把任意长度的输入,通过散列算法,变换成固定长度的输出,该输出就是散列值.这种转换是一种压缩映射,也就是散列值的空间通常远小于输入的空间,不同的输入可能会散 ...

  3. [转帖]AMD:Zen 2霄龙处理器每美元性能可达英特尔至强5.6倍

    AMD:Zen 2霄龙处理器每美元性能可达英特尔至强5.6倍 2019-10-20 6:35:38来源:IT之家作者:孤城责编:孤城评论:32 https://www.ithome.com/0/451 ...

  4. nginx tar包安装 包含openssl,rewrite,stream,sticky 等模块

    最近需要使用nginx 但是发现有时缺少一些模块. 所以 在学习如何增加上相应的模块. 主要学习的网站: 沧海书生 Ansible爱好者 https://www.cnblogs.com/tssc/p/ ...

  5. 解决idea tomcat乱码问题

    解决idea Server Output.TomcatLocalhost Log.Tomcat Catalina Log控制台乱码问题 问题原因:编码不一致,tomcat启动后默认编码UTF-8,而w ...

  6. 开始使用 Ubuntu(字体渲染去模糊+软件安装+优化配置+常见错误)(29)

    1. 中文字体渲染美化 + 去模糊 步骤: 1. 解压安装 lulinux_fontsConf_181226.tar.gz,按里面的安装说明操作: 2. 开启字体渲染: 打开 unity-tweak- ...

  7. STM32之中断函数

    本文做中断函数的索引,帮助我们找到中断函数名.中断函数参数以及中断服务函数他们的来源,以便我们编程. 1)如果一个工程只有一个中断,则我们可以进行两个步骤就可以了: 使能中断通道 编写中断服务函数 2 ...

  8. c++ (1) c++ 与c 的区别

    可以说c++ 语言在c基础上扩展了许多  在学习玩c语言之后  学习c++ 会发现容易一些  但是c++也有优越于c 的地方 c++ 与c 语言都属于本地编译型语言 ,直接编译成本地编译码,运行特别快 ...

  9. centos7上使用git clone出现问题

    centos 7  git clone时出现不支持协议版本的问题 unable to access 'https://github.com/baloonwj/TeamTalk.git/': Peer ...

  10. github上更新fork的别人的项目

    直接上解决方案的步骤 (1)在自己fork后的项目的位置上,点击New pull request. (2)比较和原创版本(base)的变化 (3 ) compare across forks. 使得左 ...