結果
問題 | No.1207 グラフX |
ユーザー | hanbei_dayo |
提出日時 | 2020-08-30 17:32:22 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 400 ms / 2,000 ms |
コード長 | 2,686 bytes |
コンパイル時間 | 1,512 ms |
コンパイル使用メモリ | 111,708 KB |
実行使用メモリ | 42,904 KB |
最終ジャッジ日時 | 2024-11-15 13:52:47 |
合計ジャッジ時間 | 16,357 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 354 ms
23,632 KB |
testcase_01 | AC | 339 ms
22,988 KB |
testcase_02 | AC | 333 ms
23,420 KB |
testcase_03 | AC | 335 ms
22,868 KB |
testcase_04 | AC | 344 ms
22,724 KB |
testcase_05 | AC | 389 ms
42,904 KB |
testcase_06 | AC | 400 ms
42,496 KB |
testcase_07 | AC | 394 ms
42,732 KB |
testcase_08 | AC | 273 ms
18,780 KB |
testcase_09 | AC | 292 ms
21,864 KB |
testcase_10 | AC | 376 ms
34,040 KB |
testcase_11 | AC | 376 ms
42,580 KB |
testcase_12 | AC | 268 ms
18,552 KB |
testcase_13 | AC | 169 ms
13,216 KB |
testcase_14 | AC | 324 ms
22,444 KB |
testcase_15 | AC | 286 ms
20,324 KB |
testcase_16 | AC | 164 ms
13,348 KB |
testcase_17 | AC | 222 ms
18,668 KB |
testcase_18 | AC | 191 ms
16,340 KB |
testcase_19 | AC | 241 ms
15,420 KB |
testcase_20 | AC | 333 ms
22,792 KB |
testcase_21 | AC | 32 ms
9,128 KB |
testcase_22 | AC | 216 ms
17,788 KB |
testcase_23 | AC | 239 ms
18,604 KB |
testcase_24 | AC | 166 ms
15,540 KB |
testcase_25 | AC | 341 ms
22,588 KB |
testcase_26 | AC | 261 ms
19,140 KB |
testcase_27 | AC | 317 ms
22,032 KB |
testcase_28 | AC | 297 ms
21,904 KB |
testcase_29 | AC | 292 ms
21,708 KB |
testcase_30 | AC | 152 ms
13,732 KB |
testcase_31 | AC | 151 ms
13,468 KB |
testcase_32 | AC | 130 ms
13,552 KB |
testcase_33 | AC | 139 ms
13,520 KB |
testcase_34 | AC | 287 ms
20,440 KB |
testcase_35 | AC | 33 ms
9,156 KB |
testcase_36 | AC | 274 ms
21,472 KB |
testcase_37 | AC | 246 ms
17,820 KB |
testcase_38 | AC | 66 ms
10,612 KB |
testcase_39 | AC | 143 ms
14,892 KB |
testcase_40 | AC | 129 ms
13,660 KB |
testcase_41 | AC | 211 ms
15,396 KB |
testcase_42 | AC | 3 ms
8,232 KB |
testcase_43 | AC | 3 ms
8,188 KB |
testcase_44 | AC | 4 ms
8,108 KB |
testcase_45 | AC | 315 ms
22,792 KB |
testcase_46 | AC | 307 ms
22,788 KB |
testcase_47 | AC | 305 ms
23,944 KB |
testcase_48 | AC | 311 ms
22,920 KB |
ソースコード
#include<stdio.h> #include<iostream> #include<algorithm> #include<vector> #include<string> #include<utility> #include<map> #include<set> #include<queue> #include<stack> #include<functional> #include<math.h> #include<random> #include <bitset> using namespace std; #define N (1000000000+7) //#define N 998244353 #define INF 1e16 typedef long long ll; typedef pair<int,int> P; typedef pair<int,P> Q; const int inf = (int)1e9; ll gcd(ll a, ll b) { if (b > a) { ll tmp = b; b = a; a = tmp; } if (a%b == 0)return b; else return gcd(b, a%b); } class UnionFind { public: vector<int>par, rank, node; UnionFind(int n) :par(n), rank(n, 0),node(n,1) { for (int i = 0;i < n;i++)par[i] = i; } void init(int n) { for (int i = 0;i < n;i++) { par[i] = i; rank[i] = 0; node[i] = 1; } } int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); } bool same(int x, int y) { return find(x) == find(y); } void unite_tree(int x, int y) { x = find(x); y = find(y); if (x == y)return; if (rank[x] < rank[y]) { par[x] = y; } else { par[y] = x; if (rank[x] == rank[y])rank[x]++; } } int root_size(int x){ return rank[find(x)]; } int size(int x) { return node[find(x)]; } void unite_node(int x, int y) { x = find(x); y = find(y); if (x == y)return; if (size(x) < size(y))swap(x, y); node[x] += node[y]; par[y] = par[x]; } }; ll x; struct edge{ int from; int to; ll cost; }; struct E{ int to; ll cost; }; vector<E>g[200010]; ll inv(ll x,ll power) { ll res = 1; ll k = power; ll y = x%N; while (k) { if (k & 1)res = (res*y) % N; y = (y%N*y%N) % N; k /= 2; } return res; } ll dfs(int v, int p, ll n, ll &ans) { ll ret = 0; for (auto e: g[v]) { int to = e.to; ll cost = inv(x,e.cost); if (to == p) continue; ll a = dfs(to, v, n, ans); ll t = (((cost*a)%N)*(n-a))%N; ans = (ans+t)%N; ret = (ret+a)%N; } return (ret + 1)%N; } int main(void){ ll n,m; cin>>n>>m>>x; UnionFind uf = UnionFind(n); vector<edge>e; for(int i=0;i<m;i++){ int a,b; ll z; cin>>a>>b>>z; a--; b--; e.push_back({a,b,z}); } auto f = [&](edge &a,edge &b){ return a.cost<b.cost; }; sort(e.begin(),e.end(),f); for(int i=0;i<m;i++){ int u = e[i].from; int v = e[i].to; ll c = e[i].cost; if(!uf.same(u,v)){ uf.unite_node(u,v); g[u].push_back({v,c}); g[v].push_back({u,c}); } } ll ans = 0; dfs(0,-1,n,ans); cout<<ans<<endl; return 0; }