void TopSort(vector< vector<int> > AdjacencyMatrix)
{
queue<int> q;
int i,j;
int size = (int)AdjacencyMatrix.size();
vector<int> inDegree(size,0);
for(i = 0;i < size;++i)
{
for(j = 0;j < size;++j)
if(AdjacencyMatrix[j][i] == 1)
++inDegree[i];
if(inDegree[i] == 0)
q.push(i);
}
int v;
while(!q.empty())
{
// output
v = q.front();
q.pop();
for(i = 0;i < size;++i)
{
if(AdjacencyMatrix[v][i] == 1)
--inDegree[i];
if(inDegree[i] == 0)
q.push(i);
}
}
if(!q.empty())
cerr << "Graph has a cycle" << endl;
}