제출 #606931

#제출 시각아이디문제언어결과실행 시간메모리
606931thezomb1eRailway (BOI17_railway)C++17
100 / 100
283 ms49468 KiB
//thatsramen

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define eb emplace_back
#define pb push_back
#define ft first
#define sd second
#define pi pair<int, int>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define dbg(...) dbg_out(__VA_ARGS__)

using ll = long long;
using ld = long double;
using namespace std;
using namespace __gnu_pbds;

//Constants
const ll INF = 5 * 1e18;
const int IINF = 2 * 1e9;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const ld PI = 3.14159265359;

//Templates
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) {return os << '(' << p.first << ", " << p.second << ')';}
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) {os << '['; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << ']';}
void dbg_out() {cerr << endl;}
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << H << ' '; dbg_out(T...); }
template<typename T> void mins(T& x, T y) {x = min(x, y);}
template<typename T> void maxs(T& x, T y) {x = max(x, y);}
template<typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> using omset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

//order_of_key(k): number of elements strictly less than k
//find_by_order(k): k-th element in the set

void setPrec() {cout << fixed << setprecision(15);}
void unsyncIO() {cin.tie(0)->sync_with_stdio(0);}
void setIn(string s) {freopen(s.c_str(), "r", stdin);}
void setOut(string s) {freopen(s.c_str(), "w", stdout);}
void setIO(string s = "") {
    unsyncIO(); setPrec();
    if(s.size()) setIn(s + ".in"), setOut(s + ".out");
}

// #define TEST_CASES

void solve() {
    int n, m, k;
    cin >> n >> m >> k;
    vector<vector<pi>> e(n + 5);
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        e[u].pb({v, i}); e[v].pb({u, i});
    }
    vector<int> par(n + 5), dep(n + 5, 0);
    function<void(int, int)> dfs_init = [&](int f, int p) {
        for (const auto &[to, id] : e[f]) {
            if (to == p) continue;
            dep[to] = dep[f] + 1;
            par[to] = f;
            dfs_init(to, f);
        }
    };
    dfs_init(1, -1);
    par[1] = 1;
    vector<vector<int>> jump(n + 5, vector<int> (20));
    for (int i = 1; i <= n; i++) {
        jump[i][0] = par[i];
    }
    for (int i = 1; i < 20; i++) {
        for (int j = 1; j <= n; j++) {
            jump[j][i] = jump[jump[j][i - 1]][i - 1];
        }
    }
    auto get = [&](int u, int v) -> int {
        if (dep[u] < dep[v]) swap(u, v);
        for (int i = 19; i >= 0; i--) {
            int to = jump[u][i];
            if (dep[to] >= dep[v]) {
                u = to;
            }
        }
        if (u == v) return u;
        for (int i = 19; i >= 0; i--) {
            if (jump[u][i] != jump[v][i]) {
                u = jump[u][i];
                v = jump[v][i];
            }
        }
        return par[u];
    };
    vector<int> val(n + 5, 0);
    vector<map<int, int>> comps(n + 5);
    for (int i = 0; i < m; i++) {
        int z; cin >> z;
        vector<int> cur(z);
        for (int j = 0; j < z; j++) {
            cin >> cur[j];
        }
        for (int j = 0; j < z - 1; j++) {
            int lca = get(cur[j], cur[j + 1]);
            comps[cur[j]][i]++;
            comps[cur[j + 1]][i]++;
            comps[lca][i] -= 2;
        }
    }
    vector<int> sz(n + 5), ans;
    function<void(int, int)> dfs_size = [&](int f, int p) {
        sz[f] = (int) comps[f].size();
        for (const auto &[to, id] : e[f]) {
            if (to == p) continue;
            dfs_size(to, f);
            sz[f] += sz[to];
        }
    };
    dfs_size(1, -1);
    auto merge = [&](int u, int v) {
        for (const auto &[x, y] : comps[v]) {
            comps[u][x] += y;
            if (comps[u][x] == 0) {
                comps[u].erase(x);
            }
        }
    };
    function<void(int, int)> dfs = [&](int f, int p) {
        int big = -1, big_id = -1;
        for (const auto &[to, id] : e[f]) {
            if (to == p) continue;
            if (big == -1 || sz[to] > sz[big]) 
                big = to, big_id = id;
        }
        if (big != -1) {
            dfs(big, f);
            if ((int) comps[big].size() >= k) 
                ans.pb(big_id + 1);
            
            swap(comps[big], comps[f]);
            merge(f, big);
        }
        for (const auto &[to, id] : e[f]) {
            if (to == p || to == big) continue;
            dfs(to, f);
            if ((int) comps[to].size() >= k) 
                ans.pb(id + 1);

            merge(f, to);
        }
        //dbg(f, comps[f]);
    };
    dfs(1, -1);
    cout << ans.size() << '\n';
    sort(all(ans));
    for (int x : ans) {
        cout << x << ' ';
    }
}

int main() {
    setIO();

    int tt = 1;
    #ifdef TEST_CASES
        cin >> tt;
    #endif

    while (tt--)
        solve();

    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

railway.cpp: In function 'void setIn(std::string)':
railway.cpp:44:30: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   44 | void setIn(string s) {freopen(s.c_str(), "r", stdin);}
      |                       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
railway.cpp: In function 'void setOut(std::string)':
railway.cpp:45:31: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   45 | void setOut(string s) {freopen(s.c_str(), "w", stdout);}
      |                        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#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...