:heavy_check_mark: test/aoj/GRL_3_A.test.cpp

Depends on

Code

// competitive-verifier: PROBLEM https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/3/GRL_3_A
#include "../../graph/graph-template.hpp"
#include "../../graph/lowlink.hpp"
#include "../../template.hpp"
void solve() {
    INT(n, m);
    Graph<bool> g(n);
    g.read(m, 0);
    LowLink lowlink(g);
    ranges::sort(lowlink.articulation);
    for(auto i : lowlink.articulation)
        print(i);
}
int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    solve();
}
#line 1 "test/aoj/GRL_3_A.test.cpp"
// competitive-verifier: PROBLEM https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/3/GRL_3_A
#line 2 "other/fastio.hpp"
// ref: https://maspypy.com/library-checker-many-a-b , Nyaanさん
#line 2 "other/type-utils.hpp"
#include <bits/stdc++.h>
using ll = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;
using u128 = unsigned __int128;
using vi = std::vector<int>;
using vii = std::vector<std::vector<int>>;
using pii = std::pair<int, int>;
using vl = std::vector<ll>;
using vll = std::vector<vl>;
using pll = std::pair<ll, ll>;

template <class T>
concept extended_integral =
    std::integral<T> || std::same_as<std::remove_cv_t<T>, i128> ||
    std::same_as<std::remove_cv_t<T>, u128>;
template <class T>
concept extended_signed_integral =
    std::signed_integral<T> || std::same_as<std::remove_cv_t<T>, i128>;
template <class T>
concept extended_unsigned_integral =
    std::unsigned_integral<T> || std::same_as<std::remove_cv_t<T>, u128>;

template <class T>
concept Streamable =
    requires(std::ostream &os, T &x) { os << x; } || extended_integral<T>;
template <class mint>
concept is_modint = requires(mint &x) {
    { x.val() } -> std::convertible_to<int>;
};
#line 4 "other/fastio.hpp"
namespace fastio {
constexpr int SZ = 1 << 17;
constexpr int offset = 64;
constexpr int mod = 10000;
char in_buf[SZ];
int in_left{}, in_right{};
char out_buf[SZ];
char out_tmp[offset];
int out_right{};
struct Pre {
    char num[4 * mod]{};
    constexpr Pre() {
        for(int i = 0; i < mod; ++i) {
            for(int n = i, j = 3; j >= 0; --j, n /= 10)
                num[4 * i + j] = '0' + n % 10;
        }
    }
    constexpr const char *operator[](int i) const { return &num[4 * i]; }
} constexpr pre;
void load() {
    memmove(in_buf, in_buf + in_left, in_right - in_left);
    in_right += -in_left + std::fread(in_buf + in_right - in_left, 1,
                                      SZ - (in_right - in_left), stdin);
    in_left = 0;
    if(in_right < SZ)
        in_buf[in_right++] = '\n';
}
void read(char &c) {
    do {
        if(in_left == in_right)
            load();
        c = in_buf[in_left++];
    } while(isspace(c));
}
void read(std::string &s) {
    s.clear();
    char c;
    do {
        if(in_left == in_right)
            load();
        c = in_buf[in_left++];
    } while(isspace(c));
    do {
        s += c;
        if(in_left == in_right)
            load();
        c = in_buf[in_left++];
    } while(!isspace(c));
}
template <extended_integral T> void read(T &x) {
    if(in_right - in_left < offset)
        load();
    char c;
    do
        c = in_buf[in_left++];
    while(c < '-'); // \n:10 space:32 -:45 '0':48
    bool minus{};
    if constexpr(extended_signed_integral<T>) {
        if(c == '-') {
            c = in_buf[in_left++];
            minus = true;
        }
    }
    x = 0;
    while(c >= '0') {
        x = 10 * x + (c & 15);
        c = in_buf[in_left++];
    }
    if constexpr(extended_signed_integral<T>) {
        if(minus)
            x = -x;
    }
}
void flush() { fwrite(out_buf, 1, std::exchange(out_right, 0), stdout); }
void write_range(const char *c, int n) {
    int pos{};
    while(pos < n) {
        if(out_right == SZ)
            flush();
        int len = std::min(n - pos, SZ - out_right);
        memcpy(out_buf + out_right, c + pos, len);
        out_right += len;
        pos += len;
    }
}

void write(char c) {
    if(SZ == out_right)
        flush();
    out_buf[out_right++] = c;
}
void write(const char *c) { write_range(c, strlen(c)); }
void write(const std::string &s) { write_range(s.data(), s.size()); }
template <std::floating_point T> void write(T x) {
    int n = std::snprintf(out_tmp, sizeof(out_tmp), "%.16g", x);
    write_range(out_tmp, n);
}
void write(bool x) { write(x ? '1' : '0'); }
template <extended_integral T> void write(T x) {
    if(x == 0) {
        write('0');
    }
    if constexpr(extended_signed_integral<T>) {
        if(x < 0) {
            write('-');
            x = -x;
        }
    }
    if(SZ - out_right < offset)
        flush();
    int cur = offset;
    for(; x >= 1000; x /= mod) {
        cur -= 4;
        memcpy(out_tmp + cur, pre[x % mod], 4);
    }
    if(x >= 100) {
        cur -= 3;
        memcpy(out_tmp + cur, pre[x % mod] + 1, 3);
    } else if(x >= 10) {
        cur -= 2;
        memcpy(out_tmp + cur, pre[x % mod] + 2, 2);
    } else if(x >= 1) {
        cur -= 1;
        memcpy(out_tmp + cur, pre[x % mod] + 3, 1);
    }
    write_range(out_tmp + cur, offset - cur);
}
struct Dummy {
    // プログラム終了時に出力
    ~Dummy() { flush(); }
} dummy;
} // namespace fastio
using fastio::write;
#line 4 "template.hpp"
// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#line 8 "template.hpp"
using namespace std;
#ifdef LOCAL
#include <debug.hpp>
#else
#define debug(...)
#endif
template <Streamable T> void print_one(const T &value) { fastio::write(value); }
template <is_modint T> void print_one(const T &value) {
    print_one(value.val());
}
void print() { print_one('\n'); }
template <class T, class... Ts> void print(const T &a, const Ts &...b) {
    print_one(a);
    ((print_one(' '), print_one(b)), ...);
    print();
}
template <ranges::range Iterable>
    requires(!Streamable<Iterable>)
