CSES - NOI 2019 - Results
Submission details
Task:Distance Code
Sender:Ivar
Submission time:2019-03-06 15:53:56 +0200
Language:C++
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
Test results
testverdicttimegroup
#1--1, 2, 3details
#2--1, 2, 3details
#3--1, 2, 3details
#4--1, 2, 3details
#5--1, 2, 3details
#6--1, 2, 3details
#7--1, 2, 3details
#8--1, 2, 3details
#9--1, 2, 3details
#10--1, 2, 3details
#11--1, 2, 3details
#12--2, 3details
#13--2, 3details
#14--2, 3details
#15--2, 3details
#16--3details
#17--3details
#18--3details
#19--3details
#20--1, 2, 3details

Code

using namespace std;
#include <bits/stdc++.h> // Remove first slash when turned in
#define rep(i, a, b) for(int i = a; i < b; i++)
#define sz(v) int(v.size())
#define trav(x, v) for(auto &x : v)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;



int t, n;

vector<set<int>> neighbors;

void genList() {
    set<tuple<int, int>> close;
    set<tuple<int, int>> indicies;

    rep(i, 0, n) {
        close.insert(
            {sz(neighbors[i]), i} );
        indicies.insert(
            {i, sz(neighbors[i])}
        );
    }
    int index; int _;
    rep(i, 0, n) {
        auto elem = close.begin();
        tie(_, index) = *elem;
        cout << index+1 << " ";
        close.erase(elem);
        //cout << "1" << endl; 
        auto neighbor = *neighbors[index].begin();
        //cout << "2" << endl;
        auto a = indicies.upper_bound({neighbor, -1});
        //cout << "3" << endl;
        int ind, num;
        tie(ind, num) = *a;
        //cout << "4" << endl;

        indicies.erase(a);
        indicies.insert({ind, num-1});
        //cout << "5" << endl;
        //cout << "Close size: " << sz(close) << " contains " << num << " " << ind << endl;
        close.erase(close.find({num, ind}));
        close.insert({num-1, ind});
        //cout << "6" << endl;
        neighbors[index].erase(neighbors[index].begin());
        neighbors[neighbor].erase(neighbors[neighbor].find(index));
    } 

}


vector<int> distanceRemoved;
vector<vi> edges;
vector<vi> distances;


void calcDist() {
    rep(i, 0, n) {

        deque<tuple<int, int>> q = {{i, 0}};
        vector<bool> seen(n, false);

        while (!q.empty()) {
            int el, dist;
            tie(el, dist) = q.front();
            q.pop_front();
            if (seen[el])
                continue;
            seen[el] = true;

            distances[i][el] = dist;

            rep(j, 0, n) {
                if (edges[el][j] != 1)
                    continue;
                q.emplace_back(j, dist+1);
            }
        }
    }
}
void updateDist(int node) {
    deque<tuple<int, int>> q;
    q.emplace_back(tuple<int,int>(node, 0));
    vector<bool> seen(n, false);

    while (!q.empty()) {
        int el, dist;
        tie(el, dist) = q.front();
        q.pop_front();
        if (seen[el])
            continue;
        seen[el] = true;

        distances[node][el] = dist;
        distances[el][node] = dist;

        rep(j, 0, n) {
            if (edges[el][j] != 1)
                continue;
            q.emplace_back(j, dist+1);
        }
    }
}

bool possible(int lastnode, int ind) {
    
    updateDist(lastnode);
    
    if (ind == n-1) {
        rep(i, 0, n) {
            rep(j, i+1, n) {
                if (edges[i][j] == 1)
                    cout << i+1 << " " << j+1 << endl;
            }
        }
        return true;
    }
    
    if (distanceRemoved[ind] == 1) {
        edges[lastnode][lastnode+1] = 1;
        edges[lastnode+1][lastnode] = 1;

        if (possible(lastnode+1, ind+1)) return true;
        edges[lastnode][lastnode+1] = 0;
        edges[lastnode+1][lastnode] = 0;
    }
    else {

        rep(i, 0, lastnode) {
            if (distances[i][lastnode] == distanceRemoved[ind]-1) {
                
                edges[lastnode+1][i] = 1;
                edges[i][lastnode+1] = 1;
                if (possible(lastnode+1, ind+1)) return true;
                edges[lastnode+1][i] = 0;
                edges[i][lastnode+1] = 0;
            }
        }
    }
    return false;
}

