# gym100801I Insider's Information

## 题解

#include <bits/stdc++.h>

#define fi first
#define se second
#define db double
#define U unsigned
#define P std::pair<int,int>
#define LL long long
#define pb push_back
#define MP std::make_pair
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(int i = a;i <= b;++i)
#define ROF(i,a,b) for(int i = a;i >= b;--i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl

const int MAXN = 2e5 + 5;
int n,m,a[MAXN],b[MAXN],c[MAXN];
std::vector<int> G[MAXN],lim[MAXN];
int deg[MAXN];
int dfn[MAXN],p[MAXN];
bool zt[MAXN];// left=0 right=1
bool vis[MAXN];

int main(){
#ifndef RainAir
freopen("insider.in","r",stdin);
freopen("insider.out","w",stdout);
#endif
scanf("%d%d",&n,&m);
FOR(i,1,m){
scanf("%d%d%d",a+i,b+i,c+i);
G[a[i]].pb(i);
G[c[i]].pb(i);
++deg[b[i]];
}
std::queue<int> q;int ts = 0;
FOR(i,1,n) if(!deg[i]) q.push(i);
while(!q.empty()){
int v = q.front();q.pop();
dfn[v] = ++ts;
for(auto x:G[v]){
if(vis[x]) continue;
vis[x] = 1;
if(!--deg[b[x]]){
q.push(b[x]);
}
}
}
FOR(i,1,m) assert(!(dfn[b[i]] < dfn[a[i]] && dfn[b[i]] < dfn[c[i]]));
FOR(i,1,n) p[dfn[i]] = i;
FOR(i,1,m) if(dfn[a[i]] > dfn[c[i]]) std::swap(a[i],c[i]);
FOR(i,1,m) lim[b[i]].pb(i),lim[c[i]].pb(i);
FOR(i,1,n){
int cnt[2];CLR(cnt,0);
int v = p[i];
for(auto x:lim[v]){
if(v == b[x]){
if(dfn[c[x]] < dfn[b[x]]) continue;
cnt[zt[a[x]]]++;
}
if(v == c[x]){
if(dfn[b[x]] < dfn[c[x]]) continue;
cnt[zt[a[x]]^1]++;
}
}
zt[v] = cnt[0] > cnt[1] ? 0 : 1;
}
std::vector<int> v1,v2;
FOR(i,1,n){
if(zt[p[i]]) v2.pb(p[i]);
else v1.pb(p[i]);
}
std::reverse(all(v2));
for(auto x:v1) printf("%d ",x);
for(auto x:v2) printf("%d ",x);
puts("");
return 0;
}

posted @ 2020-11-20 14:41  RainAir  阅读(14)  评论(0编辑  收藏