Andrew and Taxi CodeForces - 1100E (思维,拓扑)
大意: 给定有向图, 每条边有一个权值, 假设你有$x$个控制器, 那么可以将所有权值不超过$x$的边翻转, 求最少的控制器数, 使得翻转后图无环
先二分转为判定问题. 每次check删除能动的边, 若剩余图有环显然不成立, 否则将剩余的图拓排一下, 再把能动的边按拓排的方向即可保证无环.
#include <iostream>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <string.h>
#include <queue>
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define hr cout<<'\n'
#define pb push_back
#define mid ((l+r)>>1)
#define lc (o<<1)
#define rc (lc|1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false);
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
void exgcd(ll a,ll b,ll &d,ll &x,ll &y){b?exgcd(b,a%b,d,y,x),y-=a/b*x:x=1,y=0,d=a;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head const int N = 4e5+10, INF = 0x3f3f3f3f;
int n, m;
struct _ {int u,v,w;}e[N];
vector<int> g[N];
int deg[N], s[N]; int chk(int x) {
REP(i,1,n) g[i].clear(),deg[i]=0;
REP(i,1,m) {
if (e[i].w>x) {
g[e[i].u].pb(e[i].v);
++deg[e[i].v];
}
}
queue<int> q;
*s = 0;
REP(i,1,n) if (!deg[i]) q.push(i),s[i]=++*s;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v:g[u]) {
if (!--deg[v]) q.push(v),s[v]=++*s;
}
}
REP(i,1,n) if (deg[i]) return 0;
return 1;
} int main() {
scanf("%d%d", &n, &m);
REP(i,1,m) scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
int l = 0, r = 1e9, ans;
while (l<=r) {
if (chk(mid)) ans=mid,r=mid-1;
else l=mid+1;
}
chk(ans);
vector<int> path;
REP(i,1,m) {
if (e[i].w<=ans&&s[e[i].u]>s[e[i].v]) {
path.pb(i);
}
}
printf("%d %d\n",ans,int(path.size()));
for (int i:path) printf("%d ",i);hr;
}
Andrew and Taxi CodeForces - 1100E (思维,拓扑)的更多相关文章
- E - E CodeForces - 1100E(拓扑排序 + 二分)
E - E CodeForces - 1100E 一个n个节点的有向图,节点标号从1到n,存在m条单向边.每条单向边有一个权值,代表翻转其方向所需的代价.求使图变成无环图,其中翻转的最大边权值最小的方 ...
- CodeForces - 1100E 二分+拓扑排序
题意: 一个n个节点的有向图,节点标号从1到n,存在m条单向边.每条单向边有一个权值,代表翻转其方向所需的代价.求使图变成无环图,其中翻转的最大边权值最小的方案,以及该方案翻转的最大的边权. Inpu ...
- CF 1100E Andrew and Taxi(二分答案)
E. Andrew and Taxi time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- CF-1100 E Andrew and Taxi
CF-1100E Andrew and Taxi https://codeforces.com/contest/1100/problem/E 知识点: 二分 判断图中是否有环 题意: 一个有向图,每边 ...
- CF1100E Andrew and Taxi
题目地址:CF1100E Andrew and Taxi 二分,每次取到一个 \(mid\) ,只保留长度 \(>mid\) 的边 dfs判环,若有环,说明 \(ans>mid\) ,否则 ...
- Codeforces Round #532 (Div. 2) E. Andrew and Taxi(二分+拓扑排序)
题目链接:https://codeforces.com/contest/1100/problem/E 题意:给出 n 个点 m 条边的有向图,要翻转一些边,使得有向图中不存在环,问翻转的边中最大权值最 ...
- E. Andrew and Taxi(二分+拓扑判环)
题目链接:http://codeforces.com/contest/1100/problem/E 题目大意:给你n和m,n代表有n个城市,m代表有m条边,然后m行输入三个数,起点,终点,花费.,每一 ...
- Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)
Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megaby ...
- CF1100E Andrew and Taxi 二分答案+拓扑排序
\(\color{#0066ff}{ 题目描述 }\) 给定一个有向图,改变其中某些边的方向,它将成为一个有向无环图. 现在求一个改变边方向的方案,使得所选边边权的最大值最小. \(\color{#0 ...
随机推荐
- 解决命令行执行shell脚本成功,但crontab执行失败
实际生产案例 生产机房自建PPTP客户端通过拨号连接到生产机房,但是一旦客户端网络是意外断线再重新拨号 会产生IP冲突,于是写了一个脚本监控PPTP的IP是否有多个(一般冲突以后会生成2个IP) #! ...
- New Garbage Collector http://wiki.luajit.org/New-Garbage-Collector
New Garbage Collector http://wiki.luajit.org/New-Garbage-Collector GC Algorithms This is a short ove ...
- B - Network---UVA 315(无向图求割点)
A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connectin ...
- Python开发【Django】:图片验证码、KindEditor
图片验证码 生成图片验证码需要以下: session check_code.py(依赖:Pillow,字体文件) 模块安装 pip install Pillow src属性后面加? 在utils下拷贝 ...
- 005-java的Annotation
一.概述 Annotation,JDK1.5开始提供 二.基本定义 public @interface HelloWorld { } 1.使用@Interface定义,名称大写 2.使用@Target ...
- [golang note] 协程基础
协程概念 √ 协程通常称为coroutine,在golang中称为goroutine. √ 协程本质上是一种用户态线程,它不需要操作系统来进行抢占式调度,在实际实现中寄存在线程之中. √ 协程系统开销 ...
- idea中使用插件Grep Console在IDEA的log的不同的级别,可以设置不同的颜色。
一.安装. 1. 2. 3. 二,使用. 1. 2.
- ng-深度学习-课程笔记-8: 超参数调试,Batch正则(Week3)
1 调试处理( tuning process ) 如下图所示,ng认为学习速率α是需要调试的最重要的超参数. 其次重要的是momentum算法的β参数(一般设为0.9),隐藏单元数和mini-batc ...
- C++多线程学习资料参考
新的C++11语法:<Professional C++>,或<C ++ Primer Plus>: C++11多线程:<C++ Concurrency in Actio ...
- 20145104张家明 《Java程序设计》第4周学习总结
20145104张家明 <Java程序设计>第4周学习总结 教材学习内容总结 第六章 1.继承的定义及目的 面向对象中,子类继承父类,避免重复的行为定义.不过并非为了避免重复定义行为就使用 ...