結果

問題 No.1207 グラフX
ユーザー stoqstoq
提出日時 2020-09-27 13:48:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 362 ms / 2,000 ms
コード長 9,077 bytes
コンパイル時間 2,992 ms
コンパイル使用メモリ 234,508 KB
実行使用メモリ 75,784 KB
最終ジャッジ日時 2023-09-13 01:14:01
合計ジャッジ時間 20,666 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 295 ms
63,380 KB
testcase_01 AC 292 ms
63,472 KB
testcase_02 AC 289 ms
64,200 KB
testcase_03 AC 293 ms
63,684 KB
testcase_04 AC 290 ms
63,360 KB
testcase_05 AC 362 ms
75,196 KB
testcase_06 AC 348 ms
74,932 KB
testcase_07 AC 356 ms
75,784 KB
testcase_08 AC 204 ms
40,756 KB
testcase_09 AC 234 ms
50,104 KB
testcase_10 AC 353 ms
70,168 KB
testcase_11 AC 357 ms
74,832 KB
testcase_12 AC 209 ms
44,420 KB
testcase_13 AC 95 ms
16,644 KB
testcase_14 AC 276 ms
60,880 KB
testcase_15 AC 235 ms
48,512 KB
testcase_16 AC 97 ms
18,268 KB
testcase_17 AC 174 ms
37,192 KB
testcase_18 AC 156 ms
37,212 KB
testcase_19 AC 151 ms
28,028 KB
testcase_20 AC 290 ms
62,216 KB
testcase_21 AC 14 ms
4,680 KB
testcase_22 AC 174 ms
38,764 KB
testcase_23 AC 198 ms
44,020 KB
testcase_24 AC 138 ms
33,376 KB
testcase_25 AC 281 ms
62,464 KB
testcase_26 AC 214 ms
47,524 KB
testcase_27 AC 266 ms
56,572 KB
testcase_28 AC 237 ms
50,384 KB
testcase_29 AC 249 ms
54,556 KB
testcase_30 AC 114 ms
24,520 KB
testcase_31 AC 76 ms
13,584 KB
testcase_32 AC 101 ms
25,472 KB
testcase_33 AC 107 ms
24,704 KB
testcase_34 AC 218 ms
48,272 KB
testcase_35 AC 16 ms
4,672 KB
testcase_36 AC 229 ms
49,660 KB
testcase_37 AC 188 ms
41,520 KB
testcase_38 AC 47 ms
12,452 KB
testcase_39 AC 110 ms
26,884 KB
testcase_40 AC 55 ms
9,496 KB
testcase_41 AC 144 ms
28,192 KB
testcase_42 AC 1 ms
4,376 KB
testcase_43 AC 2 ms
4,380 KB
testcase_44 AC 1 ms
4,376 KB
testcase_45 AC 275 ms
63,228 KB
testcase_46 AC 274 ms
63,092 KB
testcase_47 AC 281 ms
63,544 KB
testcase_48 AC 277 ms
63,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define MOD_TYPE 1

#pragma region Macros

#include <bits/stdc++.h>
using namespace std;

#if 0
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using Int = boost::multiprecision::cpp_int;
using lld = boost::multiprecision::cpp_dec_float_100;
#endif

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

using ll = long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
template <typename Q_type>
using smaller_queue = priority_queue<Q_type, vector<Q_type>, greater<Q_type>>;

constexpr ll MOD = (MOD_TYPE == 1 ? (ll)(1e9 + 7) : 998244353);
//constexpr ll MOD = 1;
constexpr int INF = (int)1e9 + 10;
constexpr ll LINF = (ll)4e18;
constexpr double PI = acos(-1.0);
constexpr double EPS = 1e-11;
constexpr int Dx[] = {0, 0, -1, 1, -1, 1, -1, 1, 0};
constexpr int Dy[] = {1, -1, 0, 0, -1, -1, 1, 1, 0};

#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define REPI(i, m, n) for (int i = m; i < (int)(n); ++i)
#define repi(i, n) REPI(i, 0, n)
#define MP make_pair
#define MT make_tuple
#define YES(n) cout << ((n) ? "YES" : "NO") << "\n"
#define Yes(n) cout << ((n) ? "Yes" : "No") << "\n"
#define possible(n) cout << ((n) ? "possible" : "impossible") << "\n"
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << "\n"
#define Yay(n) cout << ((n) ? "Yay!" : ":(") << "\n"
#define all(v) v.begin(), v.end()
#define NP(v) next_permutation(all(v))
#define dbg(x) cerr << #x << ":" << x << "\n";

