QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#110370#1825. The King's GuardsNyaanAC ✓854ms9884kbC++2023.8kb2023-06-01 18:21:582023-06-01 18:22:00

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-01 18:22:00]
  • 评测
  • 测评结果:AC
  • 用时:854ms
  • 内存:9884kb
  • [2023-06-01 18:21:58]
  • 提交

answer

/**
 * date   : 2023-06-01 19:21:46
 * author : Nyaan
 */

#define NDEBUG

using namespace std;

// intrinstic
#include <immintrin.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <cfloat>
#include <chrono>
#include <cinttypes>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <tuple>
#include <type_traits>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

// utility

namespace Nyaan {
using ll = long long;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;

template <typename T>
using V = vector<T>;
template <typename T>
using VV = vector<vector<T>>;
using vi = vector<int>;
using vl = vector<long long>;
using vd = V<double>;
using vs = V<string>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<long long>>;

template <typename T, typename U>
struct P : pair<T, U> {
  template <typename... Args>
  P(Args... args) : pair<T, U>(args...) {}

  using pair<T, U>::first;
  using pair<T, U>::second;

  P &operator+=(const P &r) {
    first += r.first;
    second += r.second;
    return *this;
  }
  P &operator-=(const P &r) {
    first -= r.first;
    second -= r.second;
    return *this;
  }
  P &operator*=(const P &r) {
    first *= r.first;
    second *= r.second;
    return *this;
  }
  template <typename S>
  P &operator*=(const S &r) {
    first *= r, second *= r;
    return *this;
  }
  P operator+(const P &r) const { return P(*this) += r; }
  P operator-(const P &r) const { return P(*this) -= r; }
  P operator*(const P &r) const { return P(*this) *= r; }
  template <typename S>
  P operator*(const S &r) const {
    return P(*this) *= r;
  }
  P operator-() const { return P{-first, -second}; }
};

using pl = P<ll, ll>;
using pi = P<int, int>;
using vp = V<pl>;

constexpr int inf = 1001001001;
constexpr long long infLL = 4004004004004004004LL;

template <typename T>
int sz(const T &t) {
  return t.size();
}

template <typename T, typename U>
inline bool amin(T &x, U y) {
  return (y < x) ? (x = y, true) : false;
}
template <typename T, typename U>
inline bool amax(T &x, U y) {
  return (x < y) ? (x = y, true) : false;
}

template <typename T>
inline T Max(const vector<T> &v) {
  return *max_element(begin(v), end(v));
}
template <typename T>
inline T Min(const vector<T> &v) {
  return *min_element(begin(v), end(v));
}
template <typename T>
inline long long Sum(const vector<T> &v) {
  return accumulate(begin(v), end(v), 0LL);
}

template <typename T>
int lb(const vector<T> &v, const T &a) {
  return lower_bound(begin(v), end(v), a) - begin(v);
}
template <typename T>
int ub(const vector<T> &v, const T &a) {
  return upper_bound(begin(v), end(v), a) - begin(v);
}

constexpr long long TEN(int n) {
  long long ret = 1, x = 10;
  for (; n; x *= x, n >>= 1) ret *= (n & 1 ? x : 1);
  return ret;
}

template <typename T, typename U>
pair<T, U> mkp(const T &t, const U &u) {
  return make_pair(t, u);
}

template <typename T>
vector<T> mkrui(const vector<T> &v, bool rev = false) {
  vector<T> ret(v.size() + 1);
  if (rev) {
    for (int i = int(v.size()) - 1; i >= 0; i--) ret[i] = v[i] + ret[i + 1];
  } else {
    for (int i = 0; i < int(v.size()); i++) ret[i + 1] = ret[i] + v[i];
  }
  return ret;
};

template <typename T>
vector<T> mkuni(const vector<T> &v) {
  vector<T> ret(v);
  sort(ret.begin(), ret.end());
  ret.erase(unique(ret.begin(), ret.end()), ret.end());
  return ret;
}

template <typename F>
vector<int> mkord(int N,F f) {
  vector<int> ord(N);
  iota(begin(ord), end(ord), 0);
  sort(begin(ord), end(ord), f);
  return ord;
}

template <typename T>
vector<int> mkinv(vector<T> &v) {
  int max_val = *max_element(begin(v), end(v));
  vector<int> inv(max_val + 1, -1);
  for (int i = 0; i < (int)v.size(); i++) inv[v[i]] = i;
  return inv;
}

vector<int> mkiota(int n) {
  vector<int> ret(n);
  iota(begin(ret), end(ret), 0);
  return ret;
}

template <typename T>
T mkrev(const T &v) {
  T w{v};
  reverse(begin(w), end(w));
  return w;
}

template <typename T>
bool nxp(vector<T> &v) {
  return next_permutation(begin(v), end(v));
}

template <typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;

}  // namespace Nyaan


// bit operation

namespace Nyaan {
__attribute__((target("popcnt"))) inline int popcnt(const u64 &a) {
  return _mm_popcnt_u64(a);
}
inline int lsb(const u64 &a) { return a ? __builtin_ctzll(a) : 64; }
inline int ctz(const u64 &a) { return a ? __builtin_ctzll(a) : 64; }
inline int msb(const u64 &a) { return a ? 63 - __builtin_clzll(a) : -1; }
template <typename T>
inline int gbit(const T &a, int i) {
  return (a >> i) & 1;
}
template <typename T>
inline void sbit(T &a, int i, bool b) {
  if (gbit(a, i) != b) a ^= T(1) << i;
}
constexpr long long PW(int n) { return 1LL << n; }
constexpr long long MSK(int n) { return (1LL << n) - 1; }
}  // namespace Nyaan


// inout