void genTree() {
    reverse(all(distanceRemoved));
    distances.assign(n, vi(n, 10000000));
    edges.assign(n, vi(n, 0));
    calcDist();
    possible(0, 0);
}


int main() {

    cin >> t;
    cin >> n;
    if (t == 1) {
        int a, b;
        neighbors.assign(n, set<int>());
        rep(i, 0, n-1) {
            cin >> a >> b;
            neighbors[a-1].insert(b-1);
            neighbors[b-1].insert(a-1);
        }

        genList();
        return 0;
    }
    else if (t == 2) {
        int a;
        rep(i, 0, n-1) {
            cin >> a;
            distanceRemoved.push_back(a);
        }
        genTree();
    }
    return 0;
}

Test details

Test 1

Group: 1, 2, 3

Verdict:

input
1
2
2 1

correct output
(empty)

user output
(empty)

Test 2

Group: 1, 2, 3

Verdict:

input
1
3
3 1
2 1

correct output
(empty)

user output
(empty)

Test 3

Group: 1, 2, 3

Verdict:

input
1
4
3 2
2 1
4 1

correct output
(empty)

user output
(empty)

Test 4

Group: 1, 2, 3

Verdict:

input
1
4
2 3
3 4
1 3

correct output
(empty)

user output
(empty)

Test 5

Group: 1, 2, 3

Verdict:

input
1
5
3 5
4 1
1 3
...

correct output
(empty)

user output
(empty)

Test 6

Group: 1, 2, 3

Verdict:

input
1
5
3 2
3 4
5 1
...

correct output
(empty)

user output
(empty)

Test 7

Group: 1, 2, 3

Verdict:

input
1
5
4 3
1 4
4 2
...

correct output
(empty)

user output
(empty)

Test 8

Group: 1, 2, 3

Verdict:

input
1
10
9 3
8 9
2 9
...

correct output
(empty)

user output
(empty)

Test 9

Group: 1, 2, 3

Verdict:

input
1
10
9 2
5 8
7 1
...

correct output
(empty)

user output
(empty)

Test 10

Group: 1, 2, 3

Verdict:

input
1
10
10 4
9 1
4 7
...

correct output
(empty)

user output
(empty)

Test 11

Group: 1, 2, 3

Verdict:

input
1
10
2 6
4 3
3 5
...

correct output
(empty)

user output
(empty)

Test 12

Group: 2, 3

Verdict:

input
1
500
10 6
6 255
6 428
...

correct output
(empty)

user output
(empty)

Test 13

Group: 2, 3

Verdict:

input
1
500
152 466
451 313
158 479
...

correct output
(empty)

user output
(empty)

Test 14

Group: 2, 3

Verdict:

input
1
500
109 440
330 190
443 161
...

correct output
(empty)

user output
(empty)

Test 15

Group: 2, 3

Verdict:

input
1
500
144 373
257 233
341 318
...

correct output
(empty)

user output
(empty)

Test 16

Group: 3

Verdict:

input
1
100000
54983 75172
93807 75172
44082 75172
...

correct output
(empty)

user output
(empty)

Test 17

Group: 3

Verdict:

input
1
100000
88863 19059
86423 76688
98536 95984
...

correct output
(empty)

user output
(empty)

Test 18

Group: 3

Verdict:

input
1
100000
59979 6389
19097 24999
27846 82330
...

correct output
(empty)

user output
(empty)

Test 19

Group: 3

Verdict:

input
1
100000
58761 66001
25102 51081
98625 67861
...

correct output
(empty)

user output
(empty)

Test 20

Group: 1, 2, 3

Verdict:

input
1
6
2 1
3 2
4 2
...

correct output
(empty)

user output
(empty)