Asuna_acmer

——Kurisu、红

2015-2016 ACM-ICPC, NEERC, Moscow Subregional Contest K题:King's Rout [topo排序]

题意:给出N个点,M条有向边,求字典序最小的topo排序,注意有重边,且不一定连通。

范围:N20W,M40W;

解法:在普通的topo排序之上,将queue改成优先队列即可实现字典序最小。


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<iostream>
#include<stdlib.h>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<bitset>
#pragma comment(linker, "/STACK:1024000000,1024000000")
template <class T>
bool scanff(T &ret){
    char c; int sgn; T bit=0.1;
    if(c=getchar(),c==EOF) return 0;
    while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();
    sgn=(c=='-')?-1:1;
    ret=(c=='-')?0:(c-'0');
    while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
    if(c==' '||c=='\n'){ ret*=sgn; return 1; }
    while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;
    ret*=sgn;
    return 1;
}
#define inf 1073741824
#define llinf 4611686018285162540LL
#define eps 1e-9
#define mod 9223372034707292160LL
#define pi acos(-1.0)
#define lth (th<<1)
#define rth (th<<1|1)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define drep(i,a,b) for(int i=a;i>=b;i--)
#define mset(x,val) memset(x,val,sizeof(x))
#define mcpy(x,y) memcpy(x,y,sizeof(y))
#define findx(x) lower_bound(b+1,b+1+bn,x)-b
#define fuck puts("FUCK!")
#define mpii(a,b) make_pair(a,b);
#define NN 101010
#define MM 202020
using namespace std;
typedef long long ll;
typedef long double lb;
typedef pair<int,int> pii;
int n,m,s,t,lm,head,tail;
set<int> st[222222];
set<int>::iterator it;
int start[222222],de[222222];
struct Edge{
    int to,val,nx;
    Edge(){}
    Edge(int t,int n){
        to=t,nx=n;
    }
}ed[MM*5];

struct node{
    int val,idx;
}ans[222222];

bool cmp(node a,node b){
    return a.val<b.val;
}

void init(){
    lm=0;
    rep(i,0,n){
        start[i]=-1;
        de[i]=0;
        st[i].clear();
    }
}

void add(int x,int y){
    ed[lm]=Edge(y,start[x]);
    start[x]=lm++;
}

void topo(){
    priority_queue<int>q;
    rep(i,1,n) if(!de[i]) q.push(i);
    int cnt=n;
    while(!q.empty()){
        int x=q.top();
        q.pop();
        ans[x].val=cnt--;
        ans[x].idx=x;
        for(int i=start[x];~i;i=ed[i].nx){
            Edge& e=ed[i];
            de[e.to]--;
            if(de[e.to]==0) q.push(e.to);
        }
    }
}

int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        init();
        rep(i,1,m){
            int x,y;
            scanff(x);
            scanff(y);
            st[y].insert(x);
        }
        rep(i,1,n){
            for(it=st[i].begin();it!=st[i].end();it++){
                add(i,*it);
                de[*it]++;
            }
        }
        topo();
        sort(ans+1,ans+1+n,cmp);
        rep(i,1,n) printf("%d%c",ans[i].idx,i==n?'\n':' ');
    }
}
/*
5 6
2 1
5 2
4 1
5 4
3 1
5 3

3 1
3 1
*/



阅读更多
版权声明:文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/GrassTreeFlower/article/details/49387801
个人分类: 图论--topo排序
上一篇2015-2016 ACM-ICPC, NEERC, Moscow Subregional Contest I题:Illegal or Not? [暴力题]
下一篇Codeforces Round #327 (Div. 1) A题: Median Smoothing [规律题]
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