QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#646167 | #7003. Rikka with Minimum Spanning Trees | SGColin | AC ✓ | 1754ms | 11144kb | C++17 | 3.3kb | 2024-10-16 21:32:48 | 2024-10-16 21:32:49 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef tuple<int, int, int> tii;
inline int rd() {
int x = 0;
bool f = 0;
char c = getchar();
for (; !isdigit(c); c = getchar()) f |= (c == '-');
for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
return f ? -x : x;
}
#define eb emplace_back
#define all(s) (s).begin(), (s).end()
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
const int N = 100007;
const int mod = 1000000007;
ull k1, k2;
ull xorShift128Plus() {
ull k3 = k1, k4 = k2;
k1 = k4;
k3 ^= k3 << 23;
k2 = k3 ^ k4 ^ (k3 >> 17) ^ (k4 >> 26);
return k2 + k4;
}
inline int fpow(int x, int t = mod - 2) {
int ret = 1;
for (; t; t >>= 1, x = 1ll * x * x % mod)
if (t & 1) ret = 1ll * ret * x % mod;
return ret;
}
struct matrix {
int n, m;
vector<vector<int>> a;
matrix(int n = 1, int m = 1) : n(n), m(m) {
a.assign(n + 1, vector<int>(m + 1, 0));
}
inline int Det() {
bool fl = 0;
rep(i, 1, n) rep(j, 1, m) if (a[i][j] < 0) a[i][j] += mod;
rep(i, 1, m) {
int nw = i;
rep(j, i, n) if (a[j][i]) {nw = j; break;}
if (a[nw][i] == 0) return 0;
if (nw != i) {swap(a[nw], a[i]); fl ^= 1;}
int inv = fpow(a[i][i]);
rep(j, i + 1, n) if (a[j][i]) {
ll cof = mod - 1ll * inv * a[j][i] % mod;
rep(k, i, n) a[j][k] = (a[j][k] + cof * a[i][k]) % mod;
}
}
ll res = 1;
rep(i, 1, n) res = res * a[i][i] % mod;
return fl ? (mod - res) % mod : res;
}
};
int f[N], tr[N];
int find(int x) {
return x == f[x] ? x : (f[x] = find(f[x]));
}
bool vis[N];
vector<int> e[N];
vector<tuple<ull, int, int>> E;
inline void work() {
E.clear();
int n, m;
cin >> n >> m >> k1 >> k2;
rep(i, 1, n) f[i] = i;
int mstcnt = 1;
rep(i, 1, m) {
int u = xorShift128Plus() % n + 1;
int v = xorShift128Plus() % n + 1;
ull w = xorShift128Plus();
E.eb(w, u, v);
}
sort(all(E));
int sum = 0, edgcnt = 0, ptr = 0;
while (ptr < m) {
ull nww = get<0>(E[ptr]);
vector<pii> nwE;
vector<int> nodes;
while (ptr < m) {
auto [w, u, v] = E[ptr];
if (w != nww) break;
if (find(u) != find(v)) {
u = find(u);
v = find(v);
vis[u] = vis[v] = false;
e[u].clear();
e[v].clear();
nwE.eb(u, v);
}
++ptr;
}
if (nwE.empty()) continue;
for (auto [u, v] : nwE) {e[u].eb(v); e[v].eb(u);}
// matrix tree
auto calc = [&](int u) {
vector<int> tmps;
queue<int> q;
q.push(u); vis[u] = true;
while (!q.empty()) {
u = q.front(); q.pop(); tmps.eb(u);
for (auto v : e[u]) if (!vis[v]) {
vis[v] = true; q.push(v);
}
}
sort(all(tmps));
int tmpn = tmps.size() - 1;
rep(i, 0, tmpn) tr[tmps[i]] = i;
matrix A(tmpn, tmpn);
for (auto x : tmps) {
A.a[tr[x]][tr[x]] = e[x].size();
for (auto y : e[x]) --A.a[tr[x]][tr[y]];
}
mstcnt = 1ll * mstcnt * A.Det() % mod;
};
for (auto [u, v] : nwE) if (!vis[u]) calc(u);
for (auto [u, v] : nwE)
if (find(u) != find(v)) {
f[find(u)] = find(v);
++edgcnt;
sum = (sum + nww) % mod;
}
}
if (edgcnt < n - 1) {puts("0"); return;}
printf("%lld\n", 1ll * sum * mstcnt % mod);
}
int main() {
int t; cin >> t;
while(t--) work();
return 0;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 7ms
memory: 8332kb
input:
1 2 100000 123456789 987654321
output:
575673759
result:
ok single line: '575673759'
Test #2:
score: 0
Accepted
time: 1754ms
memory: 11144kb
input:
100 17085 100000 676466090490 878574984049 24353 100000 685976930882 229685257786 4 100000 471961964055 157860429766 15406 100000 410376133349 828755231175 1 100000 809050431491 785471826497 1796 100000 218311747353 410830725634 93449 100000 761721499751 355474414706 3214 100000 277812617111 2429646...
output:
436638303 0 405150678 355979925 0 50713419 0 512195596 338362921 0 0 289558312 831627251 345103943 788519192 306890280 168133083 308823048 813378518 25758919 733644946 851485656 0 0 0 315609167 632805182 745061180 0 995073785 854970966 0 0 0 423134815 0 867689881 500810440 0 0 0 945701987 0 0 0 1959...
result:
ok 100 lines