CSES - HIIT Open 2017 - Results
Submission details
Task:Factory
Sender:Proudly div 2
Submission time:2017-05-27 15:15:17 +0300
Language:C++
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.04 sdetails
#2ACCEPTED0.06 sdetails
#3ACCEPTED0.05 sdetails
#4ACCEPTED0.03 sdetails
#5ACCEPTED0.14 sdetails
#6ACCEPTED0.09 sdetails
#7ACCEPTED0.10 sdetails
#8ACCEPTED0.10 sdetails
#9ACCEPTED0.10 sdetails
#10ACCEPTED0.09 sdetails
#11ACCEPTED0.10 sdetails
#12ACCEPTED0.09 sdetails
#13ACCEPTED0.13 sdetails
#14ACCEPTED0.09 sdetails
#15ACCEPTED0.16 sdetails
#16ACCEPTED0.04 sdetails
#170.03 sdetails

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;
        }
    }

}

Test details

Test 1

Verdict: ACCEPTED

input
1 0

correct output
1

user output
1

Test 2

Verdict: ACCEPTED

input
2 0

correct output
1

user output
1

Test 3

Verdict: ACCEPTED

input
2 1
2 1

correct output
2

user output
2

Test 4

Verdict: ACCEPTED

input
500 0

correct output
250

user output
250

Test 5

Verdict: ACCEPTED

input
500 124750
66 104
50 159
173 457
200 154
...

correct output
500

user output
500

Test 6

Verdict: ACCEPTED

input
500 96771
376 390
243 497
417 360
107 80
...

correct output
413

user output
413

Test 7

Verdict: ACCEPTED

input
500 106799
96 245
68 62
122 119
460 454
...

correct output
433

user output
433

Test 8

Verdict: ACCEPTED

input
500 83550
76 338
111 174
88 142
114 463
...

correct output
365

user output
365

Test 9

Verdict: ACCEPTED

input
500 98051
281 60
312 284
270 474
385 224
...

correct output
410

user output
410

Test 10

Verdict: ACCEPTED

input
500 86622
5 320
50 107
182 483
385 500
...

correct output
372

user output
372

Test 11

Verdict: ACCEPTED

input
500 99445
421 286
392 406
155 290
475 453
...

correct output
396

user output
396

Test 12

Verdict: ACCEPTED

input
500 99832
283 149
315 396
264 422
224 388
...

correct output
410

user output
410

Test 13

Verdict: ACCEPTED

input
500 116149
446 185
232 35
498 391
189 63
...

correct output
457

user output
457

Test 14

Verdict: ACCEPTED

input
500 84757
71 205
286 360
184 486
30 228
...

correct output
364

user output
364

Test 15

Verdict: ACCEPTED

input
500 108617
126 250
76 224
449 69
200 63
...

correct output
439

user output
439

Test 16

Verdict: ACCEPTED

input
10 20
7 8
1 3
4 8
5 9
...

correct output
5

user output
5

Test 17

Verdict:

input
10 20
2 9
9 8
4 3
7 3
...

correct output
5

user output
6