标签:turn cout use hdu ret display nbsp contest ack
拓扑排序,从上往下回溯,挺好奇差分约束为啥就不对。拓扑排序有明显的优先级。
#include<bits/stdc++.h> using namespace std; #define pb push_back typedef long long ll; const int inf=0x3f3f3f3f; typedef long long ll; typedef double db; const int N=1e4+50; vector<int>e[N]; int n,m; int in[N],a[N]; int topo(){ int cnt=0; queue<int>Q; for(int i=1;i<=n;i++)if(in[i]==0)cnt++,Q.push(i); while(!Q.empty()){ int u=Q.front();Q.pop(); for(int i=0;i<e[u].size();i++){ int v=e[u][i];--in[v]; if(!in[v]){ Q.push(v),cnt++; a[v]=max(a[v],a[u]+1); } } } if(cnt<n)return -1; int sum=0; for(int i=1;i<=n;i++)sum+=888+a[i]; return sum; } int main(){ while(~scanf("%d %d",&n,&m)){ memset(in,0,sizeof in); memset(a,0,sizeof a); for(int i=1;i<=n;i++)e[i].clear(); for(int i=1,u,v;i<=m;i++){ scanf("%d %d",&u,&v); e[v].pb(u);in[u]++; } cout<<topo()<<endl; } // system("pause"); return 0; }
带权并查集
HZNU Training 23 for Zhejiang Provincial Competition 2020
标签:turn cout use hdu ret display nbsp contest ack
原文地址:https://www.cnblogs.com/littlerita/p/12748812.html