namespace Nyaan {

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << p.first << " " << p.second;
  return os;
}
template <typename T, typename U>
istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  int s = (int)v.size();
  for (int i = 0; i < s; i++) os << (i ? " " : "") << v[i];
  return os;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (auto &x : v) is >> x;
  return is;
}

istream &operator>>(istream &is, __int128_t &x) {
  string S;
  is >> S;
  x = 0;
  int flag = 0;
  for (auto &c : S) {
    if (c == '-') {
      flag = true;
      continue;
    }
    x *= 10;
    x += c - '0';
  }
  if (flag) x = -x;
  return is;
}

istream &operator>>(istream &is, __uint128_t &x) {
  string S;
  is >> S;
  x = 0;
  for (auto &c : S) {
    x *= 10;
    x += c - '0';
  }
  return is;
}

ostream &operator<<(ostream &os, __int128_t x) {
  if (x == 0) return os << 0;
  if (x < 0) os << '-', x = -x;
  string S;
  while (x) S.push_back('0' + x % 10), x /= 10;
  reverse(begin(S), end(S));
  return os << S;
}
ostream &operator<<(ostream &os, __uint128_t x) {
  if (x == 0) return os << 0;
  string S;
  while (x) S.push_back('0' + x % 10), x /= 10;
  reverse(begin(S), end(S));
  return os << S;
}

void in() {}
template <typename T, class... U>
void in(T &t, U &...u) {
  cin >> t;
  in(u...);
}

void out() { cout << "\n"; }
template <typename T, class... U, char sep = ' '>
void out(const T &t, const U &...u) {
  cout << t;
  if (sizeof...(u)) cout << sep;
  out(u...);
}

struct IoSetupNya {
  IoSetupNya() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    cerr << fixed << setprecision(7);
  }
} iosetupnya;

}  // namespace Nyaan


// debug


#ifdef NyaanDebug
#define trc(...) (void(0))
#else
#define trc(...) (void(0))
#endif

#ifdef NyaanLocal
#define trc2(...) (void(0))
#else
#define trc2(...) (void(0))
#endif


// macro

#define each(x, v) for (auto&& x : v)
#define each2(x, y, v) for (auto&& [x, y] : v)
#define all(v) (v).begin(), (v).end()
#define rep(i, N) for (long long i = 0; i < (long long)(N); i++)
#define repr(i, N) for (long long i = (long long)(N)-1; i >= 0; i--)
#define rep1(i, N) for (long long i = 1; i <= (long long)(N); i++)
#define repr1(i, N) for (long long i = (N); (long long)(i) > 0; i--)
#define reg(i, a, b) for (long long i = (a); i < (b); i++)
#define regr(i, a, b) for (long long i = (b)-1; i >= (a); i--)
#define fi first
#define se second
#define ini(...)   \
  int __VA_ARGS__; \
  in(__VA_ARGS__)
#define inl(...)         \
  long long __VA_ARGS__; \
  in(__VA_ARGS__)
#define ins(...)      \
  string __VA_ARGS__; \
  in(__VA_ARGS__)
#define in2(s, t)                           \
  for (int i = 0; i < (int)s.size(); i++) { \
    in(s[i], t[i]);                         \
  }
#define in3(s, t, u)                        \
  for (int i = 0; i < (int)s.size(); i++) { \
    in(s[i], t[i], u[i]);                   \
  }
#define in4(s, t, u, v)                     \
  for (int i = 0; i < (int)s.size(); i++) { \
    in(s[i], t[i], u[i], v[i]);             \
  }
#define die(...)             \
  do {                       \
    Nyaan::out(__VA_ARGS__); \
    return;                  \
  } while (0)


namespace Nyaan {
void solve();
}
int main() { Nyaan::solve(); }


//


struct RollbackUnionFind {
  vector<int> data;
  stack<pair<int, int> > history;
  int inner_snap;

  RollbackUnionFind(int sz) : inner_snap(0) { data.assign(sz, -1); }

  bool unite(int x, int y) {
    x = find(x), y = find(y);
    history.emplace(x, data[x]);
    history.emplace(y, data[y]);
    if (x == y) return false;
    if (data[x] > data[y]) swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return true;
  }

  int find(int k) {
    if (data[k] < 0) return k;
    return find(data[k]);
  }

  int same(int x, int y) { return find(x) == find(y); }

  int size(int k) { return (-data[find(k)]); }

  void undo() {
    data[history.top().first] = history.top().second;
    history.pop();
    data[history.top().first] = history.top().second;
    history.pop();
  }

  void snapshot() { inner_snap = int(history.size() >> 1); }

  int get_state() { return int(history.size() >> 1); }

  void rollback(int state = -1) {
    if (state == -1) state = inner_snap;
    state <<= 1;
    assert(state <= (int)history.size());
    while (state < (int)history.size()) undo();
  }
};

/**
 * @brief RollbackつきUnion Find
 * @docs docs/data-structure/rollback-union-find.md
 */












namespace atcoder {

namespace internal {

template <class T> struct simple_queue {
    std::vector<T> payload;
    int pos = 0;
    void reserve(int n) { payload.reserve(n); }
    int size() const { return int(payload.size()) - pos; }
    bool empty() const { return pos == int(payload.size()); }
    void push(const T& t) { payload.push_back(t); }
    T& front() { return payload[pos]; }
    void clear() {
        payload.clear();
        pos = 0;
    }
    void pop() { pos++; }
};

}  // namespace internal

}  // namespace atcoder



