제출 #302939

#제출 시각아이디문제언어결과실행 시간메모리
302939nessie09슈퍼트리 잇기 (IOI20_supertrees)C++14
100 / 100
316 ms22264 KiB
#include "supertrees.h"
#include <vector>

using std::vector;

int construct(std::vector<std::vector<int>> p) {
    int n = p.size();

    vector<int> group(n, -1);
    vector<int> component(n, -1);
    int max_comp = 0;
    int cur_comp = -1;
    int cur_group = -1;
    int max_group = 0;

    for (int i = 0; i < n; ++i) {
        if (group[i] == -1) {
            group[i] = max_group;
            max_group += 1;
        }
        if (component[i] == -1) {
            component[i] = max_comp;
            max_comp += 1;
        }
        cur_comp = component[i];
        cur_group = group[i];

        for (int j = 0; j < n; ++j) {
            if (p[i][j] != p[j][i] || (i == j && p[i][j] != 1) || p[i][j] == 3) {
                return 0;
            }
            if (i == j) {
                continue;
            }

            if (p[i][j] == 1) {
                // one path, same group
                if (group[j] == -1) {
                    group[j] = cur_group;
                }
                else if (group[j] != cur_group) {
                    return 0;
                }
            }
            else {
                // 0 or more paths, different group
                if (group[j] == cur_group) {
                    return 0;
                }
            }


            if (p[i][j] != 0) {
                // at least one path, same component
                if (component[j] == -1) {
                    component[j] = cur_comp;
                }
                else if (component[j] != cur_comp) {
                    return 0;
                }
            }
            else {
                // no path, different component
                if (component[j] == cur_comp) {
                    return 0;
                }
            }
        }
    }

    vector<int> first_of_group(max_group, -1);
    vector<int> group_component(max_group, -1);
    for (int i = 0; i < n; ++i) {
        if (first_of_group[group[i]] == -1) {
            first_of_group[group[i]] = i;
        }
    }
    for (int i = 0; i < max_group; ++i) {
        group_component[i] = component[first_of_group[i]];
    }


    std::vector<std::vector<int>> answer;
    for (int i = 0; i < n; i++) {
        std::vector<int> row(n, 0);
        answer.push_back(row);
    }

    for (int i = 0; i < n; ++i) {
        for (int j = i+1; j < n; ++j) {
            if (group[j] == group[i]) {
                answer[i][j] = 1;
                break;
            }
        }
        for (int j = i-1; j >= 0; --j) {
            if (group[j] == group[i]) {
                answer[i][j] = 1;
                break;
            }
        }
        if (first_of_group[group[i]] == i) {
            int mygroup = group[i];
            int mycomp = component[i];
            int next_group = -1;
            int prev_group = -1;
            int have_next = 0;
            for (int j = 1; j < max_group; ++j) {
                next_group = (mygroup + j) % max_group;

                if (group_component[next_group] == mycomp) {
                    have_next = 1;
                    answer[i][first_of_group[next_group]] = 1;
                    answer[first_of_group[next_group]][i] = 1;
                    break;
                }
            }
            for (int j = 1; j < max_group; ++j) {
                prev_group = (mygroup - j + max_group) % max_group;

                if (group_component[prev_group] == mycomp) {
                    answer[i][first_of_group[prev_group]] = 1;
                    answer[first_of_group[prev_group]][i] = 1;
                    break;
                }
            }
            if (have_next && next_group == prev_group) {
                return 0;
            }
        }
    }

    build(answer);
    return 1;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...