Submission #2829695
Source Code Expand
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define mp make_pair #define pb push_back #define REP(i,n) for (int i = 0; i < n; ++ i) #define FOR(i,a,b) for (int i = a; i <= b; ++ i) #define FORD(i,b,a) for (int i = b; i >= a; -- i) #define debug(x) cerr << #x << ": " << x << "\n" #define task "" typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> ii; typedef pair<ll, ll> pll; typedef pair<ll, int> li; typedef vector<int> vi; const int N = 1000 + 5; const int M = 2000 + 5; const ll md = 1e9 + 7; struct Edge{ int u, v, c; bool operator < (Edge oth) { return c < oth.c; } } e[M]; int n, m, lab[N], rnk[N]; ll x; ll Pow(int x) { if (x == 0) return 1; else { ll res = Pow(x / 2); res = res * res % md; if (x % 2) res = res * 2 % md; return res; } } int root(int u) { if (lab[u] == -1) return u; else return lab[u] = root(lab[u]); } void join(int u, int v) { u = root(u), v = root(v); if (rnk[u] > rnk[v]) { lab[v] = u; } else { lab[u] = v; if (rnk[u] == rnk[v]) rnk[v] ++; } } ll MST() { ll res = 0; REP(i, m) { int u = e[i].u, v = e[i].v; if (root(u) != root(v)) { join(u, v); res += e[i].c; } } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); //freopen(task".inp", "r", stdin); //freopen(task".out", "w", stdout); cin >> n >> m >> x; REP(i, m) { cin >> e[i].u >> e[i].v >> e[i].c; } sort(e, e+m); int cnt1 = 0, cnt2 = 0, cnt0 = 0; REP(i, m) { memset(lab, -1, sizeof lab); memset(rnk, 0, sizeof rnk); join(e[i].u, e[i].v); ll tmp = e[i].c + MST(); if (tmp < x) cnt0 ++; else if (tmp == x) cnt1 ++; else if (tmp > x) cnt2 ++; } //cer << cnt0 << " " << cnt1 << " " << cnt2 << "\n"; if (cnt1 == 0) cout << 0; else if (cnt0 == 0) { //cout << ((Pow(n-1) - 2) * Pow(m-n+1) % md + 2 * (Pow(cnt1 - n + 1) - 1) % md * Pow(cnt2) % md) % md; cout << (Pow(cnt1) - 2) * Pow(cnt2) % md; } else cout << 2 * (Pow(cnt1) - 1) % md * Pow(cnt2) % md; }
Submission Info
Submission Time | |
---|---|
Task | E - Bichrome Spanning Tree |
User | npnkhoi |
Language | C++14 (GCC 5.4.1) |
Score | 900 |
Code Size | 2104 Byte |
Status | AC |
Exec Time | 60 ms |
Memory | 256 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 | 53 ms | 256 KB |
02.txt | AC | 51 ms | 256 KB |
03.txt | AC | 37 ms | 256 KB |
04.txt | AC | 56 ms | 256 KB |
05.txt | AC | 54 ms | 256 KB |
06.txt | AC | 53 ms | 256 KB |
07.txt | AC | 56 ms | 256 KB |
08.txt | AC | 58 ms | 256 KB |
09.txt | AC | 60 ms | 256 KB |
10.txt | AC | 56 ms | 256 KB |
11.txt | AC | 41 ms | 256 KB |
12.txt | AC | 13 ms | 256 KB |
13.txt | AC | 13 ms | 256 KB |
14.txt | AC | 13 ms | 256 KB |
15.txt | AC | 42 ms | 256 KB |
16.txt | AC | 40 ms | 256 KB |
17.txt | AC | 55 ms | 256 KB |
18.txt | AC | 54 ms | 256 KB |
19.txt | AC | 55 ms | 256 KB |
20.txt | AC | 56 ms | 256 KB |
21.txt | AC | 58 ms | 256 KB |
22.txt | AC | 55 ms | 256 KB |
23.txt | AC | 56 ms | 256 KB |
24.txt | AC | 13 ms | 256 KB |
25.txt | AC | 13 ms | 256 KB |
26.txt | AC | 40 ms | 256 KB |
27.txt | AC | 52 ms | 256 KB |
28.txt | AC | 58 ms | 256 KB |
29.txt | AC | 57 ms | 256 KB |
30.txt | AC | 58 ms | 256 KB |
31.txt | AC | 55 ms | 256 KB |
32.txt | AC | 55 ms | 256 KB |
33.txt | AC | 51 ms | 256 KB |
34.txt | AC | 39 ms | 256 KB |
35.txt | AC | 30 ms | 256 KB |
36.txt | AC | 30 ms | 256 KB |
37.txt | AC | 30 ms | 256 KB |
38.txt | AC | 53 ms | 256 KB |
39.txt | AC | 56 ms | 256 KB |
40.txt | AC | 56 ms | 256 KB |
41.txt | AC | 42 ms | 256 KB |
42.txt | AC | 19 ms | 256 KB |
43.txt | AC | 15 ms | 256 KB |
44.txt | AC | 12 ms | 256 KB |
45.txt | AC | 20 ms | 256 KB |
46.txt | AC | 32 ms | 256 KB |
47.txt | AC | 13 ms | 256 KB |
48.txt | AC | 13 ms | 256 KB |
sample-01.txt | AC | 1 ms | 256 KB |
sample-02.txt | AC | 1 ms | 256 KB |
sample-03.txt | AC | 1 ms | 256 KB |
sample-04.txt | AC | 1 ms | 256 KB |