Ejemplo n.º 1
0
    vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) {
        vector<vector<int>> graph(numCourses);
        vector<int>         indegrees(numCourses, 0);
        queue<int>          q;

        for (const auto& pair : prerequisites) {
            graph[pair.second].emplace_back(pair.first);    // must take second before first
            ++indegrees[pair.first];
        }

        for (int n = 0; n < indegrees.size(); ++n) {
            if (indegrees[n] == 0) {
                q.push(n);
            }
        }

        vector<int> result;
        result.reserve(numCourses);

        int visited = 0;
        while (!q.empty()) {
            int n = q.front();
            q.pop();
            ++visited;
            result.emplace_back(n);
            for (int m : graph[n]) {
                if (--indegrees[m] == 0) {
                    q.push(m);
                }
            }
        }

        if (numCourses != visited) {
            result.clear();
        }

        return result;
    }
    string alienOrder(vector<string>& words) {
        //find the order, apparently it is a topologcal sorting problem
        //1. make graph
        //2. top sorting with the graph,
        //question is how to avoid cycle?
        
        vector<unordered_set<int> > graph(26);
        vector<int> indegrees(26);
        vector<int> result;
        
        int row_size = words.size();
        
        //make graph
	    int stop = '$' - 'a';
        int count = 0;
        //init graph for every existed letter!!!
        //the letter could have no pre or post 
        //don't forget
        for(int i=0;i<words.size();i++){
            for(int j=0;j<words[i].size();j++){
		        int add = words[i][j] - 'a';
                if(graph[add].empty())
                {
                    graph[add].insert(stop);
                    count++;
                }
            }
        }	    
	    
        for(int i = 0; i < row_size - 1; i++)
        {
            int len1 = words[i].size(), len2 = words[i+1].size();
            for(int j = 0; j < min(len1, len2); j++)
            {
                int u = words[i][j] - 'a';
                int v = words[i+1][j] - 'a';
                if(u != v) 
                {
                    if(graph[u].find(v) == graph[u].end())
		            {
                        graph[u].insert(v);
			            indegrees[v]++;
		            }
		            break; //important!!!
                }
            }
        }
       	
        //topological sorting
        queue<int> q;
        for(int k = 0; k < indegrees.size(); k++)
        {
            if(indegrees[k] == 0 && !graph[k].empty())
            {
                q.push(k);
            }
        }
        //int cnt = 0;
        while(!q.empty())
        {
            int n = q.front();
            q.pop();
            //if(graph[n].empty()) continue;
            result.push_back(n);
            for(auto neigh : graph[n])
            {
                if(neigh != stop && (--indegrees[neigh] == 0))
                {
                    q.push(neigh);
                }
            }
            count--;
        }
        
        if(count != 0) return "";
            
        //convert the results to string
        string s;
        for(auto v : result)
        {
            s += (v + 'a');
        }
        
        return s;
    }