——Kurisu、红

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

#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>
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;
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();
}
}

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++){
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
*/

#### 2014-2015 ACM-ICPC, NEERC, Moscow Subregional Contest —— E. Equal Digits

2014-11-10 23:31:25

#### 2015-2016 ACM-ICPC, NEERC, Moscow Subregional Contest K. King’s Rout（拓扑排序）

2015-11-12 20:11:46

#### F. Friends（2014-2015 ACM-ICPC, NEERC, Moscow Subregional Contest ）

2014-11-13 18:05:00

#### 2015-2016 ACM-ICPC, NEERC, Moscow Subregional Contest K

2016-08-29 19:24:27

#### 2015-2016 ACM-ICPC, NEERC, Moscow Subregional Contest

2015-11-02 23:05:06

#### 2015-2016 ACM-ICPC, NEERC, Moscow Subregional Contest A

2016-08-29 19:21:18

#### 2016-2017 ACM-ICPC, NEERC, Moscow Subregional Contest K. Knights of the Old Republic kruskal

2017-08-24 22:04:17

#### 2017-2018 ACM-ICPC, NEERC, Northern Subregional Contest

2017-11-18 19:43:36

#### 2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest J Bottles

2016-10-27 10:20:33

#### 【2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest A】【水题 讨论】Alex Origami Squares 矩形内置三个最大正方形

2015-11-15 20:42:37