void print(const Iterable &v) {
    for(auto it = v.begin(); it != v.end(); ++it) {
        if(it != v.begin())
            print_one(' ');
        print_one(*it);
    }
    print();
}
#define all(v) begin(v), end(v)
template <class T> void UNIQUE(T &v) {
    ranges::sort(v);
    v.erase(unique(all(v)), end(v));
}
template <typename T> inline bool chmax(T &a, T b) {
    return ((a < b) ? (a = b, true) : (false));
}
template <typename T> inline bool chmin(T &a, T b) {
    return ((a > b) ? (a = b, true) : (false));
}
// https://trap.jp/post/1224/
template <class... T> constexpr auto min(T... a) {
    return min(initializer_list<common_type_t<T...>>{a...});
}
template <class... T> constexpr auto max(T... a) {
    return max(initializer_list<common_type_t<T...>>{a...});
}
void input() {}
template <class Head, class... Tail> void input(Head &head, Tail &...tail) {
#ifdef LOCAL
    cin >> head;
#else
    fastio::read(head);
#endif
    input(tail...);
}
template <class T> void input(vector<T> &a) {
    for(T &x : a)
        input(x);
}
#define INT(...)                                                               \
    int __VA_ARGS__;                                                           \
    input(__VA_ARGS__)
#define LL(...)                                                                \
    long long __VA_ARGS__;                                                     \
    input(__VA_ARGS__)
#define STR(...)                                                               \
    string __VA_ARGS__;                                                        \
    input(__VA_ARGS__)
