結果
問題 | No.1207 グラフX |
ユーザー | poohbear |
提出日時 | 2020-08-30 14:01:19 |
言語 | C++17 (gcc 13.2.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 381 ms / 2,000 ms |
コード長 | 4,674 bytes |
コンパイル時間 | 2,592 ms |
コンパイル使用メモリ | 217,412 KB |
実行使用メモリ | 44,900 KB |
最終ジャッジ日時 | 2023-08-09 12:09:03 |
合計ジャッジ時間 | 17,328 ms |
ジャッジサーバーID (参考情報) |
judge15 / judge11 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 297 ms
29,356 KB |
testcase_01 | AC | 305 ms
28,848 KB |
testcase_02 | AC | 297 ms
28,760 KB |
testcase_03 | AC | 299 ms
28,792 KB |
testcase_04 | AC | 304 ms
29,040 KB |
testcase_05 | AC | 377 ms
44,468 KB |
testcase_06 | AC | 379 ms
44,900 KB |
testcase_07 | AC | 379 ms
44,604 KB |
testcase_08 | AC | 264 ms
18,864 KB |
testcase_09 | AC | 263 ms
25,892 KB |
testcase_10 | AC | 376 ms
37,848 KB |
testcase_11 | AC | 381 ms
44,424 KB |
testcase_12 | AC | 257 ms
24,544 KB |
testcase_13 | AC | 164 ms
7,416 KB |
testcase_14 | AC | 293 ms
28,148 KB |
testcase_15 | AC | 265 ms
25,612 KB |
testcase_16 | AC | 158 ms
8,728 KB |
testcase_17 | AC | 210 ms
17,516 KB |
testcase_18 | AC | 169 ms
17,512 KB |
testcase_19 | AC | 227 ms
13,496 KB |
testcase_20 | AC | 301 ms
28,776 KB |
testcase_21 | AC | 28 ms
4,376 KB |
testcase_22 | AC | 206 ms
17,640 KB |
testcase_23 | AC | 221 ms
23,360 KB |
testcase_24 | AC | 147 ms
16,020 KB |
testcase_25 | AC | 294 ms
28,652 KB |
testcase_26 | AC | 236 ms
24,460 KB |
testcase_27 | AC | 284 ms
26,960 KB |
testcase_28 | AC | 273 ms
25,636 KB |
testcase_29 | AC | 269 ms
27,452 KB |
testcase_30 | AC | 149 ms
13,308 KB |
testcase_31 | AC | 145 ms
5,888 KB |
testcase_32 | AC | 118 ms
13,432 KB |
testcase_33 | AC | 130 ms
13,956 KB |
testcase_34 | AC | 255 ms
25,276 KB |
testcase_35 | AC | 30 ms
4,376 KB |
testcase_36 | AC | 257 ms
26,228 KB |
testcase_37 | AC | 228 ms
18,476 KB |
testcase_38 | AC | 63 ms
7,060 KB |
testcase_39 | AC | 134 ms
14,040 KB |
testcase_40 | AC | 117 ms
4,376 KB |
testcase_41 | AC | 203 ms
13,656 KB |
testcase_42 | AC | 2 ms
4,380 KB |
testcase_43 | AC | 2 ms
4,384 KB |
testcase_44 | AC | 1 ms
4,380 KB |
testcase_45 | AC | 272 ms
29,684 KB |
testcase_46 | AC | 273 ms
30,240 KB |
testcase_47 | AC | 276 ms
29,576 KB |
testcase_48 | AC | 273 ms
30,576 KB |
ソースコード
#include <bits/stdc++.h> #define rep(a,n) for (int a = 0; a < (n); ++a) using namespace std; using ll = long long; typedef pair<ll,ll> P; typedef pair<P,ll> PP; typedef vector<vector<ll> > Graph; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const ll INF = 1e18; // auto mod int // depends on Template const int mod = 1000000007; //const int mod = 998244353; struct mint { ll x; // typedef long long ll; mint(ll x=0):x((x%mod+mod)%mod){} mint operator-() const { return mint(-x);} mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if ((x += mod-a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;} mint operator+(const mint a) const { return mint(*this) += a;} mint operator-(const mint a) const { return mint(*this) -= a;} mint operator*(const mint a) const { return mint(*this) *= a;} mint pow(ll t) const { if (!t) return 1; mint a = pow(t>>1); a *= a; if (t&1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod-2);} mint& operator/=(const mint a) { return *this *= a.inv();} mint operator/(const mint a) const { return mint(*this) /= a;} }; istream& operator>>(istream& is, const mint& a) { return is >> a.x;} ostream& operator<<(ostream& os, const mint& a) { return os << a.x;} long long modpow(long long a, long long n) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } //UnionFind struct UnionFind { vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2 UnionFind(int n) : par(n, -1) { } int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根} if (par[x] < 0) return x; return par[x] = root(par[x]); } bool merge(int x, int y) {//xの木とyの木を結合する x = root(x); y = root(y); if (x == y) return false; if (par[x] > par[y]) swap(x, y); // merge technique par[x] += par[y]; par[y] = x; return true; } int size(int x) {//sizeの取得 return -par[root(x)]; } bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す int rx = root(x); int ry = root(y); return rx == ry; } }; template< typename T = int > struct Edge { int from, to; T cost; int idx; Edge() = default; Edge(int from, int to, T cost = 1, int idx = -1) : from(from), to(to), cost(cost), idx(idx) {} operator int() const { return to; } }; template<typename T > struct TreeDP{ using FX = function<T(T,T)>;//T●T->Tとなる関数の型 using FA = function<T(T)>; const T e;//単位元 struct Edge{ int to; ll cost; }; using Graph = vector<vector<Edge> >; FX merge; FA add_root; Graph G; int n;//木の要素数 vector< T > dp; TreeDP(FX merge_,FA add_root_, int n_, T e_):merge(merge_),add_root(add_root_),n(n_),e(e_){ dp.resize(n); G.resize(n); } void add_edge(int a,int b){ G[a].push_back({b}); } void build(){ dfs(0); } void dfs(int v,int p=-1){ T dp_cum = e; int deg = G[v].size(); for(int i = 0; i < deg; i++){ int x = G[v][i].to; if(x==p)continue; dfs(x,v); dp_cum = merge(dp_cum,dp[x]); } dp[v] = add_root(dp_cum); } }; //input ll N,M,X; int main(){ cin >> N >> M >> X; UnionFind u(N); Graph g(N); vector<Edge<ll> > edges; auto merge = [](ll dp_cum,ll d)->ll{ return dp_cum+d; }; auto add_root = [](ll d) -> ll{ return d+1; }; TreeDP<ll>tdp(merge,add_root,N,0); rep(i,M){ int x,y; ll z; cin >> x >> y >> z; x--;y--; if(u.same(x,y)){ continue; } u.merge(x,y); tdp.add_edge(x,y); tdp.add_edge(y,x); Edge<ll> e = {x,y,z}; edges.push_back(e); } tdp.build(); mint ans = 0; rep(i,edges.size()){ int x = edges[i].from; int y = edges[i].to; ll z = edges[i].cost; ll ch = min(tdp.dp[x],tdp.dp[y]); mint tmp = 1; tmp *= ch; tmp *= N-ch; tmp *= modpow(X,z); ans += tmp; } cout << ans << endl; return 0; }