結果
| 問題 |
No.1207 グラフX
|
| コンテスト | |
| ユーザー |
onsen_manjuuu
|
| 提出日時 | 2020-08-31 02:33:22 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.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 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 46 |
ソースコード
#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;
}
onsen_manjuuu