#define REP1_0(n, c) REP1_1(n, c)
#define REP1_1(n, c)                                                           \
    for(ll REP_COUNTER_##c = 0; REP_COUNTER_##c < (ll)(n); REP_COUNTER_##c++)
#define REP1(n) REP1_0(n, __COUNTER__)
#define REP2(i, a) for(ll i = 0; i < (ll)(a); i++)
#define REP3(i, a, b) for(ll i = (ll)(a); i < (ll)(b); i++)
#define REP4(i, a, b, c) for(ll i = (ll)(a); i < (ll)(b); i += (ll)(c))
#define overload4(a, b, c, d, e, ...) e
#define rep(...) overload4(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__)
ll inf = 3e18;
vl dx = {1, -1, 0, 0};
vl dy = {0, 0, 1, -1};
template <class T> constexpr T floor(T x, T y) noexcept {
    return x / y - ((x ^ y) < 0 and x % y);
}
template <class T> constexpr T ceil(T x, T y) noexcept {
    return x / y + ((x ^ y) >= 0 and x % y);
}
// yの符号に関わらず非負で定義 \bmod:texコマンド
template <class T> constexpr T bmod(T x, T y) noexcept {
    T m = x % y;
    return (m < 0) ? m + (y > 0 ? y : -y) : m;
}
template <std::signed_integral T> constexpr int bit_width(T x) noexcept {
    return std::bit_width((uint64_t)x);
}
template <std::signed_integral T> constexpr int popcount(T x) noexcept {
    return std::popcount((uint64_t)x);
}
constexpr bool kth_bit(auto n, auto k) { return (n >> k) & 1; }
#line 3 "graph/graph-template.hpp"
/**
 * @brief Graph Template(グラフテンプレート)
 * @see https://ei1333.github.io/library/graph/graph-template.hpp
 */
template <class T = ll> struct Edge {
    int from, to;
    T cost;
    int idx;
    Edge() = default;
    Edge(int from, int to, T cost = 1, int idx = -1)
        : from(from), to(to), cost(cost), idx(idx) {}
    Edge &operator=(const int &x) {
        to = x;
        return *this;
    }
    operator int() const { return to; }
};
template <class T = ll> struct Graph {
    using cost_type = T;
    vector<vector<Edge<T>>> g;
    int es; // edge_size
    Graph() = default;
    explicit Graph(int n) : g(n), es(0) {};
    int size() const { return ssize(g); }
    void add_directed_edge(int from, int to, T cost = 1) {
        g[from].emplace_back(from, to, cost, es++);
    }
    void add_edge(int from, int to, T cost = 1) {
        g[from].emplace_back(from, to, cost, es);
        g[to].emplace_back(to, from, cost, es++);
    }
    vector<Edge<T>> &operator[](const int &k) { return g[k]; }
    const vector<Edge<T>> &operator[](const int &k) const { return g[k]; }
    void read(int m, int padding = -1, bool weighted = false,
              bool directed = false) {
        for(int i = 0; i < m; ++i) {
            int a, b;
            T c(1);
            input(a, b);
            a += padding;
            b += padding;
            if(weighted)
                input(c);
            if(directed)
                add_directed_edge(a, b, c);
            else
                add_edge(a, b, c);
        }
    }
};
#line 4 "graph/lowlink.hpp"
/**
 * @brief Low Link(橋・関節点)
 * articulation(関節点) 削除すると連結成分が増える頂点
 * bridge(橋) 削除すると連結成分が増える辺(u,v)(u < v)
 * 未ソート
 */
template <class G> struct LowLink {
    const G &g;
    vector<int> ord, low, articulation;
    vector<pair<int, int>> bridge;
    LowLink(const G &g) : g(g), ord(g.size(), -1), low(g.size()) {
        for(int i = 0; i < ssize(g); ++i) {
            if(ord[i] == -1) {
                dfs(i, -1);
            }
        }
    }

  private:
    int id = 0;
    void dfs(int cur, int p) {
        ord[cur] = low[cur] = id++;
        bool is_articulation = false,
             second =
                 false; // 多重辺がある場合にDFS木の辺と後退辺を区別する必要がる
        int cnt = 0;
        for(auto &to : g[cur]) {
            if(to == p and !exchange(second, true))
                continue;
            if(ord[to] == -1) {
                dfs(to, cur);
                ++cnt;
                chmin(low[cur], low[to]);
                is_articulation |= p != -1 and ord[cur] <= low[to];
                if(ord[cur] < low[to]) {
                    bridge.emplace_back(minmax(cur, (int)to));
                }
            } else {
                chmin(low[cur], ord[to]);
            }
        }
        is_articulation |= p == -1 and cnt > 1;
        if(is_articulation)
            articulation.emplace_back(cur);
    }
};
#line 5 "test/aoj/GRL_3_A.test.cpp"
void solve() {
    INT(n, m);
    Graph<bool> g(n);
    g.read(m, 0);
    LowLink lowlink(g);
    ranges::sort(lowlink.articulation);
    for(auto i : lowlink.articulation)
        print(i);
}
int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    solve();
}

