Code Submission Evaluation System Login

CSES - HIIT Open 2017

HIIT Open 2017

Contest start:2017-05-27 11:00:00
Contest end:2017-05-27 16:00:00

Task list | Submit code | Submissions | Messages | Scoreboard


History
2017-05-27 15:15:17
Task:Factory
Sender:Proudly div 2
Submission time:2017-05-27 15:15:17
Status:READY
Result:WRONG ANSWER

Show test data

Code

#include <iostream>
#include <vector>
#include <set>

using namespace std;

int main(){
    int nVertices, nEdges;
    cin >> nVertices >> nEdges;
    vector<vector<int> > children(nVertices);
    vector<vector<int> > parents(nVertices);
    vector<int> temp_out_degrees(nVertices);
    vector<int> temp_in_degrees(nVertices);

    for(int i = 0; i < nEdges; i++){
        int from, to;
        cin >> from >> to; from--; to--;
        children[from].push_back(to);
        parents[to].push_back(from);
        temp_out_degrees[from]++;
        temp_in_degrees[to]++;
    }
    
    vector<int> lengths(nVertices);
    vector<int> Q;

    for(int i = 0; i < nVertices; i++){
        if(temp_out_degrees[i] == 0){
            Q.push_back(i);
            lengths[i] = 1;
        }
    }
    
    while(Q.size() > 0){
        int v = Q.back(); Q.pop_back();
        for(int p : parents[v]){
            temp_out_degrees[p]--;
            if(temp_out_degrees[p] == 0){
                int length = 0;
                for(int c : children[p]){
                    length = max(length, lengths[c]+1);
                }
                lengths[p] = length;
                Q.push_back(p);
            }
        }
    }
    
    set<pair<int,int> > prioQ;
    for(int i = 0; i < nVertices; i++){
        if(parents[i].size() == 0){
            prioQ.insert({-lengths[i], i});
        }
    }
    
    for(int day = 1; ; day++){
        // Take up to two jobs
        int j1 = prioQ.begin()->second;
        prioQ.erase(prioQ.begin());
        int j2 = -1;
        if(prioQ.size() > 0){
            j2 = prioQ.begin()->second;
            prioQ.erase(prioQ.begin());
        }
        
        // Push children
        for(int v : children[j1]){
            temp_in_degrees[v]--;
            if(temp_in_degrees[v] == 0)
                prioQ.insert({-lengths[v],v});
        }
        if(j2 != -1){
            for(int v : children[j2]){
                temp_in_degrees[v]--;
                if(temp_in_degrees[v] == 0)
                    prioQ.insert({-lengths[v],v});
            }
        }
        
        if(prioQ.size() == 0){
            cout << day << endl;
            return 0;
        }
    }

}