Submission #9583291
Source Code Expand
#include<bits/stdc++.h>
const int maxn = 100100;
const int mod = 1e9 + 7;
typedef long long ll;
struct T{ int u, v, w; } e[maxn];
struct { int to,nxt,v; }way[maxn << 1];
inline ll pow(int a,int b,int ans = 1) {
for(;b;b >>= 1,a = (ll) a * a % mod) if(b & 1)
ans = (ll) ans * a % mod;
return ans;
}
int h[maxn], num;
inline void link(int x,int y,int v) {
way[++num] = {y,h[x],v}, h[x] = num;
way[++num] = {x,h[y],v}, h[y] = num;
}
int bz[20][maxn], max[20][maxn], dep[maxn];
inline void dfs(int x,int f = 0) {
dep[x] = dep[f] + 1;
for(int i = 1;i < 20;++i) {
bz[i][x] = bz[i - 1][bz[i - 1][x]];
max[i][x] = std::max(max[i - 1][x], max[i - 1][bz[i - 1][x]]);
}
for(int i = h[x];i;i=way[i].nxt) if(way[i].to != f) {
max[0][way[i].to] = way[i].v;
bz[0][way[i].to] = x;
dfs(way[i].to,x);
}
}
inline void up(int & x,int y) {
if(x < y) x = y;
}
inline int get(int x,int y) {
if(dep[x] > dep[y]) std::swap(x, y);
int ret = 0;
for(int d = dep[y] - dep[x]; d; d &= d - 1) up(ret, max[__builtin_ctz(d)][y]), y = bz[__builtin_ctz(d)][y];
for(int i = 19;i >= 0;--i) if(bz[x][i] != bz[y][i]) {
up(ret, max[i][x]), x = bz[i][x];
up(ret, max[i][y]), y = bz[i][y];
}
if(x != y) up(ret, std::max(max[0][x], max[0][y]));
return ret;
}
int in[maxn];
int fa[maxn];
inline int find(int x){ return fa[x] == x ? x : fa[x] = find(fa[x]); }
int n, m;
ll x, sum;
int main() {
std::ios::sync_with_stdio(false), std::cin.tie(0);
std::cin >> n >> m >> x;
for(int i = 1;i <= n;++i) fa[i] = i;
for(int i = 1;i <= m;++i) std::cin >> e[i].u >> e[i].v >> e[i].w;
std::sort(e + 1,e + m + 1,[](const T & x,const T & y) { return x.w < y.w; });
for(int i = 1;i <= m;++i) {
if(find(e[i].u) != find(e[i].v)) {
fa[find(e[i].u)] = find(e[i].v);
sum += e[i].w;
link(e[i].v, e[i].u, e[i].w);
in[i] = 1;
}
}
dfs(1);
if(x < sum) {
std::cout << 0 << '\n';
return 0;
}
if(x == sum) {
int cnt = 0;
for(int i = 1;i <= m;++i) cnt += get(e[i].u, e[i].v) == e[i].w;
std::cout << (pow(2, cnt) - 2) * pow(2, m - cnt) % mod << '\n';
return 0;
}
int cnt0 = 0, cnt1 = 0;
for(int i = 1;i <= m;++i) cnt0 += e[i].w - get(e[i].u, e[i].v) < x - sum;
for(int i = 1;i <= m;++i) cnt1 += e[i].w - get(e[i].u, e[i].v) == x - sum;
std::cout << 2 * (pow(2, cnt1) - 1) * pow(2, m - cnt0 - cnt1) % mod << '\n';
}
Submission Info
Submission Time |
|
Task |
E - Bichrome Spanning Tree |
User |
skip2004 |
Language |
C++14 (GCC 5.4.1) |
Score |
0 |
Code Size |
2395 Byte |
Status |
RE |
Exec Time |
105 ms |
Memory |
16768 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
0 / 900 |
Status |
|
|
Set Name |
Test Cases |
Sample |
sample-01.txt, sample-02.txt, sample-03.txt, sample-04.txt |
All |
01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, 41.txt, 42.txt, 43.txt, 44.txt, 45.txt, 46.txt, 47.txt, 48.txt, sample-01.txt, sample-02.txt, sample-03.txt, sample-04.txt |
Case Name |
Status |
Exec Time |
Memory |
01.txt |
RE |
101 ms |
16640 KB |
02.txt |
RE |
103 ms |
16640 KB |
03.txt |
RE |
100 ms |
16640 KB |
04.txt |
RE |
101 ms |
16640 KB |
05.txt |
RE |
101 ms |
16640 KB |
06.txt |
RE |
101 ms |
16640 KB |
07.txt |
RE |
101 ms |
16640 KB |
08.txt |
RE |
101 ms |
16640 KB |
09.txt |
RE |
101 ms |
16640 KB |
10.txt |
RE |
100 ms |
16640 KB |
11.txt |
RE |
101 ms |
16640 KB |
12.txt |
RE |
101 ms |
16640 KB |
13.txt |
RE |
102 ms |
16640 KB |
14.txt |
RE |
101 ms |
16640 KB |
15.txt |
RE |
105 ms |
16640 KB |
16.txt |
AC |
6 ms |
16640 KB |
17.txt |
RE |
101 ms |
16640 KB |
18.txt |
AC |
6 ms |
16640 KB |
19.txt |
RE |
100 ms |
16768 KB |
20.txt |
AC |
6 ms |
16640 KB |
21.txt |
RE |
103 ms |
16640 KB |
22.txt |
RE |
102 ms |
16640 KB |
23.txt |
RE |
101 ms |
16640 KB |
24.txt |
RE |
100 ms |
16640 KB |
25.txt |
RE |
100 ms |
16640 KB |
26.txt |
RE |
101 ms |
16640 KB |
27.txt |
RE |
102 ms |
16640 KB |
28.txt |
RE |
101 ms |
16640 KB |
29.txt |
RE |
101 ms |
16640 KB |
30.txt |
RE |
101 ms |
16640 KB |
31.txt |
RE |
101 ms |
16640 KB |
32.txt |
RE |
103 ms |
16640 KB |
33.txt |
RE |
101 ms |
16640 KB |
34.txt |
RE |
101 ms |
16768 KB |
35.txt |
RE |
101 ms |
16640 KB |
36.txt |
RE |
100 ms |
16640 KB |
37.txt |
RE |
101 ms |
16640 KB |
38.txt |
RE |
100 ms |
16640 KB |
39.txt |
RE |
101 ms |
16640 KB |
40.txt |
RE |
100 ms |
16640 KB |
41.txt |
RE |
101 ms |
16640 KB |
42.txt |
RE |
100 ms |
16640 KB |
43.txt |
RE |
100 ms |
16640 KB |
44.txt |
AC |
5 ms |
16640 KB |
45.txt |
RE |
100 ms |
16640 KB |
46.txt |
RE |
100 ms |
16640 KB |
47.txt |
RE |
101 ms |
16640 KB |
48.txt |
RE |
103 ms |
16640 KB |
sample-01.txt |
AC |
5 ms |
16640 KB |
sample-02.txt |
AC |
5 ms |
16640 KB |
sample-03.txt |
AC |
5 ms |
16640 KB |
sample-04.txt |
AC |
5 ms |
16640 KB |