結果
問題 | No.1207 グラフX |
ユーザー | onsen_manjuuu |
提出日時 | 2020-08-31 02:33:22 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 766 ms / 2,000 ms |
コード長 | 2,883 bytes |
コンパイル時間 | 2,117 ms |
コンパイル使用メモリ | 182,884 KB |
実行使用メモリ | 55,552 KB |
最終ジャッジ日時 | 2024-11-15 16:20:27 |
合計ジャッジ時間 | 26,868 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 679 ms
41,860 KB |
testcase_01 | AC | 696 ms
41,816 KB |
testcase_02 | AC | 712 ms
42,156 KB |
testcase_03 | AC | 764 ms
41,696 KB |
testcase_04 | AC | 766 ms
42,308 KB |
testcase_05 | AC | 671 ms
55,552 KB |
testcase_06 | AC | 657 ms
55,552 KB |
testcase_07 | AC | 662 ms
55,552 KB |
testcase_08 | AC | 522 ms
26,004 KB |
testcase_09 | AC | 578 ms
32,664 KB |
testcase_10 | AC | 628 ms
48,512 KB |
testcase_11 | AC | 663 ms
55,552 KB |
testcase_12 | AC | 533 ms
28,176 KB |
testcase_13 | AC | 205 ms
9,472 KB |
testcase_14 | AC | 713 ms
39,672 KB |
testcase_15 | AC | 571 ms
31,340 KB |
testcase_16 | AC | 210 ms
11,392 KB |
testcase_17 | AC | 395 ms
24,276 KB |
testcase_18 | AC | 350 ms
24,860 KB |
testcase_19 | AC | 334 ms
16,896 KB |
testcase_20 | AC | 698 ms
40,752 KB |
testcase_21 | AC | 29 ms
5,248 KB |
testcase_22 | AC | 400 ms
24,796 KB |
testcase_23 | AC | 456 ms
28,236 KB |
testcase_24 | AC | 318 ms
22,356 KB |
testcase_25 | AC | 694 ms
40,344 KB |
testcase_26 | AC | 503 ms
30,472 KB |
testcase_27 | AC | 620 ms
36,120 KB |
testcase_28 | AC | 575 ms
32,656 KB |
testcase_29 | AC | 595 ms
35,852 KB |
testcase_30 | AC | 243 ms
16,000 KB |
testcase_31 | AC | 165 ms
7,424 KB |
testcase_32 | AC | 219 ms
17,152 KB |
testcase_33 | AC | 233 ms
16,512 KB |
testcase_34 | AC | 542 ms
30,856 KB |
testcase_35 | AC | 32 ms
5,248 KB |
testcase_36 | AC | 554 ms
32,312 KB |
testcase_37 | AC | 450 ms
25,876 KB |
testcase_38 | AC | 88 ms
8,960 KB |
testcase_39 | AC | 229 ms
18,136 KB |
testcase_40 | AC | 119 ms
5,248 KB |
testcase_41 | AC | 339 ms
17,152 KB |
testcase_42 | AC | 2 ms
5,248 KB |
testcase_43 | AC | 1 ms
5,248 KB |
testcase_44 | AC | 2 ms
5,248 KB |
testcase_45 | AC | 637 ms
41,516 KB |
testcase_46 | AC | 658 ms
41,648 KB |
testcase_47 | AC | 657 ms
41,516 KB |
testcase_48 | AC | 651 ms
41,524 KB |
ソースコード
#include<bits/stdc++.h> using namespace std; using ll = long long; #define MOD 1000000007 template<int mod> struct ModInt{ int x; ModInt():x(0){} ModInt(long long y):x(y>=0?y%mod:(mod-(-y)%mod)%mod){} ModInt &operator+=(const ModInt &p){ if((x+=p.x)>=mod)x-=mod; return *this; } ModInt &operator-=(const ModInt &p){ if((x+=mod-p.x)>=mod)x-=mod; return *this; } ModInt &operator*=(const ModInt &p){ x=(int)(1LL*x*p.x%mod); return *this; } ModInt &operator/=(const ModInt &p){ *this*=p.inverse(); return *this; } ModInt &operator^=(long long p){ ModInt res = 1; for (; p; p >>= 1) { if (p & 1) res *= *this; *this *= *this; } return *this = res; } ModInt operator-()const{return ModInt(-x);} ModInt operator+(const ModInt &p)const{return ModInt(*this)+=p;} ModInt operator-(const ModInt &p)const{return ModInt(*this)-=p;} ModInt operator*(const ModInt &p)const{return ModInt(*this)*=p;} ModInt operator/(const ModInt &p)const{return ModInt(*this)/=p;} ModInt operator^(long long p)const{return ModInt(*this)^=p;} bool operator==(const ModInt &p)const{return x==p.x;} bool operator!=(const ModInt &p)const{return x!=p.x;} explicit operator int() const { return x; } ModInt operator=(const int p) {x = p; return ModInt(*this);} ModInt inverse()const{ int a=x,b=mod,u=1,v=0,t; while(b>0){ t=a/b; a-=t*b; std::swap(a,b); u-=t*v; std::swap(u,v); } return ModInt(u); } friend std::ostream &operator<<(std::ostream &os,const ModInt<mod> &p){ return os<<p.x; } friend std::istream &operator>>(std::istream &is,ModInt<mod> &a){ long long x; is>>x; a=ModInt<mod>(x); return (is); } }; typedef ModInt<MOD> mint; class UnionFind{ private: vector<int> data; public: UnionFind(int n) : data(n,-1) {} int root(int i) { return data[i] < 0 ? i : data[i] = root(data[i]); } int size(int i) { return -data[root(i)]; } bool same(int x, int y) {return root(x) == root(y); } bool connected() {return size(0) == (int)data.size(); } bool unit(int i,int j){ i = root(i); j = root(j); if(i != j){ data[i] += data[j]; data[j] = i; } return i != j; } }; int n, m, x; vector<vector<pair<int,int>>> hen; map<pair<int,int>,int> mp; mint ans = 0; int dfs(int v, int par) { int sum = 0; for(auto i : hen[v]) { if(i.first == par)continue; sum += dfs(i.first, v); } sum++; int exp = mp[{v, par}]; ans += (mint(x)^exp) * (sum) * (n - sum); return sum; } int main() { cin >> n >> m >> x; UnionFind uni(n); hen.resize(n); for(int i = 0; i < m; i++) { int a, b, c; cin >> a >> b >> c; a--, b--; if(uni.same(a, b))continue; uni.unit(a,b); mp[{a,b}] = mp[{b,a}] = c; hen[a].push_back({b,c}); hen[b].push_back({a,c}); } dfs(0, -1); cout << ans << endl; }