HDOJ 1285 拓扑排序 水题
2018-02-09 16:48
190 查看
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1285
最简单的拓扑排序题就不多说了。
最简单的拓扑排序题就不多说了。
#include<bits/stdc++.h> #define INF 1e18 #define inf 1e9 #define min(a,b) a<b?a:b #define max(a,b) a>b?a:b #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0) using namespace std ; typedef long long ll; typedef unsigned long long ull; const ll _max = 505; int n,m; int mp[_max][_max]; int ft[_max]; int ans[_max]; void topsort(){ priority_queue<int,vector<int>,greater<int> > q; while(!q.empty()) q.pop(); for(int i = 1 ; i <= n ; i++){ if(!ft[i]) q.push(i); } int id = 0; int cnt = 0; while(!q.empty()){ id = q.top(); q.pop(); ans[cnt++]=id; for(int i = 1 ; i <= n ; i++){ if(mp[id][i]){ ft[i]--; if(!ft[i]) q.push(i); } } } cout<<ans[0]; for(int i = 1 ; i < cnt ; i++) cout<<" "<<ans[i]; cout<<endl; return ; } int main(){ IOS; while(cin>>n>>m){ memset(mp,0,sizeof(mp)); memset(ft,0,sizeof(ft)); int u,v; for(int i = 1 ; i <= m ; i++){ cin>>u>>v; if(!mp[u][v]){ mp[u][v] = 1; ++ft[v]; } } topsort(); } return 0; }
相关文章推荐
- HDOJ--1285 确定比赛名次(拓扑排序)
- hdoj1285 拓扑排序
- hdoj 1285 确定比赛名次(拓扑排序)
- HDOJ1285 确定比赛名次(拓扑排序)
- hdoj1285 拓扑排序
- HDOJ 1285 确定比赛排名(拓扑排序)
- hdoj 1285 确定比赛名次【拓扑排序】
- HDOJ 1285 确定比赛名次(拓扑排序)
- HDOJ题目1285确定比赛名次(拓扑排序,模板)
- HDOJ 1285:确定比赛名次 拓扑排序
- HDOJ 1285 确定比赛名次(拓扑排序)
- HDOJ 1285 确定比赛名次(拓扑排序)
- 【拓扑排序】 HDOJ 1285 确定比赛名次
- hdoj1285(拓扑排序,利用临界矩阵写的)
- HDOJ 1285 确定比赛名次(拓扑排序)
- hdoj 1285 确定比赛名次 (经典拓扑排序题) 初学拓扑排序.做
- HDOJ---1285 确定比赛名次[拓扑排序]
- hdoj 1285 确定比赛名次 【拓扑排序】
- HDOJ1285 确定比赛名次(拓扑排序)
- hdoj.1285 确定比赛名次【拓扑排序】 2015/03/30