competitive-programming-library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub kmyk/competitive-programming-library

:heavy_check_mark: graph/functional_graph.yuki1254.test.cpp

Depends on

Code

#define PROBLEM "https://yukicoder.me/problems/no/1254"
#include <algorithm>
#include <cstdio>
#include <map>
#include <utility>
#include <vector>
#include "utils/macros.hpp"
#include "graph/functional_graph.hpp"
using namespace std;

vector<int> solve(int n, const vector<pair<int, int> >& edges) {
    vector<vector<int> > g(n);
    map<pair<int, int>, int> lookup;
    REP (i, edges.size()) {
        auto [a, b] = edges[i];
        g[a].push_back(b);
        g[b].push_back(a);
        lookup[make_pair(a, b)] = i;
        lookup[make_pair(b, a)] = i;
    }

    auto cycle = get_namori_cycle(g);
    vector<int> ans;
    REP (i, cycle.size()) {
        int a = cycle[i];
        int b = cycle[(i + 1) % cycle.size()];
        ans.push_back(lookup[make_pair(a, b)]);
    }
    sort(ALL(ans));
    return ans;
}

int main() {
    int n; scanf("%d", &n);
    vector<pair<int, int> > edges(n);
    REP (i, n) {
        int a, b; scanf("%d%d", &a, &b);
        -- a;
        -- b;
        edges[i] = make_pair(a, b);
    }
    vector<int> ans = solve(n, edges);
    printf("%d\n", (int)ans.size());
    REP (i, ans.size()) {
        printf("%d%c", ans[i] + 1, i + 1 < ans.size() ? ' ' : '\n');
    }
    return 0;
}
#line 1 "graph/functional_graph.yuki1254.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/1254"
#include <algorithm>
#include <cstdio>
#include <map>
#include <utility>
#include <vector>
#line 2 "utils/macros.hpp"
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i))
#define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i))
#define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i))
#define ALL(x) std::begin(x), std::end(x)
#line 2 "graph/functional_graph.hpp"
#include <cassert>
#include <deque>
#include <functional>
#line 6 "graph/functional_graph.hpp"

/**
 * @brief Namori cycle / なもり閉路
 * @param g a simple connected undirected graph with |E| = |V|
 */
std::deque<int> get_namori_cycle(const std::vector<std::vector<int> >& g) {
    int n = g.size();
    {  // check the namori-ty
        int m = 0;
        REP (i, n) {
            m += g[i].size();
        }
        assert (m == 2 * n);
    }

    std::deque<int> stk;
    std::vector<bool> used(n);
    auto go = [&](auto&& go, int i, int parent) -> int {
        if (used[i]) return i;
        stk.push_back(i);
        used[i] = true;
        for (int j : g[i]) if (j != parent) {
            int k = go(go, j, i);
            if (k != -1) return k;
        }
        assert (stk.back() == i);
        stk.pop_back();
        used[i] = false;
        return -1;
    };
    int i = go(go, 0, -1);
    assert (i != -1);  // fails if the graph is not simple
    while (stk.front() != i) {
        stk.pop_front();
    }
    return stk;
}
#line 9 "graph/functional_graph.yuki1254.test.cpp"
using namespace std;

vector<int> solve(int n, const vector<pair<int, int> >& edges) {
    vector<vector<int> > g(n);
    map<pair<int, int>, int> lookup;
    REP (i, edges.size()) {
        auto [a, b] = edges[i];
        g[a].push_back(b);
        g[b].push_back(a);
        lookup[make_pair(a, b)] = i;
        lookup[make_pair(b, a)] = i;
    }

    auto cycle = get_namori_cycle(g);
    vector<int> ans;
    REP (i, cycle.size()) {
        int a = cycle[i];
        int b = cycle[(i + 1) % cycle.size()];
        ans.push_back(lookup[make_pair(a, b)]);
    }
    sort(ALL(ans));
    return ans;
}

int main() {
    int n; scanf("%d", &n);
    vector<pair<int, int> > edges(n);
    REP (i, n) {
        int a, b; scanf("%d%d", &a, &b);
        -- a;
        -- b;
        edges[i] = make_pair(a, b);
    }
    vector<int> ans = solve(n, edges);
    printf("%d\n", (int)ans.size());
    REP (i, ans.size()) {
        printf("%d%c", ans[i] + 1, i + 1 < ans.size() ? ' ' : '\n');
    }
    return 0;
}
Back to top page