QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#125271 | #906. 强连通分量 | NOI_AK_ME# | AC ✓ | 59ms | 16848kb | C++23 | 13.7kb | 2023-07-16 14:22:24 | 2023-07-16 14:22:26 |
Judging History
answer
#pragma GCC optimize("fast-math")
#include <bits/stdc++.h>
#if __has_include("dbg.h")
# include "dbg.h"
#else
# define dbg(...) do {} while (0)
#endif
#define cp_lib_4th(_1, _2, _3, x, ...) x
#define cp_lib_rep(i, l, r) for (int i = (l); (i) < (r); ++(i))
#define cp_lib_rep0(i, r) cp_lib_rep(i, 0, r)
#define rep(...) cp_lib_4th(__VA_ARGS__, cp_lib_rep, cp_lib_rep0, _)(__VA_ARGS__)
#define cp_lib_repr(i, r, l, ...) for (int i = (r); (i) >= (l); --(i))
#define repr(...) cp_lib_repr(__VA_ARGS__, 0)
#define all(a) ::begin(a),::end(a)
#define trav(a, b) for (auto&& a : (b))
using namespace std;
using ll = long long;
using ld = long double;
[[maybe_unused]] static constexpr int INF = int(1e9 + 5);
[[maybe_unused]] static constexpr ll INFL = ll(INF) * INF;
template <class C> int sz(const C& c) { return int(::size(c)); }
#if __cpp_lib_remove_cvref < 201711L
template <class T> using remove_cvref_t = remove_cv_t<remove_reference_t<T>>;
#endif
#if __cplusplus < 202002L
struct identity { template <class T> constexpr T&& operator()(T&& t) const noexcept { return forward<T>(t); }; };
template <class T> using iter_value_t = typename iterator_traits<remove_cvref_t<T>>::value_type;
#endif
namespace cp_lib_type_meta {
template <class T, class = void> constexpr bool is_tuple_like = false;
template <class T> constexpr bool is_tuple_like<T, void_t<tuple_element_t<0, T>>> = true;
}
namespace cp_lib_modint { struct ModIntTag {}; }
template <class T> constexpr bool IsModInt = is_base_of_v<cp_lib_modint::ModIntTag, T>;
#include <unistd.h>
namespace cp_lib_io {
constexpr int BUF_SIZE = 1 << 20;
constexpr array<array<char, 4>, 10'000> DIGITS = []{
array<array<char, 4>, 10'000> digits{};
for (int i = 3, d = 1; i >= 0; --i, d *= 10)
rep(j, 10'000)
digits[j][i] = char('0' + j / d % 10);
return digits;
}();
array<char, BUF_SIZE> ibuf, obuf;
char *iptr = data(ibuf), *iend = iptr, *optr = data(obuf);
template <class T> constexpr bool is_std_array = false;
template <class T, size_t I> constexpr bool is_std_array<array<T, I>> = true;
void flush() {
for (auto* p = begin(obuf); p != optr; p += write(STDOUT_FILENO, p, optr - p));
optr = begin(obuf);
}
int _flush_atexit = []{ atexit(flush); return 0; }();
void refill() {
memmove(begin(ibuf), iptr, iend - iptr);
iend -= iptr - begin(ibuf);
iptr = begin(ibuf);
iend += read(STDIN_FILENO, iend, end(ibuf) - 1 - iend);
*iend = '\0';
}
template <class T, class T2 = remove_cvref_t<T>>
void print(T&& val) {
if (end(obuf) - optr < 64)
flush();
if constexpr (is_same_v<T2, char>)
*optr++ = val;
else if constexpr (is_same_v<T2, bool> || is_same_v<T2, vector<bool>::reference>)
return print(int(val));
else if constexpr (is_integral_v<T2> && is_signed_v<T2>) {
if (val < 0)
*optr++ = '-';
using U = make_unsigned_t<T2>;
return print(U(val < 0 ? -U(val) : U(val)));
} else if constexpr (is_integral_v<T2> && is_unsigned_v<T2>) {
T2 val2 = val;
array<char, 64> tmp;
char* tptr = end(tmp);
while (val2 >= 10'000)
tptr -= 4, memcpy(tptr, &DIGITS[val2 % 10'000][0], 4), val2 /= T2(10'000);
int d = (val2 >= 100 ? (val2 >= 1000 ? 4 : 3) : (val2 >= 10 ? 2 : 1));
memcpy(optr, &DIGITS[val2][4 - d], d);
memcpy(optr + d, tptr, end(tmp) - tptr);
optr += d + int(end(tmp) - tptr);
} else if constexpr (is_floating_point_v<T2>)
optr += sprintf(optr, "%.30Lf", (long double)val);
else if constexpr (is_convertible_v<T, string_view>) {
string_view sv(val);
if (sz(sv) + 1 <= end(obuf) - optr)
memcpy(optr, data(sv), sz(sv)), optr += sz(sv);
else {
flush();
for (auto *p = data(sv), *pe = p + sz(sv); p != pe; p += write(STDOUT_FILENO, p, pe - p));
}
} else if constexpr (IsModInt<T2>)
return print(typename T2::Int(val));
else if constexpr (cp_lib_type_meta::is_tuple_like<T2> && !is_std_array<T2>)
return apply([](auto&&... items) { (print(items), ...); }, forward<T>(val));
else {
trav(item, val)
print(item);
return;
}
*optr++ = ' ';
}
template <class T>
void read(T& val) {
auto skip_ws = [] {
do {
for (; iptr != iend && *iptr <= ' '; ++iptr);
if (iend - iptr < 64)
refill();
} while (*iptr <= ' ');
};
auto read_other = [&](auto other) {
read(other);
return other;
};
if constexpr (is_same_v<T, char>)
skip_ws(), val = *iptr++;
else if constexpr (is_same_v<T, bool> || is_same_v<T, vector<bool>::reference>) {
val = bool(read_other(uint8_t()));
} else if constexpr (IsModInt<T>) {
val = T(read_other(ll()));
} else if constexpr (is_integral_v<T>) {
skip_ws();
if (is_signed_v<T> && *iptr == '-')
++iptr, val = T(-read_other(make_unsigned_t<T>()));
else
for (val = 0; iptr != iend && *iptr > ' '; val = T(10 * val + (*iptr++ & 15)));
} else if constexpr (is_floating_point_v<T>)
skip_ws(), val = T(strtold(iptr, &iptr));
else if constexpr (is_same_v<T, string>) {
skip_ws();
val.clear();
do {
auto* after = find_if(iptr, iend, [](char c) { return c <= ' '; });
val.append(iptr, after);
if ((iptr = after) != iend)
break;
refill();
} while (iptr != iend);
} else if constexpr (cp_lib_type_meta::is_tuple_like<T> && !is_std_array<T>)
apply([](auto&... items) { (read(items), ...); }, val);
else
trav(item, val)
read(item);
}
}
using cp_lib_io::flush;
template <class... Args>
void print(Args&&... args) { (cp_lib_io::print(forward<Args>(args)), ...); }
template <class... Args>
void println(Args&&... args) {
if (sizeof...(Args))
(cp_lib_io::print(forward<Args>(args)), ...), *(cp_lib_io::optr - 1) = '\n';
else
print('\n'), --cp_lib_io::optr;
}
template <class... Args>
void read(Args&... args) { (cp_lib_io::read(args), ...); }
namespace cp_lib_span {
template <class P> constexpr auto to_address(const P& p) noexcept { return p.operator->(); }
template <class T> constexpr T* to_address(T* p) noexcept { return p; }
}
template <class T>
class MiniSpan {
T *begin_ = nullptr, *end_ = nullptr;
public:
constexpr MiniSpan() noexcept;
template <class It>
constexpr MiniSpan(It first, size_t count) : begin_(cp_lib_span::to_address(first)), end_(begin_ + count) {}
template <class It, class End>
constexpr MiniSpan(It first, End last) : MiniSpan(first, size_t(last - first)) {}
constexpr MiniSpan& operator=(const MiniSpan&) noexcept = default;
constexpr auto begin() const noexcept { return begin_; }
constexpr auto end() const noexcept { return end_; }
constexpr auto rbegin() const noexcept { return make_reverse_iterator(end_); }
constexpr auto rend() const noexcept { return make_reverse_iterator(begin_); }
constexpr size_t size() const noexcept { return end_ - begin_; }
constexpr const T& operator[](size_t i) const { return *(begin_ + i); }
};
template <class It, class EndOrSize>
MiniSpan(It, EndOrSize) -> MiniSpan<remove_reference_t<decltype(*declval<It&>())>>;
template <bool EdgeId> struct MaybeEdgeId {
int id_or_zero() const noexcept { return 0; }
};
template <> struct MaybeEdgeId<true> {
int id;
int id_or_zero() const noexcept { return id; }
};
template <class Cost> struct MaybeCost { Cost cost; };
template <> struct MaybeCost<void> {};
template <bool EdgeId, class Cost>
struct Edge : MaybeEdgeId<EdgeId>, MaybeCost<Cost> { int to; };
struct GraphOptions {
static constexpr int DIRECTED = 1, EDGE_IDS = 2;
};
template <int Options = 0, class Cost_ = void>
struct Graph {
static constexpr bool Directed = Options & GraphOptions::DIRECTED;
static constexpr bool HasEdgeIds = Options & GraphOptions::EDGE_IDS;
static constexpr bool HasCosts = !is_void_v<Cost_>;
using Cost = Cost_;
using Edge = ::Edge<HasEdgeIds, Cost>;
using EdgeIt = decltype(begin(declval<MiniSpan<Edge>>()));
using ConstEdgeIt = decltype(begin(declval<MiniSpan<const Edge>>()));
vector<Edge> edges;
vector<int> idx;
template <class BuildEdges>
explicit Graph(int n, int m_hint, BuildEdges&& build_edges) : idx(n + 1) {
vector<pair<int, Edge>> edge_list;
edge_list.reserve(m_hint);
auto add_edge = [&](int from, int to) -> Edge& {
auto& edge = edge_list.emplace_back(from, Edge{}).second;
idx[from + 1]++;
idx[to + 1] += !Directed;
edge.to = to;
return edge;
};
if constexpr (HasCosts)
build_edges([&](int from, int to, Cost cost) { add_edge(from, to).cost = cost; });
else
build_edges(add_edge);
exclusive_scan(all(idx), begin(idx), 0);
edges.resize(sz(edge_list) << int(!Directed));
rep(i, sz(edge_list)) {
auto [from, edge] = move(edge_list[i]);
if constexpr (HasEdgeIds)
edge.id = i;
if constexpr (!Directed)
(edges[idx[edge.to + 1]++] = edge).to = from;
edges[idx[from + 1]++] = move(edge);
}
}
template <class Read>
static Graph read_graph(int n, int m, int v0, Read&& read) {
return Graph(n, m, [&](auto&& add_edge) {
rep(_, m) {
int from, to;
read(from);
read(to);
if constexpr (HasCosts) {
Cost cost;
read(cost);
add_edge(from - v0, to - v0, move(cost));
} else
add_edge(from - v0, to - v0);
}
});
}
template <class Read>
static Graph read_tree(int n, int v0, Read&& read) {
return read_graph(n, n - 1, v0, forward<Read>(read));
}
template <class Read, bool C = HasCosts, enable_if_t<!C, int> = 0>
static Graph read_parent_tree(int n, int v0, Read&& read) {
return Graph(n, n - 1, [&](auto&& add_edge) {
int p;
rep(i, 1, n)
read(p), add_edge(p - v0, i);
});
}
MiniSpan<Edge> operator[](int v) noexcept { return MiniSpan(begin(edges) + idx[v], begin(edges) + idx[v + 1]); }
MiniSpan<const Edge> operator[](int v) const noexcept { return MiniSpan(begin(edges) + idx[v], begin(edges) + idx[v + 1]); }
int n() const noexcept { return sz(idx) - 1; }
int m() const noexcept { return sz(edges) >> int(!Directed); }
};
template <class Graph>
auto find_cycle(const Graph& graph) {
vector<typename Graph::EdgeIt> edges;
vector<uint8_t> state(graph.n());
auto dfs = [&](auto&& self, int v, auto inc) -> bool {
state[v] = 1;
for (auto it = begin(graph[v]), it_end = end(graph[v]); it != it_end; ++it) {
if (int v2 = it->to, e = it->id_or_zero(); state[v2] != 2 && (Graph::Directed || pair(v2, e) != inc)) {
edges.push_back(it);
if (state[v2] == 1 || self(self, v2, pair(v, e)))
return true;
edges.pop_back();
}
}
state[v] = 2;
return false;
};
rep(v0, graph.n()) {
if (!state[v0] && dfs(dfs, v0, pair(-1, -1))) {
auto it = begin(edges);
for (int vn = edges.back()->to; v0 != vn;)
v0 = (*it++)->to;
edges.erase(begin(edges), it);
break;
}
}
return edges;
}
template <class Graph, class OnComp>
void strongly_connected_components(const Graph& graph, OnComp&& on_comp) {
int n = graph.n();
vector<int> val(n), stack;
stack.reserve(n);
auto dfs = [&, on_comp = forward<OnComp>(on_comp)](auto&& self, int v) -> int {
stack.push_back(v);
int low = val[v] = sz(stack);
trav(e, graph[v])
if (val[e.to] != -1)
low = min(low, val[e.to] ? val[e.to] : self(self, e.to));
if (val[v] != low)
return val[v] = low;
auto it = prev(end(stack));
for (val[v] = -1; *it != v; val[*it--] = -1);
on_comp(MiniSpan(it, end(stack)));
stack.erase(it, end(stack));
return low;
};
rep(v, n)
if (!val[v])
dfs(dfs, v);
}
int main() {
int n, m; read(n, m);
auto graph = Graph<GraphOptions::DIRECTED>::read_graph(n, m, 0, [](auto& x) { read(x); });
vector nodes(n, 0), idx = {n};
strongly_connected_components(graph, [&](const auto& comp) {
copy(all(comp), make_reverse_iterator(begin(nodes) + idx.back()));
idx.push_back(idx.back() - sz(comp));
});
println(sz(idx) - 1);
repr(i, sz(idx) - 2) {
MiniSpan comp(begin(nodes) + idx[i + 1], begin(nodes) + idx[i]);
println(sz(comp), comp);
}
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3484kb
input:
6 7 1 4 5 2 3 0 5 5 4 1 0 3 4 2
output:
4 1 5 2 4 1 1 2 2 3 0
result:
ok OK
Test #2:
score: 0
Accepted
time: 47ms
memory: 16768kb
input:
500000 500000 389812 410922 255712 339244 274009 248522 347288 199231 235313 301629 469588 84917 364188 449407 392348 436920 26220 198288 162188 468965 274222 92196 463222 408478 231663 467768 382681 38083 412497 160479 280851 268689 101149 25450 62271 9177 38892 268598 273853 250782 191939 89247 40...
output:
499590 1 499999 1 499995 1 499994 1 499989 1 499988 1 499987 1 499983 1 499978 1 499975 1 499972 1 499969 1 499965 1 499964 1 499954 1 499949 1 499947 1 499942 1 499939 1 499932 1 499928 1 499926 1 499925 1 499924 1 499919 1 499918 1 499915 1 499910 1 499909 1 499907 1 499906 1 499904 1 499903 1 499...
result:
ok OK
Test #3:
score: 0
Accepted
time: 58ms
memory: 16756kb
input:
500000 500000 463045 412906 148756 435111 210547 370466 332006 125085 346657 373200 141009 248049 418869 36311 103205 31879 79919 221632 325039 63377 463225 21697 115522 423754 468924 422567 113104 10277 106927 168428 136657 198931 52292 164110 411164 269182 115111 229801 490548 374967 35584 124385 ...
output:
499391 1 499995 1 499994 1 499990 1 499987 1 499985 1 499984 1 499983 1 499980 1 499979 1 499978 1 499977 1 499975 1 499974 1 499973 1 499972 1 499964 1 499962 1 499961 1 499959 1 499956 1 499953 1 499951 1 499947 1 499946 1 499940 1 499936 1 499935 1 499934 1 499933 1 499932 1 499931 1 499930 1 499...
result:
ok OK
Test #4:
score: 0
Accepted
time: 49ms
memory: 16848kb
input:
500000 500000 53335 382346 455173 92221 8244 323792 50176 7825 97274 483122 91479 85438 76999 26861 226585 485061 342260 162826 198446 160509 358060 405334 116619 383398 228241 455075 383689 132273 411544 91882 201903 245543 215635 97032 216514 238391 267192 179008 82221 449619 70697 492859 212515 4...
output:
499543 1 499999 1 499998 1 499997 1 499995 1 499994 1 499993 1 499991 1 499990 1 499987 1 499984 1 499980 1 499976 1 499975 1 499973 1 499972 1 499969 1 499968 1 499960 1 499959 1 499957 1 499954 1 499952 1 499950 1 499948 1 499947 1 499946 1 499941 1 499940 1 499938 1 499937 1 499934 1 499927 1 499...
result:
ok OK
Test #5:
score: 0
Accepted
time: 59ms
memory: 16756kb
input:
500000 500000 429248 25838 130385 474090 301066 98435 160342 325081 58836 93676 332873 451375 243336 362476 378833 389318 68972 375267 209749 483838 405727 431354 477871 331401 30320 382468 228018 369242 53158 111145 464333 346455 436090 431682 22494 69335 128989 418926 392117 10791 347423 40861 389...
output:
499908 1 499998 1 499994 1 499989 1 499987 1 499986 1 499985 1 499983 1 499980 1 499979 1 499976 1 499975 1 499974 1 499973 1 499972 1 499968 1 499965 1 499964 1 499963 1 499958 1 499954 1 499945 1 499944 1 499932 1 499931 1 499927 1 499925 1 499924 1 499921 1 499917 1 499913 1 499908 1 499906 1 499...
result:
ok OK
Test #6:
score: 0
Accepted
time: 52ms
memory: 16744kb
input:
500000 500000 277011 99116 287358 208082 234935 137730 116691 98476 82482 189249 370363 155677 444563 130877 78567 341204 189970 68991 431098 85511 440553 434235 156042 397537 261550 375332 9361 490701 94399 397514 462108 395197 236023 224717 77596 260090 171065 59494 72281 195466 37329 322314 13123...
output:
499959 1 499997 1 499996 1 499992 1 499983 1 499981 1 499980 1 499979 1 499978 1 499968 1 499967 1 499966 1 499964 1 499963 1 499961 1 499959 1 499958 1 499957 1 499956 1 499950 1 499948 1 499945 1 499944 1 499943 1 499939 1 499938 1 499936 1 499933 1 499930 1 499929 1 499924 1 499923 1 499920 1 499...
result:
ok OK
Test #7:
score: 0
Accepted
time: 35ms
memory: 14924kb
input:
389813 410923 255712 339244 274009 248522 347288 199231 235313 301629 84917 364188 26220 198288 162188 274222 92196 231663 382681 38083 160479 280851 268689 101149 25450 62271 9177 38892 268598 273853 250782 191939 89247 384002 197410 212876 258774 238988 55975 128576 220526 321996 203733 68224 2156...
output:
386899 1 389808 1 389806 1 389805 1 389804 1 389803 1 389800 1 389798 1 389796 1 389792 1 389787 1 389777 1 389775 1 389773 1 389769 1 389767 1 389766 1 389760 1 389759 1 389755 1 389753 1 389749 1 389748 1 389746 1 389745 1 389742 1 389741 1 389739 1 389734 1 389729 1 389727 1 389724 1 389723 1 389...
result:
ok OK
Test #8:
score: 0
Accepted
time: 35ms
memory: 16016kb
input:
463046 412907 148756 435111 210547 370466 332006 125085 346657 373200 141009 248049 418869 36311 103205 31879 79919 221632 325039 63377 21697 115522 423754 422567 113104 10277 106927 168428 136657 198931 52292 164110 411164 269182 115111 229801 374967 35584 124385 307573 453747 96444 292667 195578 1...
output:
463024 1 463041 1 463040 1 463039 1 463037 1 463033 1 463028 1 463026 1 463024 1 463022 1 463020 1 463018 1 463016 1 463015 1 463014 1 463011 1 463009 1 463008 1 463007 1 463000 1 462998 1 462996 1 462991 1 462988 1 462987 1 462986 1 462983 1 462982 1 462980 1 462979 1 462976 1 462975 1 462974 1 462...
result:
ok OK
Test #9:
score: 0
Accepted
time: 7ms
memory: 9636kb
input:
53336 382347 26685 8244 50176 7825 31738 24370 25943 19902 11463 26861 29977 26309 14580 31754 1838 29437 30380 12118 51083 31633 1201 18328 26346 5295 48935 19027 31496 19906 41783 5048 47936 16685 5161 34107 15907 28002 332 27672 28930 39563 36429 31696 17876 726 42526 21682 35319 8727 17974 25252...
output:
87 1 52544 1 52131 1 52091 1 51147 1 49821 1 49292 1 49069 1 49039 1 48041 1 41786 1 38700 1 35935 1 32654 1 31150 1 30693 1 30131 1 29555 1 28488 1 28249 1 27910 1 27701 1 25764 1 24488 1 24291 1 23200 1 20997 1 18187 1 12568 1 11778 1 10979 1 7881 1 7456 1 5924 1 4989 1 4368 1 2827 1 1694 53250 38...
result:
ok OK
Test #10:
score: 0
Accepted
time: 14ms
memory: 10688kb
input:
429249 25839 130385 301066 98435 160342 325081 58836 93676 332873 243336 362476 378833 389318 68972 375267 209749 405727 331401 30320 382468 228018 369242 53158 111145 346455 22494 69335 128989 418926 392117 10791 347423 40861 389364 17417 261217 42171 167706 71369 107485 278424 39639 144680 60220 1...
output:
429249 1 429248 1 429247 1 429246 1 429245 1 429244 1 429243 1 429242 1 429241 1 429240 1 429239 1 429238 1 429237 1 429236 1 429235 1 429234 1 429232 1 429231 1 429230 1 429229 1 429228 1 429227 1 429225 1 429224 1 429223 1 429222 1 429221 1 429220 1 429219 1 429218 1 429217 1 429216 1 429215 1 429...
result:
ok OK
Test #11:
score: 0
Accepted
time: 13ms
memory: 10628kb
input:
277012 99117 208082 234935 137730 116691 98476 82482 189249 155677 130877 78567 189970 68991 85511 156042 261550 9361 94399 236023 224717 77596 260090 171065 59494 72281 195466 37329 131235 97595 186133 259682 77924 208661 225412 201669 250472 163723 144340 159695 17876 71198 230537 223132 49533 606...
output:
277012 1 277010 1 277009 1 277008 1 277007 1 277005 1 277004 1 277003 1 277002 1 277001 1 277000 1 276999 1 276998 1 276997 1 276995 1 276994 1 276993 1 276992 1 276991 1 276990 1 276989 1 276988 1 276987 1 276986 1 276985 1 276982 1 276981 1 276980 1 276979 1 276978 1 276977 1 276976 1 276975 1 276...
result:
ok OK