HDU 1285 确定比赛名次 拓扑排序
2017-08-28 10:11
399 查看
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=1285思路
输入的数据一定有解:拓扑排序,优先取出编号小的点。#include<cstdio> #include<queue> #include<iostream> #include<functional> #include<vector> #include<map> #include<cstring> #include<string> #include<set> #include<stack> #include<algorithm> #define cle(a) memset(a,0,sizeof(a)) #define inf(a) memset(a,0x3f,sizeof(a)) #define ll long long #define Rep(i,a,n) for(int i=a;i<=n;i++) using namespace std; #define INF2 9223372036854775807ll const int INF = ( 2e9 ) + 2; const ll maxn = 510; vector<int> g[maxn]; int In[maxn]; void topsort(int n) { priority_queue<int,vector<int>,greater<int> > q; for(int i=1;i<=n;i++) if(In[i]==0)q.push(i); int cnt=0; while(!q.empty()) { int u=q.top();q.pop(); for(int i=0,L=g[u].size();i<L;i++) { int v=g[u][i]; In[v]--; if(In[v]==0) q.push(v); } if(cnt!=n-1) printf("%d ",u); else printf("%d\n",u); cnt++; } } int main() { int n,m; while(~scanf("%d%d",&n,&m)) { memset(In,0,sizeof(In)); for(int i=0;i<=n;i++)g[i].clear(); for(int i=0;i<m;i++) { int u,v; scanf("%d%d",&u,&v); g[u].push_back(v); In[v]++; } topsort(n); } }
相关文章推荐
- 确定比赛名次 HDU - 1285 (拓扑排序)
- HDU 1285--确定比赛名次【拓扑排序 && 邻接表实现】
- HDU 1285 确定比赛名次【拓扑排序】
- HDU - 1285 确定比赛名次 —— 拓扑排序
- hdu 1285 确定比赛名次 ( 拓扑排序 )
- HDU 1285确定比赛名次(拓扑排序)
- HDU-1285 确定比赛名次(拓扑排序)
- hdu 1285 确定比赛名次 拓扑排序
- HDU 1285-确定比赛名次(拓扑排序)
- hdu 1285 确定比赛名次 (简单的拓扑排序)
- hdu 1285 确定比赛名次(拓扑排序)
- HDU 1285 确定比赛名次 拓扑排序
- HDU 1285 确定比赛名次(拓扑排序)
- 【HDU】-1285-确定比赛名次(拓扑排序)(二维数组||邻接表||队列)
- HDU 1285 确定比赛名次(拓扑排序的三种实现方法)
- hdu 1285 确定比赛名次 (浙大拓扑排序模板)
- 【拓扑排序】HDU 1285--- 确定比赛名次
- HDU-1285-确定比赛名次-拓扑排序(模板)
- hdu 1285 确定比赛名次 拓扑排序
- HDU-1285 确定比赛名次 ---- 拓扑排序