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;
}

原文地址:https://www.cnblogs.com/uid001/p/10480760.html

时间: 2024-08-30 07:01:04

Andrew and Taxi CodeForces - 1100E (思维,拓扑)的相关文章

CF-1100E Andrew and Taxi

CF-1100E Andrew and Taxi https://codeforces.com/contest/1100/problem/E 知识点: 二分 判断图中是否有环 题意: 一个有向图,每边有一条边权,对于一个值x,可以使得边权小于等于x的边反转,求最小的x,使得某些边反转之后图中没有环 分析:Suppose we have k traffic controllers. They can turn all edges whose weight is less than or equal

Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)

Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are given a directed graph consisting of n vertices and m edges (each edge is directed, so it can

Codeforces 919D Substring (拓扑排序+树形dp)

题目:Substring 题意:给你一个有向图, 一共有n个节点 , m条变, 一条路上的价值为这个路上出现过的某个字符最多出现次数, 现求这个最大价值, 如果价值可以无限大就输出-1. 题解:当这个有向图构成一个环的时候就会使得值无限大,所以先用拓扑排序判断一下有没有环,如果有环直接输出-1, 如果没有环就再使用树形dp并记忆化存数,来找到最大值. 代码: 1 #include<cstring> 2 #include<iostream> 3 using namespace std

CF1100E Andrew and Taxi

首先我们发现,本题具有可二分性.若花费x可以完成,x+1也一定可以完成. 那么判断是否可行,可以把二分得到的mid作为下限,仅连接边权大于等于mid的边,如果这样的图有环,那么向上二分,否则向下. 这样的正确性显然,因为如果图是一个DAG,那么剩下的边始终从拓扑序小的向大的连,这样就不会出现环. 输出方案的思路也是如此. #include<iostream> #include<cctype> #include<cstdio> #include<cstring>

Queue CodeForces - 353D (思维dp)

https://codeforces.com/problemset/problem/353/D 大意:给定字符串, 每一秒, 若F在M的右侧, 则交换M与F, 求多少秒后F全在M左侧 $dp[i]$为位置$i$处的$F$复位所花费时间, 有 $dp[i] = max(dp[i-1]+1,cnt_i)$, $cnt_i$为前$i$位$M$的个数 $dp$最大值即为答案 #include <iostream> #include <algorithm> #include <cstd

CodeForces 1131B(思维题)

You still have partial information about the score during the historic football match. You are given a set of pairs (ai,bi)(ai,bi), indicating that at some point during the match the score was "aiai: bibi". It is known that if the current score

CF915D Almost Acyclic Graph (思维+拓扑)

如果想要判定是否是DAG,用拓扑排序是一个好选择,但是本题可以删一条边 如果真的傻傻的去枚举删边就难顶了 我们要想到,对于删边,其实就是入度-1,而我们知道,删完能拓扑,说明成功了,因此只要枚举点,对入度操作再跑拓扑,就能AC 这个转化还是很有意思的,我们来思考正确性,首先对于一个环,肯定因为到了某个情况所有的入度都不为0,所以加不进队列.而对于环上一点,本来跑完拓扑之后就剩下环上的边,这次入度-1,相当于 忽略了环上的边,就成功了破解了,如果这个点上存在两个环,那还是没用,因为减完入度还有 #

codeforces 510c (拓扑排序)

#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <queue> using namespace std; const int N = 111111; int topo[205]; struct node { char a[105]; }e[105]; int n; int g[30][30]; int f[30]; int

Codeforces 721C [dp][拓扑排序]

/* 题意:给你一个有向无环图.给一个限定t. 问从1点到n点,在不超过t的情况下,最多可以拜访几个点. 保证至少有一条路时限不超过t. 思路: 1.由无后向性我们可以知道(取决于该图是一个DAG),这题一定可以dp. 2.dp[i][j]代表,到达点i,并且拜访了j个城市的最短时间. wa点: 没有初始化数组中的0.. */ #include<bits/stdc++.h> #define N 5050 using namespace std; int inf=0x3f3f3f3f; int