結果
問題 | No.1207 グラフX |
ユーザー | WA_TLE |
提出日時 | 2020-08-30 13:20:04 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 383 ms / 2,000 ms |
コード長 | 5,785 bytes |
コンパイル時間 | 1,683 ms |
コンパイル使用メモリ | 149,188 KB |
実行使用メモリ | 35,532 KB |
最終ジャッジ日時 | 2024-11-15 06:37:17 |
合計ジャッジ時間 | 16,516 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 310 ms
18,888 KB |
testcase_01 | AC | 312 ms
18,892 KB |
testcase_02 | AC | 310 ms
19,044 KB |
testcase_03 | AC | 306 ms
18,892 KB |
testcase_04 | AC | 316 ms
18,888 KB |
testcase_05 | AC | 381 ms
35,532 KB |
testcase_06 | AC | 378 ms
35,456 KB |
testcase_07 | AC | 383 ms
35,456 KB |
testcase_08 | AC | 320 ms
12,800 KB |
testcase_09 | AC | 289 ms
16,260 KB |
testcase_10 | AC | 367 ms
28,388 KB |
testcase_11 | AC | 377 ms
35,492 KB |
testcase_12 | AC | 295 ms
13,684 KB |
testcase_13 | AC | 192 ms
6,016 KB |
testcase_14 | AC | 301 ms
18,120 KB |
testcase_15 | AC | 299 ms
14,872 KB |
testcase_16 | AC | 185 ms
6,656 KB |
testcase_17 | AC | 237 ms
11,992 KB |
testcase_18 | AC | 170 ms
12,000 KB |
testcase_19 | AC | 284 ms
8,960 KB |
testcase_20 | AC | 304 ms
18,684 KB |
testcase_21 | AC | 31 ms
5,248 KB |
testcase_22 | AC | 230 ms
12,068 KB |
testcase_23 | AC | 240 ms
13,660 KB |
testcase_24 | AC | 151 ms
11,208 KB |
testcase_25 | AC | 307 ms
18,384 KB |
testcase_26 | AC | 255 ms
14,492 KB |
testcase_27 | AC | 297 ms
16,604 KB |
testcase_28 | AC | 298 ms
16,352 KB |
testcase_29 | AC | 272 ms
16,468 KB |
testcase_30 | AC | 172 ms
8,524 KB |
testcase_31 | AC | 161 ms
5,248 KB |
testcase_32 | AC | 125 ms
9,020 KB |
testcase_33 | AC | 144 ms
8,648 KB |
testcase_34 | AC | 289 ms
14,616 KB |
testcase_35 | AC | 33 ms
5,248 KB |
testcase_36 | AC | 273 ms
15,860 KB |
testcase_37 | AC | 262 ms
12,728 KB |
testcase_38 | AC | 68 ms
5,760 KB |
testcase_39 | AC | 145 ms
9,944 KB |
testcase_40 | AC | 128 ms
5,248 KB |
testcase_41 | AC | 253 ms
9,312 KB |
testcase_42 | AC | 2 ms
5,248 KB |
testcase_43 | AC | 2 ms
5,248 KB |
testcase_44 | AC | 2 ms
5,248 KB |
testcase_45 | AC | 276 ms
18,876 KB |
testcase_46 | AC | 276 ms
18,880 KB |
testcase_47 | AC | 278 ms
19,012 KB |
testcase_48 | AC | 279 ms
19,008 KB |
ソースコード
#include<deque> #include<queue> #include<vector> #include<algorithm> #include<iostream> #include<set> #include<cmath> #include<tuple> #include<string> #include<chrono> #include<functional> #include<iterator> #include<random> #include<unordered_set> #include<array> #include<map> #include<iomanip> #include<assert.h> #include<list> #include<bitset> #include<stack> #include<memory> #include<numeric> using namespace std; using namespace std::chrono; typedef long long int llint; typedef long double lldo; #define mp make_pair #define mt make_tuple #define pub push_back #define puf push_front #define pob pop_back #define pof pop_front #define fir first #define sec second #define res resize #define ins insert #define era erase #define REP(i, n) for(int i = 0;i < (n);i++) /*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/ const llint mod=1000000007; const llint inf=2.19e15+1; const long double pai=3.141592653589793238462643383279502884197; const long double eps=1e-10; template <class T,class U>bool chmin(T& a,U b){if(a>b){a=b;return true;}return false;} template <class T,class U>bool chmax(T& a,U b){if(a<b){a=b;return true;}return false;} llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);} llint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;} template<class T> void SO(T& ve){sort(ve.begin(),ve.end());} template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());} template<class T>llint LBI(const vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();} template<class T>llint UBI(const vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();} #include <algorithm> #include <utility> #include <type_traits> #include <cstdint> //永夜作 #define mnt mint_base class mint_base; //mint_base_base型用の累乗関数 constexpr mint_base m_pow(mint_base x, uint64_t n)noexcept; //mod計算を自動で行う整数テンプレートクラス class mint_base { public: constexpr mint_base operator+(const mint_base &other)const noexcept {auto v = *this;return v += other;} constexpr mint_base operator-(const mint_base &other)const noexcept {auto v = *this;return v -= other;} constexpr mint_base operator*(const mint_base &other)const noexcept {auto v = *this;return v *= other;} constexpr auto operator/(const mint_base &other)const noexcept {auto v = *this;return v /= other;} constexpr mint_base & operator+=(const mint_base &other) noexcept {a += other.a;if (mod <= a) { a -= mod; };return *this;} constexpr mint_base & operator-=(const mint_base &other) noexcept {if (a >= other.a) {a -= other.a;}else {a = (a + mod) - other.a;}return *this;} constexpr mint_base & operator*=(const mint_base &other) noexcept {a *= other.a;a %= mod;return *this;} constexpr mint_base & operator/=(const mint_base &other) noexcept {return *this *= ~other;} constexpr mint_base operator+()const noexcept { return *this; } constexpr mint_base operator-()const noexcept {return{ mod - a, mod_value_tag{} };} constexpr mint_base & operator++() noexcept {if (mod <= ++a) { a = 0; };return *this;} constexpr mint_base & operator--() noexcept {if (a <= 0) { a = mod; };--a;return *this;} constexpr mint_base operator++(int) noexcept {auto tmp = *this;++*this;return tmp;} constexpr mint_base operator--(int) noexcept {auto tmp = *this;--*this;return tmp;} constexpr mint_base operator~()const noexcept {return m_pow(*this, mod-2);} constexpr mint_base & operator=(const mint_base &other) noexcept {a=other.a;return *this;} constexpr explicit operator uint64_t()const noexcept {return a;} constexpr explicit operator int()const noexcept {return a;} constexpr explicit operator unsigned()const noexcept {return (unsigned)a;} static constexpr uint64_t getmod() noexcept {return mod;} constexpr mint_base(uint64_t a_) noexcept :a(a_ % mod) {} constexpr mint_base()noexcept : a(0) {} struct mod_value_tag {}; constexpr mint_base(uint64_t a_, mod_value_tag) :a(a_) {} private: uint64_t a; }; //mint_base型用の累乗関数 constexpr mint_base m_pow(mint_base x,uint64_t n)noexcept { mint_base res = 1; while(n>0){ if(n&1){res*=x;} x*=x; n>>=1; } return res; } //mint_baseの階乗計算 //0からxまでの階乗を返す //O(x)時間が必要 vector<mint_base> fla;//階乗が入る void fla_set(mint_base x) { fla.resize(((uint64_t)x)+1); fla[0] = 1; for (uint64_t i = 1; i <= (uint64_t)x;i++){fla[i]=fla[i-1]*i;} } vector<mint_base> gya;//~fla[i] が入る //O(x+log mod)で求める O(xlogmod)より速い void fla_gya_set(mint_base x) { fla_set(x); gya.resize(((uint64_t)x)+1); gya[(uint64_t)x]=~fla[(uint64_t)x]; for(uint64_t i =(uint64_t)x;i>0;i--){gya[i-1]=gya[i]*i;} } //mint_base型のstreamへの出力 std::ostream& operator<<(std::ostream& os, mint_base i) {os<<(uint64_t)i;return os;} //mint_base型のstreamからの入力 std::istream& operator >> (std::istream& is, mint_base& i) {uint64_t tmp;is>>tmp;i=tmp;return is;} llint n,m,X; vector<vector<pair<int,llint>>>ki; pair<mint_base,int>solve(int t,int p){ mint_base ans=0; int ko=1; for(auto it:ki[t]){ if(it.fir==p){continue;} mint_base ka; int kk; tie(ka,kk)=solve(it.fir,t); ans+=ka;ko+=kk; ans+=m_pow(X,it.sec)*kk*(n-kk); } return mp(ans,ko); } int main(void){ cin>>n>>m>>X; int i; llint ans=0; ki.res(n); vector<int>go(n); vector<int>ran(n); for(i=0;i<n;i++){go[i]=i;} for(i=0;i<m;i++){ int x,y;llint z;cin>>x>>y>>z; x--;y--; int xx=x,yy=y; while(go[xx]!=xx){xx=go[xx];} while(go[yy]!=yy){yy=go[yy];} go[x]=xx;go[y]=yy; if(go[xx]==go[yy]){continue;} if(ran[xx]>ran[yy]){swap(xx,yy);} if(ran[xx]==ran[yy]){ran[xx]++;} go[xx]=yy; ki[x].pub(mp(y,z)); ki[y].pub(mp(x,z)); } cout<<solve(0,-1).fir<<endl; return 0; }