namespace atcoder {

template <class Cap> struct mf_graph {
  public:
    mf_graph() : _n(0) {}
    mf_graph(int n) : _n(n), g(n) {}

    virtual int add_edge(int from, int to, Cap cap) {
        assert(0 <= from && from < _n);
        assert(0 <= to && to < _n);
        assert(0 <= cap);
        int m = int(pos.size());
        pos.push_back({from, int(g[from].size())});
        int from_id = int(g[from].size());
        int to_id = int(g[to].size());
        if (from == to) to_id++;
        g[from].push_back(_edge{to, to_id, cap});
        g[to].push_back(_edge{from, from_id, 0});
        return m;
    }

    struct edge {
        int from, to;
        Cap cap, flow;
    };

    edge get_edge(int i) {
        int m = int(pos.size());
        assert(0 <= i && i < m);
        auto _e = g[pos[i].first][pos[i].second];
        auto _re = g[_e.to][_e.rev];
        return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap};
    }
    std::vector<edge> edges() {
        int m = int(pos.size());
        std::vector<edge> result;
        for (int i = 0; i < m; i++) {
            result.push_back(get_edge(i));
        }
        return result;
    }
    void change_edge(int i, Cap new_cap, Cap new_flow) {
        int m = int(pos.size());
        assert(0 <= i && i < m);
        assert(0 <= new_flow && new_flow <= new_cap);
        auto& _e = g[pos[i].first][pos[i].second];
        auto& _re = g[_e.to][_e.rev];
        _e.cap = new_cap - new_flow;
        _re.cap = new_flow;
    }

    Cap flow(int s, int t) {
        return flow(s, t, std::numeric_limits<Cap>::max());
    }
    Cap flow(int s, int t, Cap flow_limit) {
        assert(0 <= s && s < _n);
        assert(0 <= t && t < _n);
        assert(s != t);

        std::vector<int> level(_n), iter(_n);
        internal::simple_queue<int> que;

        auto bfs = [&]() {
            std::fill(level.begin(), level.end(), -1);
            level[s] = 0;
            que.clear();
            que.push(s);
            while (!que.empty()) {
                int v = que.front();
                que.pop();
                for (auto e : g[v]) {
                    if (e.cap == 0 || level[e.to] >= 0) continue;
                    level[e.to] = level[v] + 1;
                    if (e.to == t) return;
                    que.push(e.to);
                }
            }
        };
        auto dfs = [&](auto self, int v, Cap up) {
            if (v == s) return up;
            Cap res = 0;
            int level_v = level[v];
            for (int& i = iter[v]; i < int(g[v].size()); i++) {
                _edge& e = g[v][i];
                if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue;
                Cap d =
                    self(self, e.to, std::min(up - res, g[e.to][e.rev].cap));
                if (d <= 0) continue;
                g[v][i].cap += d;
                g[e.to][e.rev].cap -= d;
                res += d;
                if (res == up) return res;
            }
            level[v] = _n;
            return res;
        };

        Cap flow = 0;
        while (flow < flow_limit) {
            bfs();
            if (level[t] == -1) break;
            std::fill(iter.begin(), iter.end(), 0);
            Cap f = dfs(dfs, t, flow_limit - flow);
            if (!f) break;
            flow += f;
        }
        return flow;
    }

    std::vector<bool> min_cut(int s) {
        std::vector<bool> visited(_n);
        internal::simple_queue<int> que;
        que.push(s);
        while (!que.empty()) {
            int p = que.front();
            que.pop();
            visited[p] = true;
            for (auto e : g[p]) {
                if (e.cap && !visited[e.to]) {
                    visited[e.to] = true;
                    que.push(e.to);
                }
            }
        }
        return visited;
    }

  private:
    int _n;
    struct _edge {
        int to, rev;
        Cap cap;
    };
    std::vector<std::pair<int, int>> pos;
    std::vector<std::vector<_edge>> g;
};

}  // namespace atcoder



namespace BipartiteGraphImpl {
using namespace atcoder;
struct BipartiteGraph : mf_graph<long long> {
  int L, R, s, t;
  bool is_flow;

  explicit BipartiteGraph(int N, int M)
      : mf_graph<long long>(N + M + 2),
        L(N),
        R(M),
        s(N + M),
        t(N + M + 1),
        is_flow(false) {
    for (int i = 0; i < L; i++) mf_graph<long long>::add_edge(s, i, 1);
    for (int i = 0; i < R; i++) mf_graph<long long>::add_edge(i + L, t, 1);
  }

  int add_edge(int n, int m, long long cap = 1) override {
    assert(0 <= n && n < L);
    assert(0 <= m && m < R);
    return mf_graph<long long>::add_edge(n, m + L, cap);
  }

  long long flow() {
    is_flow = true;
    return mf_graph<long long>::flow(s, t);
  }

  vector<pair<int, int>> MaximumMatching() {
    if (!is_flow) flow();
    auto es = mf_graph<long long>::edges();
    vector<pair<int, int>> ret;
    for (auto &e : es) {
      if (e.flow > 0 && e.from != s && e.to != t) {
        ret.emplace_back(e.from, e.to - L);
      }
    }
    return ret;
  }

  // call after calclating flow !
  pair<vector<int>, vector<int>> MinimumVertexCover() {
    if (!is_flow) flow();
    auto colored = PreCalc();
    vector<int> nl, nr;
    for (int i = 0; i < L; i++)
      if (!colored[i]) nl.push_back(i);
    for (int i = 0; i < R; i++)
      if (colored[i + L]) nr.push_back(i);
    return make_pair(nl, nr);
  }

  // call after calclating flow !
  pair<vector<int>, vector<int>> MaximumIndependentSet() {
    if (!is_flow) flow();
    auto colored = PreCalc();
    vector<int> nl, nr;
    for (int i = 0; i < L; i++)
      if (colored[i]) nl.push_back(i);
    for (int i = 0; i < R; i++)
      if (!colored[i + L]) nr.push_back(i);
    return make_pair(nl, nr);
  }

