广州天河网站建设,gif在线制作,网站地图怎么做_,外贸多语言网站way#xff1a;在图里面能延伸的越远#xff0c;deep越大#xff0c;说明它能从自己延伸很长到别的节点#xff08;别的节点一定有入度#xff09;#xff0c;它越可能没有入度。
#includeiostream
#includevector
#includemap
#includeset…way在图里面能延伸的越远deep越大说明它能从自己延伸很长到别的节点别的节点一定有入度它越可能没有入度。
#includeiostream
#includevector
#includemap
#includeset
#includealgorithm
using namespace std;class Node
{
public:int label;vectorNode * neighbors;Node(){}Node(int x){labelx;}
};class Record
{
public:Node* node;int deep;Record(Node* n, int deep){noden;this-deepdeep;}
};Record* getRecord(Node *cur, mapNode *,Record *mp)
{if(mp.count(cur)) return mp[cur];int follow0;for(auto next: cur-neighbors){follow max(follow, getRecord(next, mp)-deep);}Record *pnew Record(cur,follow1);mp[cur]p;return p;
}bool comp(Record *a, Record*b)
{return (a-deep)(b-deep);
}vectorNode* topoSort(vectorNode*graph)
{//获取所有节点的deep然后存到map中;mapNode*, Record*mp;for(auto node: graph){getRecord(node, mp);}//将Record*们放到vec中准备排序vectorRecord*vec;for(auto pa: mp){vec.push_back(pa.second);}sort(vec.begin(),vec.end(),comp);//放到答案数组中vectorNode*result;for(auto m: vec){result.push_back(m-node);}return result;
}
需要根据deep进行排序 然后又要能根据排序结果得到对应Node放到result答案数组中返回所以封装了个Record。