下面这个在dev c++上能跑,但是提交到HDU和vjudge的c++的时候报编译错误,无法识别优先队列的greater。
但是把HDU和vjudge提交的类型改为g++,就能通过了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int N = 501;
int n, m;
vector<int> graph[N];
int indegree[N];
int ans[N];
void func(int n){
//priority_queue<int, vector<int>, greater<int> > node; //存入度为0的点
priority_queue <int,vector<int>,greater<int> > node;
//queue<int> node;
for(int i = 1; i <= n; i++){
if(indegree[i] == 0){
node.push(i);
//printf("%d\n", i);
}
}
int num = 0;
while(!node.empty()){
int u = node.top();
//printf("%d\n", u);
node.pop();
ans[num++] = u;
for(int i = 0; i < graph[u].size(); i++){
int v = graph[u][i];
indegree[v]--; //后续节点入度-1
if(indegree[v] == 0){
node.push(v);
//printf("%d\n", v);
}
}
}
}
int main(){
int from, to;
while(cin >> n >> m){
if(n == 0 && m == 0) break;
//memset(graph, 0, sizeof(graph));
for(int i = 1; i <= n; i++)
graph[i].clear();
memset(indegree, 0, sizeof(indegree));
while(m--){
cin >> from >> to;
graph[from].push_back(to);
indegree[to]++;
//printf("%d\n", from);
}
func(n);
printf("%d", ans[0]);
for(int i = 1; i < n; i++){
printf(" %d", ans[i]);
}
printf("\n");
}
return 0;
} 
京公网安备 11010502036488号