Test cases

Env Name Status Elapsed Memory
g++ 00_small_00.in :heavy_check_mark: AC 4 ms 4 MB
g++ 00_small_01.in :heavy_check_mark: AC 4 ms 4 MB
g++ 00_small_02.in :heavy_check_mark: AC 4 ms 4 MB
g++ 00_small_03.in :heavy_check_mark: AC 4 ms 4 MB
g++ 00_small_04.in :heavy_check_mark: AC 4 ms 4 MB
g++ 00_small_05.in :heavy_check_mark: AC 4 ms 4 MB
g++ 01_critical_00.in :heavy_check_mark: AC 4 ms 4 MB
g++ 01_critical_01.in :heavy_check_mark: AC 4 ms 3 MB
g++ 01_critical_02.in :heavy_check_mark: AC 4 ms 4 MB
g++ 01_critical_03.in :heavy_check_mark: AC 4 ms 3 MB
g++ 01_critical_04.in :heavy_check_mark: AC 4 ms 4 MB
g++ 01_critical_05.in :heavy_check_mark: AC 4 ms 4 MB
g++ 01_critical_06.in :heavy_check_mark: AC 4 ms 4 MB
g++ 01_critical_07.in :heavy_check_mark: AC 4 ms 4 MB
g++ 01_critical_08.in :heavy_check_mark: AC 4 ms 4 MB
g++ 01_critical_09.in :heavy_check_mark: AC 4 ms 4 MB
g++ 01_critical_10.in :heavy_check_mark: AC 4 ms 4 MB
g++ 01_critical_11.in :heavy_check_mark: AC 4 ms 4 MB
g++ 01_critical_12.in :heavy_check_mark: AC 4 ms 4 MB
g++ 02_grid_00.in :heavy_check_mark: AC 4 ms 4 MB
g++ 02_grid_01.in :heavy_check_mark: AC 4 ms 4 MB
g++ 02_grid_02.in :heavy_check_mark: AC 4 ms 4 MB
g++ 03_rand_00.in :heavy_check_mark: AC 4 ms 4 MB
g++ 03_rand_01.in :heavy_check_mark: AC 4 ms 4 MB
g++ 03_rand_02.in :heavy_check_mark: AC 4 ms 4 MB
g++ 03_rand_03.in :heavy_check_mark: AC 4 ms 4 MB
g++ 03_rand_04.in :heavy_check_mark: AC 4 ms 4 MB
g++ 03_rand_05.in :heavy_check_mark: AC 4 ms 4 MB
g++ 03_rand_06.in :heavy_check_mark: AC 4 ms 4 MB
g++ 03_rand_07.in :heavy_check_mark: AC 4 ms 4 MB
g++ 03_rand_08.in :heavy_check_mark: AC 4 ms 4 MB
g++ 03_rand_09.in :heavy_check_mark: AC 4 ms 4 MB
g++ 03_rand_10.in :heavy_check_mark: AC 4 ms 4 MB
g++ 03_rand_11.in :heavy_check_mark: AC 5 ms 4 MB
g++ 04_linear_00.in :heavy_check_mark: AC 4 ms 4 MB
g++ 04_linear_01.in :heavy_check_mark: AC 4 ms 4 MB
g++ 04_linear_02.in :heavy_check_mark: AC 6 ms 6 MB
g++ 04_linear_03.in :heavy_check_mark: AC 6 ms 6 MB
g++ 05_maximum_00.in :heavy_check_mark: AC 9 ms 7 MB
g++ 05_maximum_01.in :heavy_check_mark: AC 11 ms 7 MB
Back to top page