GYM 101102 K.Topological Sort(线段树)

news/2024/7/7 12:49:25

Description
给出一个n个点的图,初始状态i点会向所有编号大于它的点连边,之后删去m条边,求删边之后涂的字典序最小的拓扑序
Input
第一行一整数T表示用例组数,每组用例首先输入两个整数n和m分别表示点数和要删除的边数,之后m行每行两个整数u和v表示删去u->v的这一条边(1<=n<=1e5)
Output
输出最小字典序的拓扑序
Sample Input
3
3 2
1 3
2 3
4 0
4 2
1 2
1 3
Sample Output
3 1 2
1 2 3 4
2 3 1 4
Solution
每次选择入度最小且编号最小的作为当前位置的点,之后把和这个点连的所有边的入度减一,而已经删去的那些不用减一,查询区间最小值,单点更新,区间更新,用线段树维护每个点的入度即可,时间复杂度O(nlogn+mlogn)
Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define maxn 111111
#define ls (t<<1)
#define rs ((t<<1)|1)
int T,n,m;
vector<int>g[maxn];
int Min[maxn<<2],Pos[maxn<<2],Lazy[maxn<<2],du[maxn];
void push_up(int t)
{
    Min[t]=min(Min[ls],Min[rs]);
    if(Min[ls]<Min[rs])Pos[t]=Pos[ls];
    else Pos[t]=Pos[rs];
}
void build(int l,int r,int t)
{
    Lazy[t]=0;
    if(l==r)
    {
        Min[t]=du[l],Pos[t]=l;
        return ;
    }
    int mid=(l+r)/2;
    build(l,mid,ls),build(mid+1,r,rs);
    push_up(t);
}
void push_down(int l,int r,int t)
{
    if(l==r)return ;
    if(Lazy[t])
    {
        int temp=Lazy[t];
        Lazy[t]=0;
        Lazy[ls]+=temp,Lazy[rs]+=temp,Min[ls]+=temp,Min[rs]+=temp;
    }
}
void update(int L,int R,int l,int r,int t,int v)
{
    if(L>R)return ;
    if(L<=l&&r<=R)
    {
        Min[t]+=v,Lazy[t]+=v;
        return ;
    }
    push_down(l,r,t);
    int mid=(l+r)/2;
    if(L<=mid)update(L,R,l,mid,ls,v);
    if(R>mid)update(L,R,mid+1,r,rs,v);
    push_up(t);
}
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)g[i].clear();
        for(int i=1;i<=n;i++)du[i]=i-1;
        while(m--)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            g[u].push_back(v);
            du[v]--;
        }
        build(1,n,1);
        for(int i=1;i<=n;i++)
        {
            int u=Pos[1];
            update(u,u,1,n,1,INF);
            update(u+1,n,1,n,1,-1);
            for(int j=0;j<g[u].size();j++)
            {
                int v=g[u][j];
                update(v,v,1,n,1,1);
            }
            printf("%d%c",u,i==n?'\n':' ');
        }
    }
    return 0;
}

http://www.niftyadmin.cn/n/3301779.html

相关文章

软件测试和软件调试的区别

最近替客户写论文,整理提纲的时候发现他们把软件的测试和调试的部分分开写,虽然知道两者有区别但是当时根本搞不清楚应该怎么写,网上找了些资料看了以后才有些概念,现在贴出来,以后可那能用的到. 1,软件测试是找出软件已经存在的错误,而调试是定位错误,修改程序以修正错误. 2,…

总结工作中用到的ES6语法,方便工作中查看,也总结一下经验

1.模板字符串&#xff1a; 表现形式&#xff1a;${} 举例子&#xff1a; import axios from axios;let base https://www.baidu.com/home/msg/data/personalcontent; console.log(${base}/login,${base}/login) export const requestLogin params > { return axios.post($…

VS2008安装失败原因总结

今天系统是刚装的&#xff0c;今儿个也不是第一次装系统&#xff0c;也不是第一次装vs2008了&#xff0c;遇上vs2008安装出错倒是头一回。 先装系统&#xff0c;接着装0ffice2007,接着装ms sqlserver 2005,再装adobe cs4 master套装&#xff0c;一路setup&#xff0c;很是顺利&…

GYM 101102 L.Starry Night(贪心+dfs)

Description 给出一棵树&#xff0c;问通过删点最多能够把这棵树变成多少个星星&#xff0c;星星就是一个点周围不小于三条链且不能有分叉 Input 第一行一整数T表示用例组数&#xff0c;每组用例首先输入一整数n表示点数&#xff0c;之后n-1行每行两个整数u和v表示树上一条…

Delphi容器类之---TList、TStringList、TObjectList,以及一个例程的代码分析

转载自&#xff1a;http://blog.csdn.net/jqandjq/article/details/5429137 看了这里标题&#xff0c;大家可能以为我会谈TListBox控件&#xff0c;那就错了。我要谈的是Delphi提供给我们的具有列表性质的类&#xff1a;TStringList、TList和TObjectList。TStringList用来存放字…

GYM 101149 A.Balls in Urn(水~)

Description 一次箱子里有n种颜色的球&#xff0c;第i种颜色球有a[i]个&#xff0c;现在可以选出一个球得知其数量&#xff0c;之后会从箱子里一个个把所有球都拿出来要猜其颜色&#xff0c;让最坏情况下猜对的次数最大 Input 第一行一整数n表示球的颜色数量&#xff0c;之后…

windows系统下搭建NT32环境

http://sourceforge.net/apps/mediawiki/tianocore/index.php?titleUDK2010.SR1.UP1_How-to-Build 准备&#xff1a; 1&#xff09;VS2008TEAM版本&#xff0c;en_visual_studio_team_system_2008_team_suite_x86_x64wow_dvd_X14-26461.iso 2&#xff09;VS sp1&#xff0c…

(转载)Delphi TStringList的用法

Delphi TStringList的用法 TStrings是一个抽象类&#xff0c;在实际开发中&#xff0c;是除了基本类型外&#xff0c;应用得最多的。 TStringList 常用方法与属性: var List: TStringList; i: Integer; begin List : TStringList.Create; List.Add(Strings1); {添加} List.Add(…