結果

問題 No.1207 グラフX
ユーザー onsen_manjuuuonsen_manjuuu
提出日時 2020-08-31 02:33:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 743 ms / 2,000 ms
コード長 2,883 bytes
コンパイル時間 1,693 ms
コンパイル使用メモリ 183,832 KB
実行使用メモリ 55,612 KB
最終ジャッジ日時 2024-04-27 13:58:10
合計ジャッジ時間 26,799 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 698 ms
41,728 KB
testcase_01 AC 694 ms
41,684 KB
testcase_02 AC 704 ms
42,024 KB
testcase_03 AC 743 ms
41,572 KB
testcase_04 AC 702 ms
42,136 KB
testcase_05 AC 641 ms
55,524 KB
testcase_06 AC 615 ms
55,552 KB
testcase_07 AC 626 ms
55,524 KB
testcase_08 AC 465 ms
26,180 KB
testcase_09 AC 545 ms
32,536 KB
testcase_10 AC 611 ms
48,448 KB
testcase_11 AC 644 ms
55,612 KB
testcase_12 AC 485 ms
28,176 KB
testcase_13 AC 196 ms
9,344 KB
testcase_14 AC 662 ms
39,672 KB
testcase_15 AC 535 ms
31,344 KB
testcase_16 AC 208 ms
11,392 KB
testcase_17 AC 384 ms
24,144 KB
testcase_18 AC 341 ms
24,864 KB
testcase_19 AC 343 ms
16,640 KB
testcase_20 AC 675 ms
40,752 KB
testcase_21 AC 33 ms
5,376 KB
testcase_22 AC 450 ms
24,668 KB
testcase_23 AC 438 ms
28,228 KB
testcase_24 AC 278 ms
22,360 KB
testcase_25 AC 699 ms
40,472 KB
testcase_26 AC 484 ms
30,476 KB
testcase_27 AC 601 ms
35,988 KB
testcase_28 AC 559 ms
32,716 KB
testcase_29 AC 582 ms
35,720 KB
testcase_30 AC 233 ms
16,000 KB
testcase_31 AC 170 ms
7,296 KB
testcase_32 AC 221 ms
17,020 KB
testcase_33 AC 224 ms
16,384 KB
testcase_34 AC 514 ms
30,856 KB
testcase_35 AC 34 ms
5,376 KB
testcase_36 AC 518 ms
32,188 KB
testcase_37 AC 427 ms
25,880 KB
testcase_38 AC 89 ms
8,832 KB
testcase_39 AC 242 ms
18,140 KB
testcase_40 AC 123 ms
5,376 KB
testcase_41 AC 321 ms
17,152 KB
testcase_42 AC 2 ms
5,376 KB
testcase_43 AC 1 ms
5,376 KB
testcase_44 AC 2 ms
5,376 KB
testcase_45 AC 627 ms
41,516 KB
testcase_46 AC 627 ms
41,520 KB
testcase_47 AC 700 ms
41,524 KB
testcase_48 AC 666 ms
41,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0