답안 #834173

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
834173 2023-08-22T11:31:55 Z kamelfanger83 늑대인간 (IOI18_werewolf) C++17
100 / 100
698 ms 117136 KB
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <set>

//#include "werewolf.h"

using namespace std;

struct Unionfind{
    vector<int> group;
    Unionfind(int n){
        group.resize(n);
        iota(group.begin(), group.end(), 0);
    }
    int find(int i){
        if (i == group[i]) return i;
        return group[i] = find(group[i]);
    }
    void mergeBtA(int a, int b){
        a = find(a); b = find(b);
        if (a == b) return;
        group[b] = a;
    }
};

#define maxK 18

vector<int> check_validity(int N, vector<int> X, vector<int> Y, vector<int> S, vector<int> E, vector<int> L, vector<int> R){
    vector<vector<int>> cons (N);
    for (int i = 0; i < X.size(); ++i) {
        cons[X[i]].push_back(Y[i]);
        cons[Y[i]].push_back(X[i]);
    }
    vector<vector<int>> childrend (N);
    vector<int> parentd (N, -1);
    vector<int[maxK]> parentTPd (N);
    vector<vector<int>> childrenu (N);
    vector<int> parentu (N, -1);
    vector<int[maxK]> parentTPu (N);
    auto bT = [&cons, &N](bool down, vector<vector<int>> &fchildren, vector<int> &fparent, vector<int[maxK]> &fillTP){
        Unionfind unionfind (N);
        for (int i = down ? N - 1 : 0; down ? i >= 0 : i < N; i += down ? -1 : 1) {
            for (auto c : cons[i]){
                if (c > i && down || c < i && !down){
                    c = unionfind.find(c);
                    if (c == i) continue;
                    fparent[c] = i;
                    fchildren[i].push_back(c);
                    unionfind.mergeBtA(i, c);
                }
            }
            sort(fchildren[i].begin(), fchildren[i].end());
            fchildren[i].erase(unique(fchildren[i].begin(), fchildren[i].end()), fchildren[i].end());
        }
        for (int i = 0; i < N; ++i) {
            if (fparent[i] == -1) fillTP[i][0] = i;
            else fillTP[i][0] = fparent[i];
        }
        for (int i = 1; i < maxK; ++i) {
            for (int j = 0; j < N; ++j) {
                fillTP[j][i] = fillTP[fillTP[j][i-1]][i-1];
            }
        }
    };
    bT(true, childrend, parentd, parentTPd);
    bT(false, childrenu, parentu, parentTPu);
    vector<int> preorder (N, -1); int C = 0;
    vector<pair<int, int>> prerange (N); // inclusive, exclusive
    auto dfs = [&](auto &&self, int i) -> void{
        prerange[i].first = C;
        preorder[i] = C++;
        for (auto c : childrend[i]){
            self(self, c);
        }
        prerange[i].second = C;
    };
    for (int i = 0; i < N; ++i) {
        if (preorder[i] == -1) dfs(dfs, i);
    }
    vector<set<int>> insubt (N);
    for (int i = 0; i < N; ++i) {
        insubt[i].insert(preorder[i]);
    }
    auto collect = [&](auto &&self, int i) -> void{
        for (auto c : childrenu[i]){
            if (insubt[c].size()) self(self, c);
            if (insubt[c].size() > insubt[i].size()) swap(insubt[c], insubt[i]);
            for (auto ins : insubt[c]) insubt[i].insert(ins);
            insubt[c].clear();
        }
    };
    vector<int> Qind (S.size());
    iota(Qind.begin(), Qind.end(), 0);
    sort(Qind.begin(), Qind.end(), [&](int a, int b){return R[a] < R[b];});
    vector<int> res (S.size());
    for (auto ansi : Qind){
        int husubt = S[ansi];
        if (husubt < L[ansi]) {
            res[ansi] = 0;
            continue;
        }
        for (int i = maxK - 1; i >= 0; --i) {
            if (parentTPd[husubt][i] >= L[ansi]) husubt = parentTPd[husubt][i];
        }
        int wesubt = E[ansi];
        if (wesubt > R[ansi]){
            res[ansi] = 0;
            continue;
        }
        for (int i = maxK - 1; i >= 0; --i) {
            if (parentTPu[wesubt][i] <= R[ansi]) wesubt = parentTPu[wesubt][i];
        }
        collect(collect, wesubt);
        if (insubt[wesubt].lower_bound(prerange[husubt].first) != insubt[wesubt].end() && *insubt[wesubt].lower_bound(prerange[husubt].first) < prerange[husubt].second) res[ansi] = 1;
        else res[ansi] = 0;
    }
    return res;
}