  vector<pair<int, int>> MinimumEdgeCover() {
    if (!is_flow) flow();
    auto es = MaximumMatching();
    vector<bool> useL(L), useR(R);
    for (auto &p : es) {
      useL[p.first] = true;
      useR[p.second] = true;
    }
    for (auto &e : mf_graph<long long>::edges()) {
      if (e.flow > 0 || e.from == s || e.to == t) continue;
      if (useL[e.from] == false || useR[e.to - L] == false) {
        es.emplace_back(e.from, e.to - L);
        useL[e.from] = useR[e.to - L] = true;
      }
    }
    return es;
  }

 private:
  vector<bool> PreCalc() {
    vector<vector<int>> ag(L + R);
    vector<bool> used(L, false);
    for (auto &e : mf_graph<long long>::edges()) {
      if (e.from == s || e.to == t) continue;
      if (e.flow > 0) {
        ag[e.to].push_back(e.from);
        used[e.from] = true;
      } else {
        ag[e.from].push_back(e.to);
      }
    }
    vector<bool> colored(L + R, false);
    auto dfs = [&](auto rc, int cur) -> void {
      for (auto &d : ag[cur]) {
        if (!colored[d]) colored[d] = true, rc(rc, d);
      }
    };
    for (int i = 0; i < L; i++)
      if (!used[i]) colored[i] = true, dfs(dfs, i);
    return colored;
  }
};

}  // namespace BipartiteGraphImpl

using BipartiteGraphImpl::BipartiteGraph;

/**
 * @brief 二部グラフのフロー
 * @docs docs/flow/flow-on-bipartite-graph.md
 */






template <typename T>
struct edge {
  int src, to;
  T cost;

  edge(int _to, T _cost) : src(-1), to(_to), cost(_cost) {}
  edge(int _src, int _to, T _cost) : src(_src), to(_to), cost(_cost) {}

  edge &operator=(const int &x) {
    to = x;
    return *this;
  }

  operator int() const { return to; }
};
template <typename T>
using Edges = vector<edge<T>>;
template <typename T>
using WeightedGraph = vector<Edges<T>>;
using UnweightedGraph = vector<vector<int>>;

// Input of (Unweighted) Graph
UnweightedGraph graph(int N, int M = -1, bool is_directed = false,
                      bool is_1origin = true) {
  UnweightedGraph g(N);
  if (M == -1) M = N - 1;
  for (int _ = 0; _ < M; _++) {
    int x, y;
    cin >> x >> y;
    if (is_1origin) x--, y--;
    g[x].push_back(y);
    if (!is_directed) g[y].push_back(x);
  }
  return g;
}

// Input of Weighted Graph
template <typename T>
WeightedGraph<T> wgraph(int N, int M = -1, bool is_directed = false,
                        bool is_1origin = true) {
  WeightedGraph<T> g(N);
  if (M == -1) M = N - 1;
  for (int _ = 0; _ < M; _++) {
    int x, y;
    cin >> x >> y;
    T c;
    cin >> c;
    if (is_1origin) x--, y--;
    g[x].emplace_back(x, y, c);
    if (!is_directed) g[y].emplace_back(y, x, c);
  }
  return g;
}

// Input of Edges
template <typename T>
Edges<T> esgraph(int N, int M, int is_weighted = true, bool is_1origin = true) {
  Edges<T> es;
  for (int _ = 0; _ < M; _++) {
    int x, y;
    cin >> x >> y;
    T c;
    if (is_weighted)
      cin >> c;
    else
      c = 1;
    if (is_1origin) x--, y--;
    es.emplace_back(x, y, c);
  }
  return es;
}

// Input of Adjacency Matrix
template <typename T>
vector<vector<T>> adjgraph(int N, int M, T INF, int is_weighted = true,
                           bool is_directed = false, bool is_1origin = true) {
  vector<vector<T>> d(N, vector<T>(N, INF));
  for (int _ = 0; _ < M; _++) {
    int x, y;
    cin >> x >> y;
    T c;
    if (is_weighted)
      cin >> c;
    else
      c = 1;
    if (is_1origin) x--, y--;
    d[x][y] = c;
    if (!is_directed) d[y][x] = c;
  }
  return d;
}

/**
 * @brief グラフテンプレート
 * @docs docs/graph/graph-template.md
 */


// 一般のグラフのstからの距離!!!!
// unvisited nodes : d = -1
vector<int> Depth(const UnweightedGraph &g, int start = 0) {
  int n = g.size();
  vector<int> ds(n, -1);
  ds[start] = 0;
  queue<int> q;
  q.push(start);
  while (!q.empty()) {
    int c = q.front();
    q.pop();
    int dc = ds[c];
    for (auto &d : g[c]) {
      if (ds[d] == -1) {
        ds[d] = dc + 1;
        q.push(d);
      }
    }
  }
  return ds;
}

// Depth of Rooted Weighted Tree
// unvisited nodes : d = -1
template <typename T>
vector<T> Depth(const WeightedGraph<T> &g, int start = 0) {
  vector<T> d(g.size(), -1);
  auto dfs = [&](auto rec, int cur, T val, int par = -1) -> void {
    d[cur] = val;
    for (auto &dst : g[cur]) {
      if (dst == par) continue;
      rec(rec, dst, val + dst.cost, cur);
    }
  };
  dfs(dfs, start, 0);
  return d;
}

// Diameter of Tree
// return value : { {u, v}, length }
pair<pair<int, int>, int> Diameter(const UnweightedGraph &g) {
  auto d = Depth(g, 0);
  int u = max_element(begin(d), end(d)) - begin(d);
  d = Depth(g, u);
  int v = max_element(begin(d), end(d)) - begin(d);
  return make_pair(make_pair(u, v), d[v]);
}

