Submission #766641

#TimeUsernameProblemLanguageResultExecution timeMemory
766641asdfgraceRailway (BOI17_railway)C++17
100 / 100
70 ms23228 KiB
#include <bits/stdc++.h>
using namespace std;
#define DEBUG(x) //x
#define A(x) DEBUG(assert(x))
#define PRINT(x) DEBUG(cerr << x)
#define PV(x) DEBUG(cerr << #x << " = " << x << '\n')
#define PV2(x) DEBUG(cerr << #x << " = " << x.first << ',' << x.second << '\n')
#define PAR(x) DEBUG(PRINT(#x << " = { "); for (auto y : x) PRINT(y << ' '); PRINT("}\n");)
#define PAR2(x) DEBUG(PRINT(#x << " = { "); for (auto [y, z] : x) PRINT(y << ',' << z << "  "); PRINT("}\n");)
typedef long long i64;
const int INF = 1000000007; //998244353;

struct S {
  int n, m, k, t;
  vector<vector<pair<int, int>>> edges;
  vector<vector<int>> anc;
  vector<int> dep, ord, pos, subtree, val, in;
  constexpr static int LOG = 20;

  void run() {
    cin >> n >> m >> k;
    edges.resize(n);
    for (int i = 1; i < n; ++i) {
      int a, b;
      cin >> a >> b;
      --a; --b;
      edges[a].push_back({b, i});
      edges[b].push_back({a, i});
    }
    anc.resize(LOG, vector<int>(n, 0));
    dep.resize(n, 0); subtree.resize(n, 1);
    pos.resize(n, 0); ord.resize(n, 0);
    in.resize(n, 0);
    t = 0;
    dfs(0);
    for (int jump = 1; jump < LOG; ++jump) {
      for (int i = 0; i < n; ++i) {
        anc[jump][i] = anc[jump - 1][anc[jump - 1][i]];
      }
    }
    PAR(in);
    
    val.resize(n, 0);
    while (m--) {
      int x; cin >> x;
      vector<int> nodes(x + 1);
      for (int i = 0; i < x; ++i) {
        cin >> nodes[i]; --nodes[i];
      }
      sort(nodes.begin(), nodes.begin() + x, [&](int a, int b) {
        return pos[a] < pos[b];
      });
      nodes[x] = nodes[0];
      PAR(nodes);
      for (int i = 0; i < x; ++i) {
        int lca = LCA(nodes[i], nodes[i + 1]);
        ++val[nodes[i]];
        ++val[nodes[i + 1]];
        PV(lca);
        val[lca] -= 2;
      }
      PAR(in);
    }
    PAR(val);
    dfs2(0);
    vector<int> ans;
    PAR(val);
    PV(k);
    PAR(in);
    for (int i = 1; i < n; ++i) {
      if (val[i] >= 2 * k) {
        ans.push_back(in[i]);
      }
    }
    sort(ans.begin(), ans.end());
    cout << ans.size() << '\n';
    for (int i = 0; i < ans.size(); ++i) {
      cout << ans[i] << (i + 1 == ans.size() ? '\n' : ' ');
    }
  }
  
  void dfs(int node) {
    pos[node] = t;
    ord[t++] = node;
    for (auto [next, id] : edges[node]) {
      if (next != anc[0][node]) {
        anc[0][next] = node;
        dep[next] = dep[node] + 1;
        in[next] = id;
        dfs(next);
        subtree[node] += subtree[next];
      }
    }
  }
  
  void dfs2(int node) {
    for (auto [next, id] : edges[node]) {
      if (next != anc[0][node]) {
        dfs2(next);
        val[node] += val[next];
      }
    }
  }
  
  int kth_anc(int k, int a) {
    for (int jump = LOG - 1; jump >= 0; --jump) {
      if (1 << jump <= k) {
        a = anc[jump][a];
        k -= 1 << jump;
      }
    }
    return a;
  }
  
  int LCA(int a, int b) {
    if (dep[a] < dep[b]) swap(a, b);
    a = kth_anc(dep[a] - dep[b], a);
    if (a == b) return a;
    for (int jump = LOG - 1; jump >= 0; --jump) {
      if (anc[jump][a] != anc[jump][b]) {
        a = anc[jump][a];
        b = anc[jump][b];
      }
    }
    return anc[0][a];
  }
};

int main() {
  ios::sync_with_stdio(0); cin.tie(0);
  auto sol = make_unique<S>();
  sol->run();
}


Compilation message (stderr)

railway.cpp: In member function 'void S::run()':
railway.cpp:77:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   77 |     for (int i = 0; i < ans.size(); ++i) {
      |                     ~~^~~~~~~~~~~~
railway.cpp:78:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |       cout << ans[i] << (i + 1 == ans.size() ? '\n' : ' ');
      |                          ~~~~~~^~~~~~~~~~~~~
#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...