Compilation message

werewolf.cpp: In function 'std::vector<int> check_validity(int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
werewolf.cpp:32:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   32 |     for (int i = 0; i < X.size(); ++i) {
      |                     ~~^~~~~~~~~~
werewolf.cpp: In lambda function:
werewolf.cpp:46:27: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   46 |                 if (c > i && down || c < i && !down){
      |                     ~~~~~~^~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 5 ms 1748 KB Output is correct
11 Correct 5 ms 1748 KB Output is correct
12 Correct 5 ms 1748 KB Output is correct
13 Correct 5 ms 1748 KB Output is correct
14 Correct 5 ms 1748 KB Output is correct
15 Correct 6 ms 1876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 676 ms 95012 KB Output is correct
2 Correct 553 ms 99892 KB Output is correct
3 Correct 495 ms 99372 KB Output is correct
4 Correct 503 ms 99388 KB Output is correct
5 Correct 534 ms 99432 KB Output is correct
6 Correct 560 ms 99900 KB Output is correct
7 Correct 616 ms 102716 KB Output is correct
8 Correct 478 ms 99820 KB Output is correct
9 Correct 368 ms 99256 KB Output is correct
10 Correct 398 ms 99768 KB Output is correct
11 Correct 412 ms 99680 KB Output is correct
12 Correct 476 ms 100472 KB Output is correct
13 Correct 635 ms 110908 KB Output is correct
14 Correct 600 ms 110908 KB Output is correct
15 Correct 588 ms 110992 KB Output is correct
16 Correct 587 ms 111012 KB Output is correct
17 Correct 647 ms 102764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 5 ms 1748 KB Output is correct
11 Correct 5 ms 1748 KB Output is correct
12 Correct 5 ms 1748 KB Output is correct
13 Correct 5 ms 1748 KB Output is correct
14 Correct 5 ms 1748 KB Output is correct
15 Correct 6 ms 1876 KB Output is correct
16 Correct 676 ms 95012 KB Output is correct
17 Correct 553 ms 99892 KB Output is correct
18 Correct 495 ms 99372 KB Output is correct
19 Correct 503 ms 99388 KB Output is correct
20 Correct 534 ms 99432 KB Output is correct
21 Correct 560 ms 99900 KB Output is correct
22 Correct 616 ms 102716 KB Output is correct
23 Correct 478 ms 99820 KB Output is correct
24 Correct 368 ms 99256 KB Output is correct
25 Correct 398 ms 99768 KB Output is correct
26 Correct 412 ms 99680 KB Output is correct
27 Correct 476 ms 100472 KB Output is correct
28 Correct 635 ms 110908 KB Output is correct
29 Correct 600 ms 110908 KB Output is correct
30 Correct 588 ms 110992 KB Output is correct
31 Correct 587 ms 111012 KB Output is correct
32 Correct 647 ms 102764 KB Output is correct
33 Correct 698 ms 100532 KB Output is correct
34 Correct 228 ms 32716 KB Output is correct
35 Correct 690 ms 101512 KB Output is correct
36 Correct 610 ms 100132 KB Output is correct
37 Correct 660 ms 100920 KB Output is correct
38 Correct 647 ms 100440 KB Output is correct
39 Correct 586 ms 101252 KB Output is correct
40 Correct 639 ms 112948 KB Output is correct
41 Correct 521 ms 100412 KB Output is correct
42 Correct 457 ms 100344 KB Output is correct
43 Correct 657 ms 106072 KB Output is correct
44 Correct 593 ms 100864 KB Output is correct
45 Correct 436 ms 101312 KB Output is correct
46 Correct 415 ms 100664 KB Output is correct
47 Correct 584 ms 111148 KB Output is correct
48 Correct 581 ms 110848 KB Output is correct
49 Correct 588 ms 111100 KB Output is correct
50 Correct 551 ms 110872 KB Output is correct
51 Correct 589 ms 117136 KB Output is correct
52 Correct 603 ms 117004 KB Output is correct