// Diameter of Weighted Tree
// return value : { {u, v}, length }
template <typename T>
pair<pair<int, int>, T> Diameter(const WeightedGraph<T> &g) {
  auto d = Depth(g, 0);
  int u = max_element(begin(d), end(d)) - begin(d);
  d = Depth(g, u);
  int v = max_element(begin(d), end(d)) - begin(d);
  return make_pair(make_pair(u, v), d[v]);
}

// nodes on the path u-v ( O(N) )
template <typename G>
vector<int> Path(G &g, int u, int v) {
  vector<int> ret;
  int end = 0;
  auto dfs = [&](auto rec, int cur, int par = -1) -> void {
    ret.push_back(cur);
    if (cur == v) {
      end = 1;
      return;
    }
    for (int dst : g[cur]) {
      if (dst == par) continue;
      rec(rec, dst, cur);
      if (end) return;
    }
    if (end) return;
    ret.pop_back();
  };
  dfs(dfs, u);
  return ret;
}

/**
 * @brief グラフユーティリティ
 * @docs docs/graph/graph-utility.md
 */






struct UnionFind {
  vector<int> data;
  UnionFind(int N) : data(N, -1) {}

  int find(int k) { return data[k] < 0 ? k : data[k] = find(data[k]); }

  int unite(int x, int y) {
    if ((x = find(x)) == (y = find(y))) return false;
    if (data[x] > data[y]) swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return true;
  }

  // f ... merge function
  template<typename F>
  int unite(int x, int y,const F &f) {
    if ((x = find(x)) == (y = find(y))) return false;
    if (data[x] > data[y]) swap(x, y);
    data[x] += data[y];
    data[y] = x;
    f(x, y);
    return true;
  }

  int size(int k) { return -data[find(k)]; }

  int same(int x, int y) { return find(x) == find(y); }
};

/**
 * @brief Union Find(Disjoint Set Union)
 * @docs docs/data-structure/union-find.md
 */


template <typename T>
T kruskal(int N, Edges<T> &es) {
  sort(begin(es), end(es),
       [&](edge<T> a, edge<T> b) { return a.cost < b.cost; });
  UnionFind uf(N);
  T ret = 0;
  for (edge<T> &e : es) {
    if (uf.same(e.src, e.to)) continue;
    ret += e.cost;
    uf.unite(e.src, e.to);
  }
  return ret;
}

using namespace Nyaan;

void q() {
  inl(N, M, G);
  Edges<ll> es;

  rep(i, M) {
    inl(u, v, w);
    --u, --v;
    es.emplace_back(u, v, w);
  }
  vvi v;
  rep(i, G) {
    ini(n);
    vi a(n);
    in(a);
    each(x, a)-- x;
    v.push_back(a);
  }
  sort(all(es), [](auto& e1, auto& e2) { return e1.cost < e2.cost; });

  {
    RollbackUnionFind uf(N);
    Edges<ll> nes;
    each(e, es) {
      if (uf.same(e.src, e.to)) {
        continue;
      }
      nes.push_back(e);
      uf.unite(e.src, e.to);
    }
    es = nes;
  }

  {
    BipartiteGraph flow(G, N);
    rep(i, G) each(j, v[i]) flow.add_edge(i, j);
    ll m = flow.flow();
    if (m != G) die(-1);
  }

  RollbackUnionFind uf(N);
  ll ans = 0, cc = N;
  each(e, es) {
    uf.unite(e.src, e.to);
    BipartiteGraph flow(G, N);
    rep(i, G) each(j, v[i]) flow.add_edge(i, uf.find(j));
    ll m = flow.flow();
    if (m == G) {
      ans += e.cost, cc--;
    } else {
      uf.undo();
    }
  }
  out(cc == G ? ans : -1);
}

