Submission #9611757
Source Code Expand
#include <bits/stdc++.h> const int mod = 1000000007; const int MAXN = 200010; typedef long long LL; void reduce(int & x) { x += x >> 31 & mod; } int pows[MAXN]; int n, m; LL X; int li[MAXN], bak; int xs[MAXN], ys[MAXN], vs[MAXN]; bool prob[MAXN], realin[MAXN]; std::vector<int> es[MAXN]; namespace bingchaset { int fa[MAXN]; void init(int n) { for (int i = 1; i <= n; ++i) fa[i] = i; } int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } } int head[MAXN], nxt[MAXN << 1], to[MAXN << 1], val[MAXN << 1], tot; void addedge(int b, int e, int v) { nxt[++tot] = head[b]; to[head[b] = tot] = e; val[tot] = v; nxt[++tot] = head[e]; to[head[e] = tot] = b; val[tot] = v; } int fa[17][MAXN], vt[17][MAXN], dep[MAXN]; void dfs(int u) { for (int i = 1; i != 17; ++i) { fa[i][u] = fa[i - 1][fa[i - 1][u]]; vt[i][u] = std::max(vt[i - 1][u], vt[i - 1][fa[i - 1][u]]); } for (int i = head[u]; i; i = nxt[i]) if (to[i] != fa[0][u]) { fa[0][to[i]] = u; vt[0][to[i]] = val[i]; dep[to[i]] = dep[u] + 1; dfs(to[i]); } } int getminv(int x, int y) { int res = 0; if (dep[x] < dep[y]) std::swap(x, y); for (int i = dep[x] - dep[y]; i; i &= i - 1) { int t = __builtin_ctz(i); res = std::max(res, vt[t][x]); x = fa[t][x]; } if (x == y) return res; for (int i = 16; ~i; --i) if (fa[i][x] != fa[i][y]) { res = std::max(res, std::max(vt[i][x], vt[i][y])); x = fa[i][x], y = fa[i][y]; } res = std::max(res, std::max(vt[0][x], vt[0][y])); return res; } void solve() { std::cin >> n >> m >> X; for (int i = 1; i <= m; ++i) std::cin >> xs[i] >> ys[i] >> vs[i]; memcpy(li, vs, m + 1 << 2); memset(realin, 0, m + 1); memset(prob, 0, m + 1); std::sort(li + 1, li + 1 + m); bak = std::unique(li + 1, li + 1 + m) - li - 1; for (int i = 1; i <= bak; ++i) es[i].clear(); for (int i = 1; i <= m; ++i) { vs[i] = std::lower_bound(li + 1, li + 1 + bak, vs[i]) - li; es[vs[i]].push_back(i); } using bingchaset::find; bingchaset::init(n); int ccc = 0; LL sz = 0; for (int i = 1; i <= bak; ++i) { const int SZ = es[i].size(); for (int j = 0; j != SZ; ++j) { int id = es[i][j]; int x = xs[id], y = ys[id]; if (find(x) != find(y)) prob[id] = true; } for (int j = 0; j != SZ; ++j) { int id = es[i][j]; int x = xs[id], y = ys[id]; if (find(x) != find(y)) { bingchaset::fa[find(x)] = find(y); realin[id] = true; sz += li[vs[id]]; ++ccc; } } } if (ccc != n - 1 || sz > X || n <= 2) { std::cout << 0 << std::endl; return ; } if (sz == X) { int cnt = 0; for (int i = 1; i <= m; ++i) cnt += prob[i]; int ans = (LL) (pows[cnt] - 2) * pows[m - cnt] % mod; reduce(ans); std::cout << ans << std::endl; return ; } for (int i = 1; i <= m; ++i) if (realin[i]) addedge(xs[i], ys[i], vs[i]); dfs(1); int cnx = n - 1, cny = 0; for (int i = 1; i <= m; ++i) if (!realin[i]) { int t = li[vs[i]] - li[getminv(xs[i], ys[i])]; cnx += t < X - sz; cny += t == X - sz; } int ans = 2ll * (pows[cny] - 1) % mod; reduce(ans); ans = (LL) ans * pows[m - cnx - cny] % mod; std::cout << ans << std::endl; memset(head, 0, n + 1 << 2); tot = 0; } int main() { *pows = 1; for (int i = 1; i != MAXN; ++i) reduce(pows[i] = pows[i - 1] * 2 - mod); std::ios_base::sync_with_stdio(false), std::cin.tie(0); solve(); return 0; }
Submission Info
Submission Time | |
---|---|
Task | E - Bichrome Spanning Tree |
User | daklqw |
Language | C++14 (GCC 5.4.1) |
Score | 900 |
Code Size | 3443 Byte |
Status | AC |
Exec Time | 11 ms |
Memory | 40064 KB |
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 900 / 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 | AC | 11 ms | 40064 KB |
02.txt | AC | 11 ms | 40064 KB |
03.txt | AC | 11 ms | 40064 KB |
04.txt | AC | 11 ms | 40064 KB |
05.txt | AC | 11 ms | 40064 KB |
06.txt | AC | 11 ms | 40064 KB |
07.txt | AC | 11 ms | 40064 KB |
08.txt | AC | 11 ms | 40064 KB |
09.txt | AC | 11 ms | 40064 KB |
10.txt | AC | 11 ms | 40064 KB |
11.txt | AC | 11 ms | 40064 KB |
12.txt | AC | 11 ms | 40064 KB |
13.txt | AC | 11 ms | 40064 KB |
14.txt | AC | 4 ms | 9216 KB |
15.txt | AC | 5 ms | 9344 KB |
16.txt | AC | 5 ms | 9344 KB |
17.txt | AC | 5 ms | 9344 KB |
18.txt | AC | 5 ms | 9344 KB |
19.txt | AC | 11 ms | 40064 KB |
20.txt | AC | 5 ms | 9344 KB |
21.txt | AC | 5 ms | 9344 KB |
22.txt | AC | 11 ms | 40064 KB |
23.txt | AC | 11 ms | 40064 KB |
24.txt | AC | 11 ms | 40064 KB |
25.txt | AC | 11 ms | 40064 KB |
26.txt | AC | 11 ms | 40064 KB |
27.txt | AC | 11 ms | 40064 KB |
28.txt | AC | 11 ms | 40064 KB |
29.txt | AC | 11 ms | 40064 KB |
30.txt | AC | 11 ms | 40064 KB |
31.txt | AC | 11 ms | 40064 KB |
32.txt | AC | 11 ms | 40064 KB |
33.txt | AC | 11 ms | 40064 KB |
34.txt | AC | 11 ms | 40064 KB |
35.txt | AC | 11 ms | 39936 KB |
36.txt | AC | 11 ms | 40064 KB |
37.txt | AC | 11 ms | 39936 KB |
38.txt | AC | 11 ms | 40064 KB |
39.txt | AC | 11 ms | 40064 KB |
40.txt | AC | 11 ms | 40064 KB |
41.txt | AC | 11 ms | 40064 KB |
42.txt | AC | 11 ms | 39936 KB |
43.txt | AC | 11 ms | 40064 KB |
44.txt | AC | 4 ms | 9216 KB |
45.txt | AC | 11 ms | 40064 KB |
46.txt | AC | 11 ms | 40064 KB |
47.txt | AC | 11 ms | 40064 KB |
48.txt | AC | 11 ms | 40064 KB |
sample-01.txt | AC | 4 ms | 9216 KB |
sample-02.txt | AC | 10 ms | 39936 KB |
sample-03.txt | AC | 4 ms | 9216 KB |
sample-04.txt | AC | 10 ms | 39936 KB |