struct io_init
{
  io_init()
  {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(30) << setiosflags(ios::fixed);
  };
} io_init;
template <typename T>
inline bool chmin(T &a, T b)
{
  if (a > b)
  {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
inline bool chmax(T &a, T b)
{
  if (a < b)
  {
    a = b;
    return true;
  }
  return false;
}
inline ll CEIL(ll a, ll b)
{
  return (a + b - 1) / b;
}
template <typename A, size_t N, typename T>
inline void Fill(A (&array)[N], const T &val)
{
  fill((T *)array, (T *)(array + N), val);
}
template <typename T, typename U>
constexpr istream &operator>>(istream &is, pair<T, U> &p) noexcept
{
  is >> p.first >> p.second;
  return is;
}
template <typename T, typename U>
constexpr ostream &operator<<(ostream &os, pair<T, U> &p) noexcept
{
  os << p.first << " " << p.second;
  return os;
}
#pragma endregion

#pragma region mint
template <int MOD>
struct Fp
{
  long long val;

  constexpr Fp(long long v = 0) noexcept : val(v % MOD)
  {
    if (val < 0)
      v += MOD;
  }

  constexpr int getmod()
  {
    return MOD;
  }

  constexpr Fp operator-() const noexcept
  {
    return val ? MOD - val : 0;
  }

  constexpr Fp operator+(const Fp &r) const noexcept
  {
    return Fp(*this) += r;
  }

  constexpr Fp operator-(const Fp &r) const noexcept
  {
    return Fp(*this) -= r;
  }

  constexpr Fp operator*(const Fp &r) const noexcept
  {
    return Fp(*this) *= r;
  }

  constexpr Fp operator/(const Fp &r) const noexcept
  {
    return Fp(*this) /= r;
  }

  constexpr Fp &operator+=(const Fp &r) noexcept
  {
    val += r.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }

  constexpr Fp &operator-=(const Fp &r) noexcept
  {
    val -= r.val;
    if (val < 0)
      val += MOD;
    return *this;
  }

  constexpr Fp &operator*=(const Fp &r) noexcept
  {
    val = val * r.val % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }

  constexpr Fp &operator/=(const Fp &r) noexcept
  {
    long long a = r.val, b = MOD, u = 1, v = 0;
    while (b)
    {
      long long t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }

  constexpr bool operator==(const Fp &r) const noexcept
  {
    return this->val == r.val;
  }

  constexpr bool operator!=(const Fp &r) const noexcept
  {
    return this->val != r.val;
  }

  friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept
  {
    return os << x.val;
  }

  friend constexpr istream &operator>>(istream &is, Fp<MOD> &x) noexcept
  {
    return is >> x.val;
  }
};

Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept
{
  if (n == 0)
    return 1;
  auto t = modpow(a, n / 2);
  t = t * t;
  if (n & 1)
    t = t * a;
  return t;
}

using mint = Fp<MOD>;
#pragma endregion

struct UnionFind
{
  vector<int> par, rank_;

  UnionFind() {}
  UnionFind(int n) : par(n), rank_(n, 0) { iota(begin(par), end(par), 0); }

  int root(int x) { return par[x] == x ? x : par[x] = root(par[x]); }

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

  void unite(int x, int y)
  {
    x = root(x);
    y = root(y);
    if (x == y)
      return;

    if (rank_[x] < rank_[y])
    {
      par[x] = y;
    }
    else
    {
      par[y] = x;
      if (rank_[x] == rank_[y])
        rank_[x]++;
    }
  }
};

template <typename T>
struct kruskal
{
  UnionFind uf;
  kruskal(int n = 200010) : uf(n) {}

  struct edge
  {
    int u, v;
    T cost;
    int num;
  };

  vector<edge> E;
  set<int> used_num;
  void add_E(int s, int t, T w)
  {
    static int i = 0;
    E.push_back({s, t, w, i++});
  }

  T calc()
  {
    sort(E.begin(), E.end(), [](edge &e1, edge &e2) { return e1.cost < e2.cost; });
    T res = 0;
    for (auto e : E)
    {
      if (!uf.same(e.u, e.v))
      {
        uf.unite(e.u, e.v);
        res += e.cost;
        used_num.insert(e.num);
      }
    }
    return res;
  }

  bool used(int i) { return used_num.count(i); }
};

template <typename T = ll>
struct Tree
{
  int V;
  using P = pair<int, ll>;
  vector<vector<P>> E;
  vector<int> par;
  vector<int> depth;
  vector<int> sub;
  vector<T> dist;
  vector<vector<int>> par_double;

  Tree(int V_) : V(V_)
  {
    E.resize(V);
    depth.resize(V);
    dist.resize(V);
    sub.resize(V);
  }

  void add_E(int a, int b, T w = T(1), bool direction = false)
  {
    E[a].push_back(make_pair(b, w));
    if (!direction)
      E[b].push_back(make_pair(a, w));
  }

  int dfs(int p, int d, T w)
  {
    sub[p] = 1;
    for (auto pi : E[p])
    {
      int v = pi.first;
      if (par[p] == v)
        continue;
      par[v] = p;
      depth[v] = d + 1;
      dist[v] = w + pi.second;
      sub[p] += dfs(v, depth[v], dist[v]);
    }
    return sub[p];
  }

  void make_tree(int root = 0)
  {
    calculated = false;
    par.assign(V, -1);
    par_double.assign(V, vector<int>(25));
    depth[root] = 0;
    dist[root] = T(0);
    dfs(root, 0, 0);
  }

  bool calculated;
  void calc_double()
  {
    for (int i = 0; i < V; i++)
      par_double[i][0] = par[i];

    for (int k = 0; k < 24; k++)
    {
      for (int i = 0; i < V; i++)
      {
        if (par_double[i][k] == -1)
          par_double[i][k + 1] = -1;
        else
          par_double[i][k + 1] = par_double[par_double[i][k]][k];
      }
    }
  }

  int getLCA(int a, int b)
  {
    if (!calculated)
    {
      calc_double();
      calculated = true;
    }
    if (a == b)
      return a;
    if (depth[a] < depth[b])
      swap(a, b);
    for (int k = 24; k >= 0; k--)
    {
      if (par_double[a][k] != -1 && depth[par_double[a][k]] >= depth[b])
        a = par_double[a][k];
    }
    if (a == b)
      return a;
    for (int k = 24; k >= 0; k--)
    {
      if (par_double[a][k] != -1 && par_double[a][k] != par_double[b][k])
      {
        a = par_double[a][k];
        b = par_double[b][k];
      }
    }
    return par_double[a][0];
  }

  int length(int a, int b)
  {
    return depth[a] + depth[b] - 2 * depth[getLCA(a, b)];
  }

  int distance(int a, int b)
  {
    return dist[a] + dist[b] - 2 * dist[getLCA(a, b)];
  }

  T diameter(int &a, int &b)
  {
    T Max(-1);
    for (int i = 0; i < V; i++)
    {
      if (Max < distance(0, i))
        Max = distance(0, i), a = i;
    }
    for (int i = 0; i < V; i++)
    {
      if (Max < distance(a, i))
        Max = distance(a, i), b = i;
    }
    return Max;
  }

  T diameter()
  {
    int a, b;
    return diameter(a, b);
  }

  int diameter_l(int &a, int &b)
  {
    int Max = -1;
    for (int i = 0; i < V; i++)
    {
      if (Max < length(0, i))
        Max = length(0, i), a = i;
    }
    for (int i = 0; i < V; i++)
    {
      if (Max < length(a, i))
        Max = length(a, i), b = i;
    }
    return Max;
  }

  int diameter_l()
  {
    int a, b;
    return diameter_l(a, b);
  }
};

void solve()
{
  ll n, m;
  ll x;
  cin >> n >> m >> x;
  kruskal<ll> ks(n);
  rep(i, m)
  {
    int x, y;
    ll z;
    cin >> x >> y >> z;
    x--, y--;
    ks.add_E(x, y, z);
  }
  ks.calc();
  Tree<ll> tr(n);
  for (auto i : ks.used_num)
  {
    auto [u, v, z, num] = ks.E[i];
    tr.add_E(u, v, z);
  }
  tr.make_tree();
  mint ans = 0;
  for (auto i : ks.used_num)
  {
    auto [u, v, z, num] = ks.E[i];
    if (tr.par[v] == u)
      swap(u, v);
    ll t = tr.sub[u] * (n - tr.sub[u]);
    ans += modpow(x, z) * t;
  }
  cout << ans << "\n";
}

int main()
{
  solve();
}
0