void Nyaan::solve() {
  int t = 1;
  // in(t);
  while (t--) q();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3440kb

input:

5 6 2
1 2 1
1 3 4
2 4 2
2 5 5
3 4 7
4 5 3
2 1 2
2 2 4

output:

8

result:

ok answer is '8'

Test #2:

score: 0
Accepted
time: 2ms
memory: 3464kb

input:

10 19 6
1 5 761
6 8 606
3 9 648
2 4 115
5 8 814
1 2 712
4 10 13
5 10 797
3 4 956
1 7 73
5 7 192
2 7 110
5 9 302
3 6 120
6 9 494
1 3 668
3 7 966
6 10 974
3 8 41
2 10 5
3 6 4 3
2 1 7
2 10 8
3 10 7 8
2 2 1

output:

429

result:

ok answer is '429'

Test #3:

score: 0
Accepted
time: 2ms
memory: 3448kb

input:

10 43 3
1 3 656
2 6 856
4 10 99
5 6 900
2 7 766
4 7 582
2 8 135
5 7 831
3 5 12
3 10 789
1 8 66
4 9 390
1 7 238
6 7 960
1 4 624
3 9 602
7 10 366
5 8 526
2 9 561
6 10 722
2 5 904
3 4 35
1 9 768
5 9 457
6 8 61
4 6 192
4 5 96
5 10 747
8 9 611
7 8 953
3 8 449
2 4 228
1 6 197
9 10 160
3 6 869
1 2 785
4 8 ...

output:

526

result:

ok answer is '526'

Test #4:

score: 0
Accepted
time: 16ms
memory: 3636kb

input:

277 9038 1
226 260 740
44 226 376
151 263 611
67 269 241
120 181 677
259 271 782
37 52 310
48 152 452
168 266 823
85 234 100
46 201 738
129 153 301
69 147 434
13 72 764
13 234 316
171 222 398
214 255 21
112 158 430
20 118 407
45 152 971
205 214 272
221 275 362
198 268 472
117 176 207
31 75 652
139 1...

output:

5375

result:

ok answer is '5375'

Test #5:

score: 0
Accepted
time: 47ms
memory: 3768kb

input:

297 27966 132
15 197 980
226 259 950
161 168 142
118 176 834
157 221 806
24 210 432
212 242 838
110 166 177
78 170 801
52 166 3
89 213 448
45 170 626
250 251 268
93 222 679
7 128 839
5 7 320
132 191 1
192 295 717
36 231 542
162 175 508
173 178 458
211 272 926
46 168 145
19 150 805
165 262 198
50 179...

output:

775

result:

ok answer is '775'

Test #6:

score: 0
Accepted
time: 2ms
memory: 3432kb

input:

7 7 4
1 3 7
1 4 6
2 3 5
2 4 6
4 5 10
4 6 10
4 7 10
5 4 3 2 7 5
6 5 2 6 7 4 1
2 2 3
6 7 2 5 3 1 6

output:

17

result:

ok answer is '17'

Test #7:

score: 0
Accepted
time: 396ms
memory: 6600kb

input:

300 44850 299
1 2 1
1 3 1
1 4 1
1 5 1
1 6 1
1 7 1
1 8 1
1 9 1
1 10 1
1 11 1
1 12 1
1 13 1
1 14 1
1 15 1
1 16 1
1 17 1
1 18 1
1 19 1
1 20 1
1 21 1
1 22 1
1 23 1
1 24 1
1 25 1
1 26 1
1 27 1
1 28 1
1 29 1
1 30 1
1 31 1
1 32 1
1 33 1
1 34 1
1 35 1
1 36 1
1 37 1
1 38 1
1 39 1
1 40 1
1 41 1
1 42 1
1 43 1
...

output:

1000

result:

ok answer is '1000'

Test #8:

score: 0
Accepted
time: 854ms
memory: 9884kb

input:

300 44850 299
1 2 1
1 3 1
1 4 1
1 5 1
1 6 1
1 7 1
1 8 1
1 9 1
1 10 1
1 11 1
1 12 1
1 13 1
1 14 1
1 15 1
1 16 1
1 17 1
1 18 1
1 19 1
1 20 1
1 21 1
1 22 1
1 23 1
1 24 1
1 25 1
1 26 1
1 27 1
1 28 1
1 29 1
1 30 1
1 31 1
1 32 1
1 33 1
1 34 1
1 35 1
1 36 1
1 37 1
1 38 1
1 39 1
1 40 1
1 41 1
1 42 1
1 43 1
...

output:

1000

result:

ok answer is '1000'

Test #9:

score: 0
Accepted
time: 270ms
memory: 5420kb

input:

300 44850 150
1 2 4
1 3 4
1 4 4
1 5 3
1 6 10
1 7 4
1 8 8
1 9 5
1 10 4
1 11 9
1 12 9
1 13 1
1 14 3
1 15 5
1 16 7
1 17 5
1 18 2
1 19 6
1 20 4
1 21 10
1 22 5
1 23 7
1 24 2
1 25 2
1 26 4
1 27 4
1 28 5
1 29 8
1 30 10
1 31 9
1 32 1
1 33 7
1 34 5
1 35 4
1 36 6
1 37 8
1 38 1
1 39 2
1 40 1
1 41 1
1 42 1
1 43...

output:

249

result:

ok answer is '249'

Test #10:

score: 0
Accepted
time: 258ms
memory: 5480kb

input:

300 44850 150
1 2 70
1 3 26
1 4 76
1 5 74
1 6 98
1 7 72
1 8 66
1 9 25
1 10 36
1 11 57
1 12 8
1 13 88
1 14 98
1 15 33
1 16 85
1 17 56
1 18 16
1 19 62
1 20 41
1 21 81
1 22 18
1 23 15
1 24 69
1 25 11
1 26 29
1 27 62
1 28 64
1 29 41
1 30 92
1 31 29
1 32 99
1 33 40
1 34 30
1 35 23
1 36 49
1 37 6
1 38 84
...

output:

1299

result:

ok answer is '1299'

Test #11:

score: 0
Accepted
time: 396ms
memory: 6376kb

input:

300 44850 150
1 2 3
1 3 2
1 4 20
1 5 77
1 6 77
1 7 23
1 8 39
1 9 57
1 10 83
1 11 60
1 12 6
1 13 78
1 14 64
1 15 62
1 16 41
1 17 88
1 18 77
1 19 58
1 20 39
1 21 18
1 22 43
1 23 26
1 24 40
1 25 61
1 26 23
1 27 6
1 28 47
1 29 100
1 30 59
1 31 92
1 32 60
1 33 13
1 34 57
1 35 55
1 36 99
1 37 77
1 38 63
1...

output:

1310

result:

ok answer is '1310'

Test #12:

score: 0
Accepted
time: 74ms
memory: 4096kb

input:

300 44551 299
1 2 1
1 3 1
1 4 1
1 5 1
1 6 1
1 7 1
1 8 1
1 9 1
1 10 1
1 11 1
1 12 1
1 13 1
1 14 1
1 15 1
1 16 1
1 17 1
1 18 1
1 19 1
1 20 1
1 21 1
1 22 1
1 23 1
1 24 1
1 25 1
1 26 1
1 27 1
1 28 1
1 29 1
1 30 1
1 31 1
1 32 1
1 33 1
1 34 1
1 35 1
1 36 1
1 37 1
1 38 1
1 39 1
1 40 1
1 41 1
1 42 1
1 43 1
...

output:

-1

result:

ok answer is '-1'

Test #13:

score: 0
Accepted
time: 70ms
memory: 4160kb

input:

300 44552 299
1 2 1
1 3 1
1 4 1
1 5 1
1 6 1
1 7 1
1 8 1
1 9 1
1 10 1
1 11 1
1 12 1
1 13 1
1 14 1
1 15 1
1 16 1
1 17 1
1 18 1
1 19 1
1 20 1
1 21 1
1 22 1
1 23 1
1 24 1
1 25 1
1 26 1
1 27 1
1 28 1
1 29 1
1 30 1
1 31 1
1 32 1
1 33 1
1 34 1
1 35 1
1 36 1
1 37 1
1 38 1
1 39 1
1 40 1
1 41 1
1 42 1
1 43 1
...

output:

2

result:

ok answer is '2'

Test #14:

score: 0
Accepted
time: 4ms
memory: 4068kb

input:

300 44552 300
1 2 1
1 3 1
1 4 1
1 5 1
1 6 1
1 7 1
1 8 1
1 9 1
1 10 1
1 11 1
1 12 1
1 13 1
1 14 1
1 15 1
1 16 1
1 17 1
1 18 1
1 19 1
1 20 1
1 21 1
1 22 1
1 23 1
1 24 1
1 25 1
1 26 1
1 27 1
1 28 1
1 29 1
1 30 1
1 31 1
1 32 1
1 33 1
1 34 1
1 35 1
1 36 1
1 37 1
1 38 1
1 39 1
1 40 1
1 41 1
1 42 1
1 43 1
...

output:

-1

result:

ok answer is '-1'

Test #15:

score: 0
Accepted
time: 36ms
memory: 4156kb

input:

300 44850 299
1 2 124
1 3 199
1 4 812
1 5 959
1 6 760
1 7 501
1 8 467
1 9 351
1 10 495
1 11 117
1 12 123
1 13 309
1 14 722
1 15 926
1 16 532
1 17 94
1 18 807
1 19 865
1 20 139
1 21 887
1 22 147
1 23 55
1 24 214
1 25 900
1 26 301
1 27 834
1 28 523
1 29 375
1 30 187
1 31 851
1 32 120
1 33 763
1 34 859...

output:

1

result:

ok answer is '1'

Test #16:

score: 0
Accepted
time: 38ms
memory: 4160kb

input:

300 44850 300
1 2 308
1 3 313
1 4 997
1 5 3
1 6 741
1 7 653
1 8 653
1 9 235
1 10 788
1 11 215
1 12 982
1 13 346
1 14 444
1 15 721
1 16 855
1 17 348
1 18 549
1 19 355
1 20 836
1 21 53
1 22 207
1 23 944
1 24 880
1 25 569
1 26 951
1 27 923
1 28 511
1 29 47
1 30 506
1 31 187
1 32 934
1 33 777
1 34 652
1...

output:

0

result:

ok answer is '0'

Test #17:

score: 0
Accepted
time: 454ms
memory: 6984kb

input:

300 44850 300
1 2 226
1 3 881
1 4 502
1 5 213
1 6 52
1 7 177
1 8 946
1 9 798
1 10 423
1 11 485
1 12 498
1 13 368
1 14 879
1 15 309
1 16 24
1 17 642
1 18 698
1 19 617
1 20 279
1 21 918
1 22 303
1 23 385
1 24 879
1 25 489
1 26 345
1 27 591
1 28 536
1 29 817
1 30 342
1 31 91
1 32 232
1 33 572
1 34 968
...

output:

0

result:

ok answer is '0'

Test #18:

score: 0
Accepted
time: 418ms
memory: 6976kb

input:

300 44850 300
1 2 433
1 3 780
1 4 144
1 5 734
1 6 131
1 7 867
1 8 980
1 9 901
1 10 226
1 11 386
1 12 44
1 13 851
1 14 740
1 15 924
1 16 218
1 17 895
1 18 156
1 19 695
1 20 491
1 21 375
1 22 171
1 23 460
1 24 308
1 25 59
1 26 483
1 27 494
1 28 495
1 29 859
1 30 483
1 31 347
1 32 343
1 33 743
1 34 734...

output:

0

result:

ok answer is '0'

Test #19:

score: 0
Accepted
time: 83ms
memory: 4116kb

input:

300 44313 219
81 124 61
95 185 213
123 238 928
171 206 319
32 173 62
16 102 979
111 222 360
52 223 579
81 139 955
25 181 959
9 295 276
90 254 725
130 132 990
231 259 912
216 218 789
149 195 575
204 211 885
39 100 762
7 233 775
54 116 500
31 283 84
31 189 387
101 140 278
61 227 587
38 97 30
115 237 3...

output:

106

result:

ok answer is '106'

Test #20:

score: 0
Accepted
time: 88ms
memory: 4064kb

input:

300 44614 243
59 192 809
114 169 523
55 270 698
127 128 619
210 288 64
165 184 539
158 200 385
50 277 590
151 226 964
9 41 37
37 251 870
222 231 98
105 120 558
2 156 18
164 184 412
120 199 11
157 183 595
143 146 383
190 240 577
61 163 695
46 85 107
92 299 851
231 251 780
4 114 752
21 67 218
70 166 3...

output:

63

result:

ok answer is '63'

Test #21:

score: 0
Accepted
time: 35ms
memory: 4132kb

input:

300 43263 44
172 220 779
60 269 398
264 283 307
115 235 895
140 175 125
113 257 841
155 262 979
188 240 202
59 243 155
219 230 326
229 247 793
19 142 783
161 206 203
26 108 671
51 93 417
22 195 471
175 203 143
126 266 568
14 210 466
39 251 65
145 151 566
69 118 667
110 268 109
51 122 889
5 156 472
1...

output:

1017

result:

ok answer is '1017'

Test #22:

score: 0
Accepted
time: 64ms
memory: 4168kb

input:

300 36802 149
79 150 325
1 254 817
13 285 630
153 275 180
61 276 135
68 187 68
271 285 391
10 164 987
34 144 700
64 197 429
87 206 326
106 179 613
11 263 100
22 180 272
44 69 424
66 128 796
123 265 287
254 270 663
52 56 8
4 210 107
146 238 680
151 238 313
134 241 954
82 135 924
191 273 400
116 145 1...

output:

419

result:

ok answer is '419'

Test #23:

score: 0
Accepted
time: 33ms
memory: 4156kb

input:

300 40463 41
128 189 463
50 96 453
29 209 998
13 77 971
80 207 976
145 256 634
17 36 703
6 49 159
124 199 6
121 156 33
30 37 276
118 266 598
266 284 488
212 224 418
267 279 305
150 281 794
269 290 60
57 260 971
121 193 948
32 227 531
26 127 683
9 194 806
147 299 568
121 155 751
97 273 525
33 239 729...

output:

1085

result:

ok answer is '1085'

Test #24:

score: 0
Accepted
time: 64ms
memory: 4160kb

input:

300 39338 137
5 144 55
30 266 119
49 162 697
139 163 458
57 246 798
55 291 712
32 212 941
106 203 194
75 177 813
6 147 163
41 53 780
15 254 524
64 141 2
97 248 834
2 270 407
293 299 511
139 278 720
81 254 968
54 255 692
226 291 31
44 265 922
60 229 72
47 229 343
112 157 893
101 166 927
92 264 392
21...

output:

470

result:

ok answer is '470'

Test #25:

score: 0
Accepted
time: 89ms
memory: 3788kb

input:

300 1978 261
15 180 594
42 91 394
71 212 11
79 281 531
61 70 169
80 258 921
85 173 632
48 142 543
34 66 483
71 238 391
7 25 309
108 297 333
98 252 990
206 222 140
9 275 58
49 186 601
222 265 566
15 109 74
198 286 32
49 258 658
200 226 142
52 123 566
86 279 989
20 241 794
129 297 367
132 154 91
112 1...

output:

663

result:

ok answer is '663'

Test #26:

score: 0
Accepted
time: 73ms
memory: 3680kb

input:

300 11083 192
54 112 651
211 236 605
198 291 978
28 145 728
109 111 818
180 273 77
25 106 921
246 261 2
170 174 915
11 173 783
51 57 90
69 111 263
92 189 834
75 298 433
204 276 783
5 239 52
3 240 996
57 262 866
81 264 50
97 120 161
43 247 270
40 235 813
202 211 40
89 219 969
193 284 442
174 284 865
...

output:

571

result:

ok answer is '571'

Test #27:

score: 0
Accepted
time: 92ms
memory: 4096kb

input:

300 43393 259
10 33 464
150 276 306
67 146 269
63 121 168
145 256 855
119 144 281
126 295 83
95 100 89
23 68 853
12 179 61
236 247 177
60 84 335
165 233 247
103 216 768
91 208 598
80 275 458
85 123 197
174 240 170
110 162 703
44 259 526
174 264 503
111 228 214
201 286 732
98 295 892
89 154 458
69 79...

output:

41

result:

ok answer is '41'

Test #28:

score: 0
Accepted
time: 69ms
memory: 3828kb

input:

300 32631 189
56 151 813
160 200 594
89 268 193
75 239 670
108 208 531
60 124 55
56 275 736
16 121 266
93 266 388
60 136 474
8 25 832
107 187 27
60 130 803
180 260 601
48 149 218
100 215 874
16 143 925
83 167 264
46 141 241
113 139 970
149 280 359
64 194 146
20 23 630
211 235 327
108 227 446
76 277 ...

output:

259

result:

ok answer is '259'

Test #29:

score: 0
Accepted
time: 57ms
memory: 3692kb

input:

300 27256 143
159 214 293
116 222 263
96 174 561
39 107 296
92 280 613
201 210 918
140 273 76
130 217 733
117 159 337
161 215 865
13 16 653
28 189 259
11 62 692
70 91 610
23 148 94
3 161 818
114 134 864
30 235 637
30 102 205
24 200 698
167 202 873
194 203 773
260 299 426
71 291 477
104 183 1
184 293...

output:

505

result:

ok answer is '505'

Test #30:

score: 0
Accepted
time: 63ms
memory: 4076kb

input:

300 44078 122
103 277 508
50 233 845
42 159 325
120 147 280
209 215 134
9 189 965
229 259 275
218 288 633
73 119 351
116 272 472
76 82 888
206 269 569
37 49 799
132 157 324
129 237 914
227 289 992
92 262 174
155 293 887
106 131 936
60 171 436
48 88 675
196 254 595
145 251 230
104 119 117
104 105 598...

output:

438

result:

ok answer is '438'

Test #31:

score: 0
Accepted
time: 65ms
memory: 4132kb

input:

300 33108 152
157 275 474
46 111 257
56 287 318
66 182 332
16 150 757
63 85 57
55 79 714
126 217 211
30 280 847
53 57 303
163 169 285
129 293 527
249 285 567
192 249 966
233 274 296
17 34 255
5 200 314
11 235 270
170 203 697
62 190 36
117 285 414
39 239 593
93 235 560
178 240 49
13 97 654
214 264 33...

output:

457

result:

ok answer is '457'

Test #32:

score: 0
Accepted
time: 2ms
memory: 3408kb

input:

3 0 1
2 1 2

output:

-1

result:

ok answer is '-1'

Test #33:

score: 0
Accepted
time: 0ms
memory: 3432kb

input:

1 0 1
1 1

output:

0

result:

ok answer is '0'