Andrew and Taxi CodeForces - 1100E (思维,拓扑)

时间:2022-07-01 01:15:48

大意: 给定有向图, 每条边有一个权值, 假设你有$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;
}