QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#226411 | #6225. 生成树 | SorahISA | 60 | 128ms | 34504kb | C++20 | 9.2kb | 2023-10-25 22:33:05 | 2023-10-25 22:33:06 |
Judging History
answer
#ifndef SorahISA
#define SorahISA
#include SorahISA __FILE__ SorahISA
const int mod = 998'244'353;
struct Edge {
int t = 1, f = 1; /// true, false
};
void solve() {
int N, M; cin >> N >> M;
vector<map<int, int>> adj(N); /// adj[fr][to]: eid
vector<Edge> edges;
vector<int> deg(N, 0);
vector<deque<int>> que(3); /// degree 1 and 2
for (int i = 0; i < M; ++i) {
int u, v; cin >> u >> v, --u, --v;
adj[u][v] = adj[v][u] = SZ(edges);
edges.eb(Edge{.t = 1, .f = 1});
++deg[u], ++deg[v];
}
for (int i = 0; i < N; ++i) {
if (deg[i] <= 2) que[deg[i]].eb(i);
}
int ans = 1;
auto remove_degree_1 = [&](int u) {
auto [v, eid] = *begin(adj[u]);
adj[u].clear(), adj[v].erase(u);
ans = ans * edges[eid].t % mod;
deg[u] = 0;
if (--deg[v] and deg[v] <= 2) que[deg[v]].eb(v);
};
auto contract_degree_2 = [&](int u) {
auto [v, eid_v] = *begin(adj[u]);
auto [w, eid_w] = *next(begin(adj[u]));
adj[u].clear(), adj[v].erase(u), adj[w].erase(u);
Edge tmp{
.t = edges[eid_v].t * edges[eid_w].t % mod,
.f = (edges[eid_v].t * edges[eid_w].f + edges[eid_v].f * edges[eid_w].t) % mod
};
if (adj[v].contains(w)) {
/// multiple edges ///
int eid_vw = adj[v][w];
adj[v][w] = adj[w][v] = SZ(edges);
edges.eb(Edge{
.t = (edges[eid_vw].t * tmp.f + edges[eid_vw].f * tmp.t) % mod,
.f = edges[eid_vw].f * tmp.f % mod
});
if (--deg[v] and deg[v] <= 2) que[deg[v]].eb(v);
if (--deg[w] and deg[w] <= 2) que[deg[w]].eb(w);
}
else {
adj[v][w] = adj[w][v] = SZ(edges);
edges.eb(tmp);
}
deg[u] = 0;
};
while (SZ(que[1]) or SZ(que[2])) {
if (SZ(que[1])) {
int u = que[1][0]; que[1].pf();
if (deg[u] != 1) continue;
remove_degree_1(u);
}
else {
int u = que[2][0]; que[2].pf();
if (deg[u] != 2) continue;
contract_degree_2(u);
}
}
cout << ans << "\n";
}
int32_t main() {
fastIO();
int t = 1; // cin >> t;
for (int _ = 1; _ <= t; ++_) {
// cout << "Case #" << _ << ": ";
solve();
}
return 0;
}
#else
#ifdef local
#define _GLIBCXX_DEBUG 1
#endif
#pragma GCC optimize("Ofast", "unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define int int64_t
// #define double __float80
using pii = pair<int, int>;
template <typename T> using Prior = std::priority_queue<T>;
template <typename T> using prior = std::priority_queue<T, vector<T>, greater<T>>;
// #define X first
// #define Y second
#define eb emplace_back
#define ef emplace_front
#define ee emplace
#define pb pop_back
#define pf pop_front
#define ALL(x) begin(x), end(x)
#define RALL(x) rbegin(x), rend(x)
#define SZ(x) ((int)(x).size())
template <size_t D, typename T> struct Vec : vector<Vec<D-1, T>> {
static_assert(D >= 1, "Vector dimension must be greater than zero!");
template <typename... Args> Vec(int n = 0, Args... args) : vector<Vec<D-1, T>>(n, Vec<D-1, T>(args...)) {}
};
template <typename T> struct Vec<1, T> : vector<T> {
Vec(int n = 0, const T& val = T()) : vector<T>(n, val) {}
};
template <class F>
inline constexpr decltype(auto) lambda_fix(F&& f) {
return [f = std::forward<F>(f)](auto&&... args) {
return f(f, std::forward<decltype(args)>(args)...);
};
}
#ifdef local
#define fastIO() void()
#define debug(...) \
_color.emplace_back("\u001b[31m"), \
fprintf(stderr, "%sAt [%s], line %d: (%s) = ", _color.back().c_str(), __FUNCTION__, __LINE__, #__VA_ARGS__), \
_do(__VA_ARGS__), _color.pop_back(), \
fprintf(stderr, "%s", _color.back().c_str())
#define print(...) \
fprintf(stdout, "%s", "\u001b[36m"), \
_P(__VA_ARGS__), \
fprintf(stdout, "%s", "\u001b[0m")
deque<string> _color{"\u001b[0m"};
template <typename T> concept is_string = is_same_v<T, string&> or is_same_v<T, const string&>;
template <typename T> concept is_iterable = requires (T _t) {begin(_t);};
template <typename T> inline void _print_err(T &&_t);
template <typename T> inline void _print_err(T &&_t) requires is_iterable<T> and (not is_string<T>);
template <size_t I, typename ...U> inline typename enable_if<I == sizeof...(U), void>::type _print_err(const tuple<U...> &);
template <size_t I, typename ...U> inline typename enable_if<I < sizeof...(U), void>::type _print_err(const tuple<U...> &_t);
template <size_t I, typename ...U> inline typename enable_if<I == sizeof...(U), void>::type _print_err(tuple<U...> &);
template <size_t I, typename ...U> inline typename enable_if<I < sizeof...(U), void>::type _print_err(tuple<U...> &_t);
template <typename T, typename U> ostream& operator << (ostream &os, const pair<T, U> &_tu);
inline void _do() {cerr << "\n";};
template <typename T> inline void _do(T &&_t) {_print_err(_t), cerr << "\n";}
template <typename T, typename ...U> inline void _do(T &&_t, U &&..._u) {_print_err(_t), cerr << ", ", _do(_u...);}
#else
#define fastIO() ios_base::sync_with_stdio(0), cin.tie(0)
#define debug(...) void()
#define print(...) _P(__VA_ARGS__)
#endif
inline void _P() {cout << "\n";};
template <typename T> inline void _P(T &&_t) {cout << _t << "\n";}
template <typename T, typename ...U> inline void _P(T &&_t, U &&..._u) {cout << _t << " ", _P(_u...);}
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int getRand(int L, int R) {
if (L > R) swap(L, R);
return (int)(rng() % ((uint64_t)R - L + 1) + L);
}
template <typename T, typename U> bool chmin(T &lhs, U rhs) {return lhs > rhs ? lhs = rhs, 1 : 0;}
template <typename T, typename U> bool chmax(T &lhs, U rhs) {return lhs < rhs ? lhs = rhs, 1 : 0;}
/// below are Fast I/O and _print_err templates ///
/*
/// Fast I/O by FHVirus ///
/// https://fhvirus.github.io/blog/2020/fhvirus-io/ ///
#include <unistd.h>
const int S = 65536;
int OP = 0;
char OB[S];
inline char RC() {
static char buf[S], *p = buf, *q = buf;
return p == q and (q = (p = buf) + read(0, buf, S)) == buf ? -1 : *p++;
}
inline int RI() {
static char c;
int a;
while (((c = RC()) < '0' or c > '9') and c != '-' and c != -1);
if (c == '-') {
a = 0;
while ((c = RC()) >= '0' and c <= '9') a *= 10, a -= c ^ '0';
}
else {
a = c ^ '0';
while ((c = RC()) >= '0' and c <= '9') a *= 10, a += c ^ '0';
}
return a;
}
inline void WI(int n, char c = '\n') {
static char buf[20], p;
if (n == 0) OB[OP++] = '0';
p = 0;
if (n < 0) {
OB[OP++] = '-';
while (n) buf[p++] = '0' - (n % 10), n /= 10;
}
else {
while (n) buf[p++] = '0' + (n % 10), n /= 10;
}
for (--p; p >= 0; --p) OB[OP++] = buf[p];
OB[OP++] = c;
if (OP > S-20) write(1, OB, OP), OP = 0;
}
/// Fast I/O by FHVirus ///
/// https://fhvirus.github.io/blog/2020/fhvirus-io/ ///
*/
#ifdef local
template <typename T> inline void _print_err(T &&_t) {cerr << _t;}
template <typename T> inline void _print_err(T &&_t) requires is_iterable<T> and (not is_string<T>) {
string _tmp_color = _color.back();
++_tmp_color[3], _color.emplace_back(_tmp_color);
cerr << _color.back() << "[";
for (bool _first = true; auto &_x : _t) {
if (!_first) cerr << ", ";
_print_err(_x), _first = false;
}
cerr << "]" << (_color.pop_back(), _color.back());
}
template <typename T, typename U> ostream& operator << (ostream &os, const pair<T, U> &_tu) {
string _tmp_color = _color.back();
++_tmp_color[3], _color.emplace_back(_tmp_color);
cerr << _color.back() << "(";
_print_err(_tu.first), cerr << ", ", _print_err(_tu.second);
cerr << ")" << (_color.pop_back(), _color.back());
return os;
}
template <size_t I = 0, typename ...U> inline typename enable_if<I == sizeof...(U), void>::type _print_err(const tuple<U...> &) {
cerr << ")" << (_color.pop_back(), _color.back());
}
template <size_t I = 0, typename ...U> inline typename enable_if<I < sizeof...(U), void>::type _print_err(const tuple<U...> &_t) {
if (!I) {
string _tmp_color = _color.back();
++_tmp_color[3], _color.emplace_back(_tmp_color);
cerr << _color.back();
}
cerr << (I ? ", " : "("), _print_err(get<I>(_t)), _print_err<I+1, U...>(_t);
}
template <size_t I = 0, typename ...U> inline typename enable_if<I == sizeof...(U), void>::type _print_err(tuple<U...> &) {
cerr << ")" << (_color.pop_back(), _color.back());
}
template <size_t I = 0, typename ...U> inline typename enable_if<I < sizeof...(U), void>::type _print_err(tuple<U...> &_t) {
if (!I) {
string _tmp_color = _color.back();
++_tmp_color[3], _color.emplace_back(_tmp_color);
cerr << _color.back();
}
cerr << (I ? ", " : "("), _print_err(get<I>(_t)), _print_err<I+1, U...>(_t);
}
#endif
#endif
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 2ms
memory: 3848kb
input:
1828 1828 277 1778 1778 1650 1650 1169 1778 1812 1169 1041 1650 662 662 1187 662 939 1041 224 224 1098 1169 497 1169 1443 224 1356 1169 719 662 781 1169 818 1041 514 1778 986 939 1721 818 1102 1098 1376 719 1028 818 1084 1812 79 1187 722 719 67 79 1816 1721 695 514 1458 781 696 79 656 224 159 1169 6...
output:
8
result:
ok single line: '8'
Test #2:
score: 0
Accepted
time: 1ms
memory: 3840kb
input:
1816 1816 123 240 123 1242 123 1055 240 521 1242 927 521 590 1055 1257 240 514 927 848 1257 603 590 1631 848 1419 1419 1502 603 1761 514 447 1502 609 603 313 313 1322 1631 1482 447 1361 609 1375 447 1624 1624 845 1322 9 1361 1290 1375 737 1624 1813 1290 1752 1290 586 586 933 933 1223 9 1144 1144 17 ...
output:
111
result:
ok single line: '111'
Subtask #2:
score: 10
Accepted
Test #3:
score: 10
Accepted
time: 25ms
memory: 13240kb
input:
48897 49999 44175 43993 44175 13021 13021 23273 13021 35214 44175 43686 23273 25433 25433 46383 43686 45608 13021 26466 46383 13484 23273 28961 43686 21379 25433 46792 45608 16454 44175 3160 13484 16479 46792 39917 3160 11777 39917 30437 26466 19880 39917 29973 43686 43378 26466 40225 23273 38230 30...
output:
778245294
result:
ok single line: '778245294'
Test #4:
score: 0
Accepted
time: 32ms
memory: 13032kb
input:
46495 49999 18809 25060 18809 20105 25060 14021 14021 11908 20105 25840 20105 45172 11908 2540 45172 43458 25060 42642 14021 30673 25060 11344 11344 31485 20105 38798 20105 1297 11908 23 23 8876 14021 16485 43458 37234 42642 15882 31485 34755 25060 24921 25060 4755 30673 3332 38798 42105 24921 34288...
output:
246777581
result:
ok single line: '246777581'
Subtask #3:
score: 0
Runtime Error
Test #5:
score: 10
Accepted
time: 73ms
memory: 23308kb
input:
96912 99999 71789 3940 71789 21169 21169 50408 50408 68720 68720 67449 50408 69195 69195 64168 64168 70336 69195 81723 3940 91136 21169 75782 50408 36767 71789 77653 50408 30673 30673 79224 70336 4586 77653 32309 91136 57060 36767 3517 71789 16769 75782 11348 68720 46490 64168 15995 36767 51887 5040...
output:
373712628
result:
ok single line: '373712628'
Test #6:
score: 0
Accepted
time: 84ms
memory: 25780kb
input:
93838 99999 70859 84560 84560 47786 70859 28659 47786 68306 84560 30646 68306 74308 47786 17627 28659 23677 68306 13344 30646 41537 74308 81822 13344 68192 41537 78973 68192 39506 13344 39886 78973 81944 81944 77078 81944 81305 81305 78681 39886 48824 78681 31993 39506 54883 39886 49425 49425 45444 ...
output:
839730642
result:
ok single line: '839730642'
Test #7:
score: -10
Runtime Error
input:
93217 99999 39897 48691 48691 70455 39897 66817 39897 25247 66817 34625 34625 59442 34625 82125 48691 66391 34625 70534 82125 27147 25247 28264 34625 59838 59442 69379 27147 68103 69379 22810 66391 15567 59838 15685 59838 71678 15567 28010 59838 7600 68103 69439 28010 15816 15685 39777 28010 44976 1...
output:
result:
Subtask #4:
score: 10
Accepted
Test #8:
score: 10
Accepted
time: 66ms
memory: 23104kb
input:
92616 99999 70768 12420 70768 67024 70768 37650 67024 14933 12420 81054 81054 6944 70768 9552 14933 31629 70768 51744 81054 62067 31629 49539 51744 47773 9552 19607 81054 89603 6944 2797 81054 78691 70768 39322 37650 16268 31629 34316 78691 18322 70768 46861 70768 36460 9552 42042 51744 53292 53292 ...
output:
810112214
result:
ok single line: '810112214'
Test #9:
score: 0
Accepted
time: 61ms
memory: 25532kb
input:
94017 99999 24807 46668 24807 81260 81260 28843 24807 339 24807 85051 339 89974 85051 6774 89974 54730 6774 69479 85051 51263 85051 85168 85168 1149 1149 41024 54730 70391 6774 29667 29667 43389 70391 50356 70391 50688 29667 17145 41024 92883 92883 11499 92883 48754 92883 80483 11499 3362 92883 7241...
output:
73515925
result:
ok single line: '73515925'
Subtask #5:
score: 10
Accepted
Test #10:
score: 10
Accepted
time: 79ms
memory: 23060kb
input:
91653 99999 1786 58358 1786 87672 1786 62883 1786 56997 56997 79414 87672 56317 56997 30068 58358 43013 1786 25272 56317 11600 11600 57554 25272 37668 87672 30501 79414 1591 1591 74 30501 74650 57554 83362 87672 25595 1786 19541 25272 68790 30068 59157 19541 56546 79414 59557 79414 32173 56317 63887...
output:
396960248
result:
ok single line: '396960248'
Test #11:
score: 0
Accepted
time: 73ms
memory: 23668kb
input:
98027 99999 68700 70996 68700 70969 70996 87037 68700 31954 31954 76351 70969 67408 87037 30644 76351 74201 70969 95791 30644 57725 67408 67730 30644 9543 95791 17418 95791 36144 57725 89464 17418 96341 89464 95071 9543 80303 80303 75802 36144 28295 28295 13083 36144 94918 28295 36606 94918 97547 80...
output:
142117900
result:
ok single line: '142117900'
Subtask #6:
score: 10
Accepted
Test #12:
score: 10
Accepted
time: 57ms
memory: 23752kb
input:
98982 100000 37308 3159 37308 76920 37308 67591 67591 78501 76920 30922 67591 90524 3159 70630 67591 82536 70630 85743 70630 73885 85743 51097 82536 81427 70630 75802 82536 11006 75802 11893 82536 94161 75802 66471 51097 19126 19126 4071 75802 90741 75802 8924 66471 9660 8924 50226 50226 13970 50226...
output:
3947657
result:
ok single line: '3947657'
Test #13:
score: 0
Accepted
time: 74ms
memory: 23116kb
input:
92717 100000 88071 40013 40013 12184 88071 68384 88071 54188 40013 68412 68384 32761 12184 18940 32761 70231 70231 23569 68384 23421 54188 37005 32761 8334 23421 61868 32761 82547 70231 52689 23421 88714 18940 92131 40013 51926 40013 9007 51926 90144 32761 88549 32761 19068 12184 43987 54188 44910 8...
output:
572124101
result:
ok single line: '572124101'
Subtask #7:
score: 0
Runtime Error
Test #14:
score: 0
Runtime Error
input:
99170 100000 58553 14320 14320 9444 58553 22219 14320 77126 22219 32808 58553 62144 58553 72681 72681 72235 77126 74762 72681 95464 72235 70259 95464 18298 70259 63380 18298 88666 72235 65028 70259 26344 70259 39126 65028 38489 70259 54083 26344 28793 63380 82981 88666 94908 54083 15140 94908 21117 ...
output:
result:
Subtask #8:
score: 10
Accepted
Test #16:
score: 10
Accepted
time: 114ms
memory: 34504kb
input:
148738 150000 97229 147704 147704 3732 3732 126210 147704 114875 97229 4145 114875 125662 126210 54609 126210 125530 125662 57240 3732 12701 114875 148509 125530 127724 57240 36638 54609 145259 125530 16588 16588 82269 148509 110113 12701 83842 145259 86308 36638 91488 110113 18954 86308 4751 18954 ...
output:
767723653
result:
ok single line: '767723653'
Test #17:
score: 0
Accepted
time: 128ms
memory: 34036kb
input:
138710 150000 53931 85292 85292 88589 53931 94589 85292 93632 94589 53422 53422 45414 93632 127800 45414 14891 45414 81869 88589 103316 85292 137429 53422 80797 53422 46149 85292 118979 53422 88912 81869 115796 115796 92856 53931 18436 45414 29822 127800 90152 90152 112533 46149 33694 45414 9543 534...
output:
376267810
result:
ok single line: '376267810'
Subtask #9:
score: 0
Runtime Error
Test #18:
score: 0
Runtime Error
input:
183279 200000 46233 1718 1718 145581 1718 99335 145581 149635 46233 44778 1718 7931 99335 14807 149635 86473 7931 50719 86473 86707 7931 138797 14807 106593 106593 28 86707 89350 106593 163773 106593 55983 89350 110272 89350 4686 4686 71136 110272 27450 110272 135707 71136 163719 110272 173695 71136...
output:
result:
Subtask #10:
score: 0
Runtime Error
Test #20:
score: 0
Runtime Error
input:
193116 200000 94070 92162 92162 141789 141789 191032 94070 86611 141789 146661 141789 85085 94070 15000 85085 186121 146661 64671 85085 25108 186121 152468 15000 87079 87079 29491 64671 156412 29491 192599 64671 162723 25108 10551 87079 172614 172614 122064 162723 96437 96437 61867 156412 4076 61867...