結果

問題 No.1477 Lamps on Graph
ユーザー sten_sansten_san
提出日時 2021-07-03 03:56:51
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 105 ms / 2,000 ms
コード長 3,434 bytes
コンパイル時間 1,128 ms
コンパイル使用メモリ 31,100 KB
実行使用メモリ 10,960 KB
最終ジャッジ日時 2023-09-12 09:15:55
合計ジャッジ時間 6,051 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 0 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 0 ms
4,376 KB
testcase_06 AC 0 ms
4,376 KB
testcase_07 AC 0 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 0 ms
4,376 KB
testcase_12 AC 54 ms
5,904 KB
testcase_13 AC 49 ms
5,848 KB
testcase_14 AC 66 ms
6,616 KB
testcase_15 AC 27 ms
4,376 KB
testcase_16 AC 22 ms
4,688 KB
testcase_17 AC 19 ms
4,468 KB
testcase_18 AC 59 ms
9,536 KB
testcase_19 AC 48 ms
6,400 KB
testcase_20 AC 17 ms
4,376 KB
testcase_21 AC 51 ms
8,108 KB
testcase_22 AC 11 ms
4,376 KB
testcase_23 AC 34 ms
4,420 KB
testcase_24 AC 75 ms
7,480 KB
testcase_25 AC 24 ms
4,376 KB
testcase_26 AC 71 ms
8,296 KB
testcase_27 AC 32 ms
4,584 KB
testcase_28 AC 42 ms
5,724 KB
testcase_29 AC 36 ms
4,728 KB
testcase_30 AC 29 ms
5,864 KB
testcase_31 AC 23 ms
4,664 KB
testcase_32 AC 86 ms
10,792 KB
testcase_33 AC 82 ms
10,424 KB
testcase_34 AC 67 ms
10,960 KB
testcase_35 AC 105 ms
10,160 KB
testcase_36 AC 103 ms
10,044 KB
testcase_37 AC 91 ms
10,116 KB
testcase_38 AC 96 ms
10,160 KB
testcase_39 AC 101 ms
10,192 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdbool.h>

typedef struct vector {
    void *head;
    size_t used, capa;
} *vector;

#define vector_make(...) vector_make_overload(__VA_ARGS__, vector_make_2, vector_make_1)(__VA_ARGS__)
#define vector_make_1(bsize) vector_make_(bsize)
#define vector_make_2(type, size) vector_make_(sizeof(type) * (size))
#define vector_make_overload(arg1, arg2, name, ...) name

#define vector_resize(type, vec, size) vector_resize_(vec, sizeof(type) * (size))

#define vector_index(type, vec, index) ((type *)(vec)->head)[index]
#define vector_size(type, vec) ((vec)->used / sizeof(type))
#define vector_push_back(type, vec, value) \
do { \
    vector_resize(type, vec, vector_size(type, vec) + 1); \
    vector_index(type, vec, vector_size(type, vec) - 1) = value; \
} while (0)

vector vector_make_(size_t bsize) {
    vector v = malloc(sizeof(struct vector));

    if (v == NULL) {
        return NULL;
    }

    v->head = malloc(bsize);

    if (v->head == NULL) {
        free(v);
        return NULL;
    }

    v->used = bsize;
    v->capa = bsize;

    return v;
}

bool vector_resize_(vector v, size_t bsize) {
    if (bsize <= v->capa) {
        v->used = bsize;
        return true;
    }

    size_t new_capa = (bsize + v->capa) / (v->capa + 1) * (v->capa + 1);

    void *new = realloc(v->head, new_capa);
    if (new == NULL) {
        return false;
    }

    v->head = new;
    v->used = bsize;
    v->capa = new_capa;

    return true;
}

vector a;
int compare(const void *x, const void *y) {
    return vector_index(int, a, *(int *)x) - vector_index(int, a, *(int *)y);
}

int main() {
    int n, m;
    scanf("%d %d", &n, &m);

    a = vector_make(int, n);
    for (int i = 0; i < n; ++i) {
        scanf("%d", &vector_index(int, a, i));
    }

    vector g = vector_make(vector, n);
    for (int i = 0; i < n; ++i) {
        vector_index(vector, g, i) = vector_make(int, 0);
    }

    for (int i = 0; i < m; ++i) {
        int u, v;
        scanf("%d %d", &u, &v);
        --u; --v;

        vector_push_back(int, vector_index(vector, g, u), v);
        vector_push_back(int, vector_index(vector, g, v), u);
    }

    vector lamp = vector_make(bool, n);
    for (int i = 0; i < n; ++i) {
        vector_index(bool, lamp, i) = false;
    }

    int k;
    scanf("%d", &k);

    for (int i = 0; i < k; ++i) {
        int b;
        scanf("%d", &b);
        --b;
        vector_index(bool, lamp, b) = true;
    }

    vector x = vector_make(int, 0);

    vector index = vector_make(int, n);
    for (int i = 0; i < n; ++i) {
        vector_index(int, index, i) = i;
    }

    qsort(&vector_index(int, index, 0), n, sizeof(int), &compare);
    for (int i = 0; i < n; ++i) {
        int idx = vector_index(int, index, i);

        if (!vector_index(bool, lamp, idx)) {
            continue;
        }

        vector_push_back(int, x, idx);

        vector list = vector_index(vector, g, idx);

        vector_index(bool, lamp, idx) = false;
        for (int j = 0; j < vector_size(int, list); ++j) {
            int v = vector_index(int, list, j);

            if (vector_index(int, a, idx) < vector_index(int, a, v)) {
                vector_index(bool, lamp, v) ^= 1;
            }
        }
    }

    printf("%zu\n", vector_size(int, x));
    for (int i = 0; i < vector_size(int, x); ++i) {
        printf("%d\n", vector_index(int, x, i) + 1);
    }
}

0