codeforces 532 E. Andrew and Taxi 二分 + 拓扑排序

E

题意 给你一个有向边权图 你可以改变一条边的方向 贡献为这条边的边权 如果你改变了很多条边 那么贡献就是这么多条边中最大的那条边的边权

做法 二分那个最大边权 那么小于最大边权的相当于对图没影响 判断其他是不是一个环

然后如何建反边 利用 拓扑序对有向边 u -> v 来讲 如果 u 的拓扑序 是小于 v 的拓扑序 那么就是无环的

于是我们只要检查边两个点的拓扑序如果前面大于后面则需要交换

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
const int MAX_N = 100025;
int q[MAX_N],ind[MAX_N],n,m,top[MAX_N];
struct in
{
    int a,b,c;
}arr[MAX_N];
vector<int > ans;
vector<pll> G[MAX_N];
bool check(int mid)
{
    int rt = 0;
    for(int i = 1;i<=n;++i) ind[i] = 0;
    for(int i = 1;i<=n;++i)
    {
        for(int j = 0;j<G[i].size();++j)
        {
            if(G[i][j].second>mid) ind[G[i][j].first]++;
        }
    }
    for(int i = 1;i<=n;++i)
        if(ind[i]==0)
        {
            q[++rt] = i;
            top[i] = rt;
        }
    for(int i = 1;i<=rt;++i)
    {
        int u = q[i];
        for(int j = 0;j<G[u].size();j++)
        {
            pll now = G[u][j];
            if(now.second<=mid) continue;
            if(--ind[now.first]==0)
            {
                q[++rt] = now.first;
                top[now.first] = rt;
            }
        }
    }
    for(int i = 1;i<=n;++i) if(ind[i]) return false;
    return true;
}
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i = 1;i<=m;++i)
    {
        scanf("%d%d%d",&arr[i].a,&arr[i].b,&arr[i].c);
        G[arr[i].a].push_back(pll(arr[i].b,arr[i].c));
    }
    int l = 0,r = 1000000000;
    while(l<=r)
    {
        int mid = (l+r)>>1;
        if(check(mid)) r = mid - 1;
        else l = mid + 1;
    }
    check(l);
    for(int i = 1;i<=m;++i)
    {
        if(arr[i].c>l) continue;
        else if(top[arr[i].a]>top[arr[i].b]) ans.push_back(i);
    }
    printf("%d %d\n",l,ans.size());
    int sz = ans.size();
    for(int i = 0;i<sz;++i)
        i==sz-1?printf("%d\n",ans[i]):printf("%d ",ans[i]);
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/heucodesong/article/details/88704644
今日推荐