QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#680730 | #6239. 毒瘤 | propane | 100 ✓ | 93ms | 26724kb | C++20 | 8.0kb | 2024-10-26 22:22:20 | 2024-10-26 22:22:22 |
Judging History
answer
#include<iostream>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#include<algorithm>
using namespace std;
using LL = long long;
const int mod = 998244353;
void add(int &a, int b){
a += b;
if (a >= mod) a -= mod;
}
int pls(int a, int b){
a += b;
if (a >= mod) a -= mod;
return a;
}
int mul(int a, int b){
return 1LL * a * b % mod;
}
int qpow(int a, int b){
int ans = 1;
while(b){
if (b & 1) ans = mul(ans, a);
b >>= 1;
a = mul(a, a);
}
return ans;
}
const int maxn = 1e5 + 5;
int p[maxn];
int find(int x){
return p[x] == x ? x : p[x] = find(p[x]);
}
bool merge(int x, int y){
x = find(x), y = find(y);
if (x != y){
p[x] = y;
return true;
}
return false;
}
int dep[maxn], sz[maxn], in[maxn], out[maxn], top[maxn], fa[maxn], seq[maxn], ts;
vector<int> g[maxn];
void dfs_sz(int u){
if (fa[u]){
g[u].erase(find(g[u].begin(), g[u].end(), fa[u]));
}
sz[u] = 1;
for(auto &j : g[u]){
fa[j] = u;
dep[j] = dep[u] + 1;
dfs_sz(j);
sz[u] += sz[j];
if (sz[j] > sz[g[u][0]]) swap(j, g[u][0]);
}
}
void dfs_hld(int u){
in[u] = ++ts;
out[top[u]] = ts;
seq[ts] = u;
for(auto j : g[u]){
top[j] = (j == g[u][0] ? top[u] : j);
dfs_hld(j);
}
}
const int N = 2;
struct Matrix{
int a[2][2];
Matrix(){
a[0][0] = a[1][1] = 1;
a[0][1] = a[1][0] = 0;
}
Matrix operator*(const Matrix &t) const {
Matrix ans;
memset(ans.a, 0, sizeof ans.a);
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
LL sum = 0;
for(int k = 0; k < N; k++){
sum += 1LL * a[i][k] * t.a[k][j];
}
ans.a[i][j] = sum % mod;
}
}
return ans;
}
};
struct Vector{
int a[2];
Vector(){
a[0] = a[1] = 0;
}
Vector operator*(const Matrix &t) const {
Vector ret = Vector();
for(int i = 0; i < 2; i++){
LL ans = 0;
for(int j = 0; j < 2; j++){
ans += 1LL * a[j] * t.a[j][i];
}
ret.a[i] = ans % mod;
}
return ret;
}
};
Matrix tr[maxn * 4], val[maxn];
void modify(int u, int l, int r, int x, const Matrix &v){
if (l == r){
tr[u] = v;
return;
}
int mid = (l + r) / 2;
if (x <= mid) modify(2 * u, l, mid, x, v);
else modify(2 * u + 1, mid + 1, r, x, v);
tr[u] = tr[2 * u + 1] * tr[2 * u];
}
Vector ret;
void query(int u, int l, int r, int L, int R){
if (l > R or r < L) return;
if (l >= L and r <= R){
ret = ret * tr[u];
return;
}
int mid = (l + r) / 2;
query(2 * u + 1, mid + 1, r, L, R);
query(2 * u, l, mid, L, R);
}
// f 0/1 : 不取/取
int f[maxn][2], h[maxn][2], c[maxn][2];
void dfs(int u){
h[u][0] = h[u][1] = 1;
for(auto j : g[u]){
dfs(j);
if (j == g[u][0]) continue;
if (f[j][0] == 0){
c[u][1] += 1;
}
else{
h[u][1] = mul(h[u][1], f[j][0]);
}
int t = pls(f[j][0], f[j][1]);
if (t == 0){
c[u][0] += 1;
}
else{
h[u][0] = mul(h[u][0], t);
}
}
f[u][0] = (c[u][0] > 0 ? 0 : h[u][0]);
f[u][1] = (c[u][1] > 0 ? 0 : h[u][1]);
if (!g[u].empty()){
f[u][1] = mul(f[u][1], f[g[u][0]][0]);
f[u][0] = mul(f[u][0], f[g[u][0]][0] + f[g[u][0]][1]);
}
val[u].a[0][0] = (c[u][0] > 0 ? 0 : h[u][0]);
val[u].a[0][1] = (c[u][1] > 0 ? 0 : h[u][1]);
val[u].a[1][0] = (c[u][0] > 0 ? 0 : h[u][0]);
val[u].a[1][1] = 0;
}
void build(int u, int l, int r){
if (l == r){
tr[u] = val[seq[r]];
return;
}
int mid = (l + r) / 2;
build(2 * u, l, mid);
build(2 * u + 1, mid + 1, r);
tr[u] = tr[2 * u + 1] * tr[2 * u];
}
int typ[maxn];
void del(int u, int f0, int f1){
if (f0 == 0){
c[u][1] -= 1;
}
else{
h[u][1] = mul(h[u][1], qpow(f0, mod - 2));
}
int t = pls(f0, f1);
if (t == 0){
c[u][0] -= 1;
}
else{
h[u][0] = mul(h[u][0], qpow(t, mod - 2));
}
}
void add(int u, int f0, int f1){
if (f0 == 0){
c[u][1] += 1;
}
else{
h[u][1] = mul(h[u][1], f0);
}
int t = pls(f0, f1);
if (t == 0){
c[u][0] += 1;
}
else{
h[u][0] = mul(h[u][0], t);
}
}
// 0 : 都可以 1 : 只能不选 2 : 只能选
void modify(int x, int type){
typ[x] = type;
if (type == 0){
val[x].a[0][0] = (c[x][0] > 0 ? 0 : h[x][0]);
val[x].a[0][1] = (c[x][1] > 0 ? 0 : h[x][1]);
val[x].a[1][0] = (c[x][0] > 0 ? 0 : h[x][0]);
val[x].a[1][1] = 0;
}
else if (type == 1){
val[x].a[0][0] = (c[x][0] > 0 ? 0 : h[x][0]);
val[x].a[0][1] = 0;
val[x].a[1][0] = (c[x][0] > 0 ? 0 : h[x][0]);
val[x].a[1][1] = 0;
}
else{
val[x].a[0][0] = 0;
val[x].a[0][1] = (c[x][1] > 0 ? 0 : h[x][1]);
val[x].a[1][0] = 0;
val[x].a[1][1] = 0;
}
while(x){
ret.a[0] = 1, ret.a[1] = 0;
query(1, 1, ts, in[top[x]], out[top[x]]);
int f0 = ret.a[0], f1 = ret.a[1];
modify(1, 1, ts, in[x], val[x]);
ret.a[0] = 1, ret.a[1] = 0;
query(1, 1, ts, in[top[x]], out[top[x]]);
int f2 = ret.a[0], f3 = ret.a[1];
x = fa[top[x]];
if (x){
del(x, f0, f1);
add(x, f2, f3);
if (typ[x] == 0){
val[x].a[0][0] = (c[x][0] > 0 ? 0 : h[x][0]);
val[x].a[0][1] = (c[x][1] > 0 ? 0 : h[x][1]);
val[x].a[1][0] = (c[x][0] > 0 ? 0 : h[x][0]);
val[x].a[1][1] = 0;
}
else if (typ[x] == 1){
val[x].a[0][0] = (c[x][0] > 0 ? 0 : h[x][0]);
val[x].a[0][1] = 0;
val[x].a[1][0] = (c[x][0] > 0 ? 0 : h[x][0]);
val[x].a[1][1] = 0;
}
else{
val[x].a[0][0] = 0;
val[x].a[0][1] = (c[x][1] > 0 ? 0 : h[x][1]);
val[x].a[1][0] = 0;
val[x].a[1][1] = 0;
}
}
}
}
int cnt[maxn];
int main(){
#ifdef LOCAL
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int n, m;
cin >> n >> m;
vector<pair<int, int> > edge;
for(int i = 1; i <= n; i++) p[i] = i;
vector<int> p;
for(int i = 1; i <= m; i++){
int a, b;
cin >> a >> b;
if (merge(a, b)){
g[a].push_back(b);
g[b].push_back(a);
}
else{
edge.push_back({a, b});
p.push_back(a);
p.push_back(b);
}
}
sort(p.begin(), p.end());
p.erase(unique(p.begin(), p.end()), p.end());
dep[1] = 1;
dfs_sz(1);
top[1] = 1;
dfs_hld(1);
dfs(1);
build(1, 1, ts);
int ans = 0;
// 容斥
auto bf = [&](auto &&bf, int u, int sign) -> void {
if (u == edge.size()){
for(auto x : p){
modify(x, cnt[x] ? 2 : 0);
}
ret.a[0] = 1, ret.a[1] = 0;
query(1, 1, ts, in[1], out[1]);
add(ans, mul(sign, ret.a[0]));
add(ans, mul(sign, ret.a[1]));
return;
}
auto [x, y] = edge[u];
// 同时选
{
cnt[x] += 1;
cnt[y] += 1;
bf(bf, u + 1, mod - sign);
cnt[x] -= 1;
cnt[y] -= 1;
}
{
bf(bf, u + 1, sign);
}
};
bf(bf, 0, 1);
cout << ans << '\n';
}
详细
Pretests
Final Tests
Test #1:
score: 5
Accepted
time: 0ms
memory: 15932kb
input:
12 17 12 3 12 5 11 12 7 5 5 10 1 5 5 8 8 7 11 9 3 6 10 1 1 9 1 7 12 4 2 9 11 2 7 3
output:
232
result:
ok 1 number(s): "232"
Test #2:
score: 5
Accepted
time: 0ms
memory: 15796kb
input:
16 20 16 13 2 9 10 2 12 16 8 5 7 5 11 1 15 5 1 5 3 12 3 7 7 1 14 9 7 4 5 13 13 9 16 11 12 6 16 9 14 13
output:
2224
result:
ok 1 number(s): "2224"
Test #3:
score: 5
Accepted
time: 2ms
memory: 15788kb
input:
20 26 16 17 9 1 5 4 19 8 17 12 6 4 2 15 1 12 8 15 10 8 13 18 6 20 6 11 2 18 15 16 3 15 17 7 7 3 6 3 18 14 12 9 10 15 4 18 12 7 6 12 4 2
output:
14970
result:
ok 1 number(s): "14970"
Test #4:
score: 5
Accepted
time: 7ms
memory: 15784kb
input:
20 30 9 6 11 1 16 4 15 9 7 12 15 8 13 12 19 20 13 1 3 8 12 10 17 18 5 4 3 15 19 17 9 5 5 11 6 2 17 7 10 19 16 14 4 14 11 2 6 16 20 10 2 1 18 3 19 18 18 8 13 7
output:
5108
result:
ok 1 number(s): "5108"
Test #5:
score: 5
Accepted
time: 40ms
memory: 26724kb
input:
100000 99999 32392 20339 53764 35809 75457 35809 35809 58597 55219 68809 88911 33189 35809 67893 66678 54083 35809 94700 24829 35809 27962 33187 35809 23269 53084 41158 25935 54398 59133 221 35809 46885 28772 68400 68729 51110 35809 80652 92799 51189 48432 49245 83595 35809 43422 26670 39330 35809 8...
output:
916590208
result:
ok 1 number(s): "916590208"
Test #6:
score: 5
Accepted
time: 33ms
memory: 25816kb
input:
100000 99999 14518 14600 50309 76720 14600 51460 23212 88291 14600 52071 14600 19701 53669 14600 10873 25204 41886 8245 14600 71848 59167 14600 33871 14600 14600 72940 14600 70022 14600 63214 41972 14600 99116 50290 14600 79116 64263 50704 59304 14600 14600 70237 14600 81199 45762 14104 95334 4873 1...
output:
17571874
result:
ok 1 number(s): "17571874"
Test #7:
score: 5
Accepted
time: 30ms
memory: 24624kb
input:
100000 100000 77527 60422 4255 83658 83658 22032 29878 10808 93167 83658 65225 45617 99708 83658 47725 24385 24385 86739 74606 62233 14041 33405 87670 86749 24949 83658 43317 73904 83428 31861 24467 33008 44793 4052 44293 66707 24385 38917 93604 24385 75286 83658 32645 10004 31921 51144 19189 83658 ...
output:
875644714
result:
ok 1 number(s): "875644714"
Test #8:
score: 5
Accepted
time: 35ms
memory: 26224kb
input:
100000 100000 57974 91743 30424 13671 56134 64712 83699 77403 83699 53181 83699 37610 9296 33292 49327 83699 65434 94635 14853 25476 26187 2450 68251 87824 19930 76281 83699 6139 83699 76917 81232 83699 25234 60195 89702 42232 53669 37341 61139 44991 64845 57276 61885 83656 50713 34422 60432 96523 7...
output:
706054454
result:
ok 1 number(s): "706054454"
Test #9:
score: 5
Accepted
time: 0ms
memory: 16344kb
input:
3000 3001 2740 142 2303 2416 1076 1574 1820 2836 2559 1881 2922 641 416 2768 641 492 887 161 2286 84 933 1693 1044 1165 1751 2403 388 404 1 1434 426 1648 2235 640 1677 1966 2947 1165 710 520 84 107 1491 2342 702 2757 282 1251 1008 216 115 2551 2179 641 2364 223 1027 163 2046 688 758 2335 274 2518 17...
output:
697563451
result:
ok 1 number(s): "697563451"
Test #10:
score: 5
Accepted
time: 39ms
memory: 24376kb
input:
100000 100001 63365 65156 11798 44234 5717 99110 84416 98591 16199 3054 61401 66549 44234 35245 66549 75715 52041 66549 32734 72885 67114 11639 66549 54423 2637 61970 75609 18743 89867 4885 46295 68927 7195 40538 30361 44234 6524 98591 56173 98591 19554 17364 36407 22815 74609 66549 31359 26544 2811...
output:
493821000
result:
ok 1 number(s): "493821000"
Test #11:
score: 5
Accepted
time: 42ms
memory: 25116kb
input:
100000 100001 53867 46530 9811 55431 75666 11471 27116 88802 74930 60622 60891 61514 28053 45599 17973 79826 19170 76282 49867 46246 51036 61522 20740 81057 58582 33219 49252 39579 22618 71872 45044 49068 47262 94529 2865 39782 26616 42759 20171 8265 75937 47764 88870 95542 61474 82361 11508 44405 4...
output:
657397785
result:
ok 1 number(s): "657397785"
Test #12:
score: 5
Accepted
time: 0ms
memory: 16264kb
input:
3000 3005 1780 2822 2394 2015 2380 34 1626 2234 533 1938 1369 2016 410 2915 1126 1504 2833 305 2173 2994 2337 1425 130 2403 527 1922 1855 1430 730 462 1028 2785 1050 2721 996 1886 2389 864 612 1149 1728 770 351 468 236 2014 1954 2533 2212 1980 2928 2235 2221 305 2129 432 2343 2472 2174 1432 559 1541...
output:
398671044
result:
ok 1 number(s): "398671044"
Test #13:
score: 5
Accepted
time: 3ms
memory: 14188kb
input:
3000 3007 2036 1470 30 1506 1745 2056 902 150 934 634 2517 278 1586 922 1242 2770 625 1697 1158 1087 27 1554 1584 1841 2599 1661 2540 2917 1085 1741 2646 2147 735 531 99 2430 824 2509 888 1029 2290 1600 2776 1746 60 1726 108 2563 1022 2562 1944 1545 2051 883 2100 2902 450 375 908 685 1397 2065 2339 ...
output:
230757972
result:
ok 1 number(s): "230757972"
Test #14:
score: 5
Accepted
time: 48ms
memory: 16012kb
input:
3000 3010 1408 1582 1555 150 1084 1811 649 2975 402 2023 2598 1076 2937 2225 2526 740 2746 213 2697 2219 2776 675 1286 795 2168 97 1169 298 325 1096 1052 2669 2746 611 2138 2908 2005 325 2746 769 325 854 2254 2935 2230 1286 719 972 1366 1315 2148 2740 2629 2746 2379 2746 2946 1712 3000 1265 2022 128...
output:
48410151
result:
ok 1 number(s): "48410151"
Test #15:
score: 5
Accepted
time: 36ms
memory: 24284kb
input:
100000 100005 97705 85617 96123 22752 4855 97705 13215 97705 97705 72380 33074 97705 38392 54902 62927 98740 76899 78423 97705 2659 97705 4298 3175 97705 6009 32050 12016 97705 90393 97705 23742 25666 49391 32175 57177 44975 6539 6924 97705 73819 65499 8286 53255 23451 97705 33816 15618 65534 16343 ...
output:
448371995
result:
ok 1 number(s): "448371995"
Test #16:
score: 5
Accepted
time: 49ms
memory: 23372kb
input:
100000 100007 14365 35614 53038 97837 56604 61091 60313 57826 16683 74638 53800 62081 76247 99523 25024 17092 12006 63706 63253 72126 83001 90837 60845 92771 26106 70067 23778 73704 83435 29095 46101 34163 91697 20100 85333 6425 89372 1100 65132 96636 3585 8901 84056 57644 68098 74112 96683 21407 18...
output:
277724501
result:
ok 1 number(s): "277724501"
Test #17:
score: 5
Accepted
time: 40ms
memory: 23644kb
input:
100000 100008 31442 23186 23186 41334 23186 96011 84352 23186 23186 40711 88994 23186 64418 15196 23186 67267 23186 65394 17712 70451 23186 75278 13007 21300 23186 95341 24962 27572 23186 82033 23186 45982 94774 9792 23186 5220 23186 35174 60987 58698 23186 22305 76318 90005 63661 23186 20580 46873 ...
output:
147196989
result:
ok 1 number(s): "147196989"
Test #18:
score: 5
Accepted
time: 63ms
memory: 24760kb
input:
100000 100009 34907 38803 7450 72560 83182 64435 92624 45807 93747 75963 79314 41003 21952 10077 13504 11569 29792 7673 57861 23262 66613 34273 4845 34259 73874 24347 69055 94637 16747 92405 23991 98394 55698 8205 68882 69094 61334 4894 23977 2137 63425 62821 77605 61405 42416 90251 7826 97504 93177...
output:
359718847
result:
ok 1 number(s): "359718847"
Test #19:
score: 5
Accepted
time: 93ms
memory: 24224kb
input:
100000 100010 3016 93258 70450 13821 37969 3851 8760 36723 66439 14460 11025 3016 93431 53948 3016 70858 34884 77973 3016 47674 6193 19991 64581 75392 43814 11224 66790 2042 72387 63669 43748 3016 48356 34864 82539 95681 71889 3016 22321 88423 32614 49681 729 45856 16914 85378 92316 87337 64017 3535...
output:
776517410
result:
ok 1 number(s): "776517410"
Test #20:
score: 5
Accepted
time: 86ms
memory: 24012kb
input:
100000 100010 23575 71188 44140 78392 74845 61272 44476 251 66072 28834 56126 43475 33545 57201 13725 26479 38652 66072 48989 12714 72584 27861 26479 25441 97047 52072 66172 7824 18585 26479 53827 26479 26479 50436 10659 50224 12390 43480 57944 26479 6121 89497 62541 57157 26479 40375 47984 26479 26...
output:
901419468
result:
ok 1 number(s): "901419468"