vijos1790
注意要建反圖,走逆向拓撲序
若正向無法保證在當前最有的情況下,是否全局最有,而反向則滿足
#include<cstdio> #include<cctype> #include<queue> using namespace std; int n,m,indg[100002],head[100002],to[200002],nex[200002],cnt,id[100002]; priority_queue<int>q;inline void read(int &x){char ch=getchar();x=0;while(!isdigit(ch))ch=getchar();while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();} }inline void addedge(int u,int v){indg[v]++;to[++cnt]=v;nex[cnt]=head[u];head[u]=cnt; }int main(){read(n);read(m);while(m--){int u,v;read(u);read(v);u--;v--;addedge(v,u);}int tot=n;for(int i=0;i<n;i++)if(indg[i]==0)q.push(i);while(!q.empty()){int now=q.top();q.pop();id[now]=tot--;for(int i=head[now];i;i=nex[i])if((--indg[to[i]])==0)q.push(to[i]);}for(int i=0;i<n;i++)if(indg[i]>0)id[i]=tot--;for(int i=0;i<n;i++)printf("%d ",id[i]); }?
轉載于:https://www.cnblogs.com/MikuKnight/p/8981153.html
總結
- 上一篇: 如何线上推广引流?百度知道实现精准引流
- 下一篇: 用极大似然法估计因子载荷矩阵_spss教