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 to kk. Then let‘s remove all these edges from the graph, make a topological sorting of the remaining graph, and orient the other edges in the order of topological sorting. If there are cycles left in the graph after removing the edges, then we cannot get rid of them, having k traffic controllers. Otherwise, by adding edges we will not add new loops. The parameter k can be iterated through a binary search. Also in binary search, you can go through not all possible values of k, but only the values that are on the edges.

Complexity — O((n+m)logC)or O((n+m)logm

假如我们有k个交通管理员(即x),它们可以反转所有边权小于等于x的边,然后先把这些边从图中移除,拓扑排序这个图,如果这个图中还有环,那么我们应该使k变大。我们可以二分判定这个k,或者直接遍历所有边权即可。

下面方面使用dfs获得每个结点被访问的时间戳(不是dfs序列),因为这个题我们可以通过检测每个边的两端点的时间戳是否和原来方向相反来判断是否需要翻转。而一般的我们确实需要用拓扑排序来看是否有边没有遍历从而得到是否有环存在。(由于拓扑排序必须以入读为0的点开始)

#include <bits/stdc++.h>
using namespace std;
int n,m,a[100005],b[100005],c[100005],pos[100005],cur;
vector<int> v[100005],e;
//获得每个结点的时间戳
void dfs(int node)
{
    pos[node]=1;
    for(int u:v[node])
        if(!pos[u])
            dfs(u);
    pos[node]=cur--;;
}
bool check(int x)
{
    //清空处理
    for(int i=1;i<=n;i++) v[i].clear();
    for(int i=0;i<=n;i++)pos[i]=0;
    e.clear();
    //移除翻转的边
    for(int i=0;i<m;i++)
        if(c[i]>x)
            v[a[i]].push_back(b[i]);
    cur=m;
    for (int i=1;i<=n;i++)
        if(!pos[i])
            dfs(i);
    for(int i=0;i<m;i++)
    {
        //判断是否有环
        if(pos[a[i]]>pos[b[i]])
        {
            if(c[i]>x) return false;
            e.push_back(i+1);
        }
    }
    return true;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin>>n>>m;
    for(int i=0;i<m;i++)
        cin>>a[i]>>b[i]>>c[i];
    //二分答案
    int st=0,en=1e9;
    while(st!=en)
    {
        int mid=(st+en)/2;
        if(check(mid)) en=mid;
        else st=mid+1;
    }
    check(st);
    cout<<st<<" "<<e.size()<<endl;
    for(int i:e) cout<<i<<" ";
    return 0;
}

原文地址:https://www.cnblogs.com/1625--H/p/10279583.html

时间: 2024-07-31 07:33:48

CF-1100E Andrew and Taxi的相关文章

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

大意: 给定有向图, 每条边有一个权值, 假设你有$x$个控制器, 那么可以将所有权值不超过$x$的边翻转, 求最少的控制器数, 使得翻转后图无环 先二分转为判定问题. 每次check删除能动的边, 若剩余图有环显然不成立, 否则将剩余的图拓排一下, 再把能动的边按拓排的方向即可保证无环. #include <iostream> #include <algorithm> #include <math.h> #include <cstdio> #include

CF1100E Andrew and Taxi

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

E - Andrew and Taxi-二分答案-topo判环

E - Andrew and Taxi 思路 :min max   明显二分答案,二分需要破坏的那些边的中机器人数量最多的那个. check 过程建边时直接忽略掉小于 mid 的边,这样去检验有无环存在即可. 当时有一点担心会出现 有一个环 有一条边 反过来之后 这个环破坏了 却成就了 另一个环,但是画图发现 这样的图 ,它们两边会形成 一个大环.那个大环一定会通过别的边破坏掉,所以不需要担心这种情况.topo 判环即可. #include<bits/stdc++.h> using names

Codeforces Round #532 (Div. 2)

D.Dasha and chess 题意 有666个黑棋子在一个999*999的棋盘上,你只有一个白棋子,黑棋子可以走到棋盘的任何地方,白棋可以走到九宫格内的点,你和交互器轮流下棋(每次只能操作一个棋子),白棋与任何一个黑棋在同一行/同一列就算你赢,给定一个局面,让白棋赢 由鸽巢定理 以(500,500)为中心,四个象限内一定存在一个象限的棋子 \(\leq 666 / 4\),剩下三个象限棋子的和一定 \(\geq 666 / 4 * 3 = 500\),走到(500,500)后沿着到棋子最少

[CSP校内集训]pestc(拓扑排序)

题意 给一个边带权的有向图,可以花费边权使得一条边反向:通过翻转边让原图变成一个DAG,要求是所有花费中的最大值最小\(,(n,m\leq 200000)\),保证无重边和自环 解法1 考场上没看出来性质,于是口胡了一个乱搞做法 连好边后直接对原图进行一遍拓扑排序,由于原图不是DAG,所以会有无法入队的环存在:如果当前队列为空而有点没有被遍历到,那么就强行选择一个点将连向它的边翻转: 具体的,我们选择\((max(\) 连向\(i\)的边 \())\)最小的\(i\),由于翻转了连向\(i\)的

[C1] Andrew Ng - AI For Everyone

About this Course AI is not only for engineers. If you want your organization to become better at using AI, this is the course to tell everyone--especially your non-technical colleagues--to take. In this course, you will learn: The meaning behind com

微信 {&quot;errcode&quot;:40029,&quot;errmsg&quot;:&quot;invalid code, hints: [ req_id: Cf.y.a0389s108 ]&quot;}

{"errcode":40029,"errmsg":"invalid code, hints: [ req_id: Cf.y.a0389s108 ]"} 问题:微信网页授权后,获取到 openid 了,一刷新又没了 微信网页授权获取到的 code 只能使用一次(5分钟内有效),使用一次后,马上失效. 页面授权跳转成功,根据 code 也换取到 openid 了. 此时刷新页面,并不会再次进行授权,而是直接刷新了一下上一次授权跳转后的链接,带的还是

CF with friends and user&#39;s influence considered on NYC data(updated Aug,11st)

Here is the code link: https://github.com/FassyGit/LightFM_liu/blob/master/U_F1.py I use NYC data as other experimens. The split of the training data was seperated by the timeline, and I have normalised the interaction matrix by replacing the checkin

CF 750

今天CF打的块残废了     就是一废物 A 在24点之前到 直接模拟即可 #include<stdio.h> #include<algorithm> #include<cstring> #include<string> #include<cmath> using namespace std; #define LL long long #define MAXN 1010 #define inf 1000000000.0 int main() {