結果

問題 No.1207 グラフX
ユーザー kotatsugamekotatsugame
提出日時 2020-10-07 23:43:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 353 ms / 2,000 ms
コード長 3,321 bytes
コンパイル時間 795 ms
コンパイル使用メモリ 78,252 KB
実行使用メモリ 33,608 KB
最終ジャッジ日時 2023-09-27 10:17:59
合計ジャッジ時間 15,041 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 300 ms
21,972 KB
testcase_01 AC 300 ms
21,728 KB
testcase_02 AC 311 ms
21,908 KB
testcase_03 AC 302 ms
21,724 KB
testcase_04 AC 297 ms
21,840 KB
testcase_05 AC 352 ms
33,532 KB
testcase_06 AC 350 ms
33,464 KB
testcase_07 AC 353 ms
33,608 KB
testcase_08 AC 244 ms
16,668 KB
testcase_09 AC 254 ms
19,084 KB
testcase_10 AC 349 ms
27,964 KB
testcase_11 AC 352 ms
33,244 KB
testcase_12 AC 242 ms
17,844 KB
testcase_13 AC 158 ms
11,208 KB
testcase_14 AC 288 ms
21,224 KB
testcase_15 AC 259 ms
18,328 KB
testcase_16 AC 147 ms
11,756 KB
testcase_17 AC 195 ms
15,984 KB
testcase_18 AC 156 ms
16,108 KB
testcase_19 AC 213 ms
13,656 KB
testcase_20 AC 291 ms
21,440 KB
testcase_21 AC 32 ms
9,472 KB
testcase_22 AC 194 ms
16,332 KB
testcase_23 AC 213 ms
18,060 KB
testcase_24 AC 138 ms
15,464 KB
testcase_25 AC 289 ms
21,288 KB
testcase_26 AC 230 ms
18,176 KB
testcase_27 AC 273 ms
19,916 KB
testcase_28 AC 262 ms
19,368 KB
testcase_29 AC 257 ms
19,720 KB
testcase_30 AC 139 ms
13,672 KB
testcase_31 AC 138 ms
10,528 KB
testcase_32 AC 111 ms
13,660 KB
testcase_33 AC 120 ms
13,516 KB
testcase_34 AC 250 ms
18,084 KB
testcase_35 AC 31 ms
9,600 KB
testcase_36 AC 249 ms
19,104 KB
testcase_37 AC 219 ms
16,476 KB
testcase_38 AC 58 ms
10,852 KB
testcase_39 AC 125 ms
14,412 KB
testcase_40 AC 117 ms
9,788 KB
testcase_41 AC 192 ms
13,696 KB
testcase_42 AC 4 ms
9,392 KB
testcase_43 AC 3 ms
9,432 KB
testcase_44 AC 3 ms
9,568 KB
testcase_45 AC 273 ms
21,712 KB
testcase_46 AC 269 ms
21,772 KB
testcase_47 AC 266 ms
21,788 KB
testcase_48 AC 266 ms
22,044 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
a.cpp:22:1: 警告: ISO C++ では型の無い ‘main’ の宣言を禁止しています [-Wreturn-type]

ソースコード

diff #

#line 1 "a.cpp"
#include<iostream>
#include<vector>
using namespace std;
#line 3 "/home/kotatsugame/library/math/modint.cpp"
#include<utility>
template<int m>
struct modint{
	unsigned int x;
	constexpr modint()noexcept:x(){}
	constexpr modint(long long _x)noexcept:x((_x%=m)<0?_x+m:_x){}
	constexpr unsigned int val()const noexcept{return x;}
	constexpr modint&operator++()noexcept{if(++x==m)x=0;return*this;}
	constexpr modint&operator--()noexcept{if(x==0)x=m;--x;return*this;}
	constexpr modint operator++(int)noexcept{modint res=*this;++*this;return res;}
	constexpr modint operator--(int)noexcept{modint res=*this;--*this;return res;}
	constexpr modint&operator+=(const modint&a)noexcept{x+=a.x;if(x>=m)x-=m;return*this;}
	constexpr modint&operator-=(const modint&a)noexcept{if(x<a.x)x+=m;x-=a.x;return*this;}
	constexpr modint&operator*=(const modint&a)noexcept{x=(unsigned long long)x*a.x%m;return*this;}
	constexpr modint&operator/=(const modint&a)noexcept{return*this*=a.inv();}
	constexpr modint operator+()const noexcept{return*this;}
	constexpr modint operator-()const noexcept{return modint()-*this;}
	constexpr modint pow(long long n)const noexcept
	{
		if(n<0)return pow(-n).inv();
		modint x=*this,r=1;
		for(;n;x*=x,n>>=1)if(n&1)r*=x;
		return r;
	}
	constexpr modint inv()const noexcept
	{
		int s=x,t=m,x=1,u=0;
		while(t)
		{
			int k=s/t;
			s-=k*t;
			swap(s,t);
			x-=k*u;
			swap(x,u);
		}
		return modint(x);
	}
	constexpr modint operator+(const modint&a)const noexcept{return modint(*this)+=a;}
	constexpr modint operator-(const modint&a)const noexcept{return modint(*this)-=a;}
	constexpr modint operator*(const modint&a)const noexcept{return modint(*this)*=a;}
	constexpr modint operator/(const modint&a)const noexcept{return modint(*this)/=a;}
	constexpr bool operator==(const modint&a)const noexcept{return x==a.x;}
	constexpr bool operator!=(const modint&a)const noexcept{return x!=a.x;}
	friend ostream&operator<<(ostream&os,const modint&a){return os<<a.x;}
	friend istream&operator>>(istream&is,modint&a){long long v;is>>v;a=modint(v);return is;}
};
#line 2 "/home/kotatsugame/library/datastructure/UF.cpp"
struct UF{
	int n;
	vector<int>parent,rank;
	UF(int n_=0):n(n_),parent(n_),rank(n_,1)
	{
		for(int i=0;i<n_;i++)parent[i]=i;
	}
	int find(int a){return parent[a]!=a?parent[a]=find(parent[a]):a;}
	bool same(int a,int b){return find(a)==find(b);}
	bool unite(int a,int b)
	{
		a=find(a),b=find(b);
		if(a==b)return false;
		if(rank[a]<rank[b])
		{
			parent[a]=b;
			rank[b]+=rank[a];
		}
		else
		{
			parent[b]=a;
			rank[a]+=rank[b];
		}
		return true;
	}
	int size(int a){return rank[find(a)];}
};
#line 6 "a.cpp"
using mint=modint<(int)1e9+7>;
int N,M;
mint X;
vector<int>G[2<<17];
vector<pair<pair<int,int>,int> >E;
int depth[2<<17],ch[2<<17];
int dfs(int u,int p,int d)
{
	depth[u]=d;
	ch[u]=1;
	for(int v:G[u])if(v!=p)
	{
		ch[u]+=dfs(v,u,d+1);
	}
	return ch[u];
}
main()
{
	cin>>N>>M>>X;
	UF uf(N);
	for(int i=0;i<M;i++)
	{
		int x,y,z;cin>>x>>y>>z;
		x--,y--;
		if(uf.unite(x,y))
		{
			G[x].push_back(y);
			G[y].push_back(x);
			E.push_back(make_pair(make_pair(x,y),z));
		}
	}
	dfs(0,-1,0);
	mint ans=0;
	for(pair<pair<int,int>,int>e:E)
	{
		int u=e.first.first,v=e.first.second;
		if(depth[u]<depth[v])u=v;
		ans+=X.pow(e.second)*ch[u]*(N-ch[u]);
	}
	cout<<ans<<endl;
}
0