結果
問題 | No.1813 Magical Stones |
ユーザー |
![]() |
提出日時 | 2023-01-25 18:35:14 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 172 ms / 2,000 ms |
コード長 | 7,599 bytes |
コンパイル時間 | 2,514 ms |
コンパイル使用メモリ | 214,728 KB |
最終ジャッジ日時 | 2025-02-10 06:47:15 |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 40 |
ソースコード
#include <bits/stdc++.h>// clang-format offstd::ostream&operator<<(std::ostream&os,std::int8_t x){return os<<(int)x;}std::ostream&operator<<(std::ostream&os,std::uint8_t x){return os<<(int)x;}std::ostream&operator<<(std::ostream&os,const __int128_t &v){if(!v)os<<"0";__int128_t tmp=v<0?(os<<"-",-v):v;std::string s;while(tmp)s+='0'+(tmp%10),tmp/=10;return std::reverse(s.begin(),s.end()),os<<s;}std::ostream&operator<<(std::ostream&os,const __uint128_t &v){if(!v)os<<"0";__uint128_t tmp=v;std::string s;while(tmp)s+='0'+(tmp%10),tmp/=10;returnstd::reverse(s.begin(),s.end()),os<<s;}#define checkpoint() (void(0))#define debug(x) (void(0))#define debugArray(x,n) (void(0))#define debugMatrix(x,h,w) (void(0))// clang-format on#ifdef __LOCAL// clang-format off#undef checkpoint#undef debug#undef debugArray#undef debugMatrixtemplate<class T, class U>std::ostream &operator<<(std::ostream&os,const std::pair<T,U>&x){return os<<"("<<x.first<<", "<<x.second<<")";}template<typename T>std::ostream &operator<<(std::ostream&os,const std::vector<T>&vec){os<<'[';for(int _=0,__= vec.size();_<__;++_)os<<(_ ?", ":"")<<vec[_];return os<<']';}template<typename T>std::ostream &operator<<(std::ostream&os,const std::set<T>&s){os<<'{';int _=0;for(const auto &x:s)os<<(_++ ? ", " : "")<<x;return os << '}';}template<typename T,std::size_t _Nm>std::ostream&operator<<(std::ostream &os,const std::array<T, _Nm> &arr) {os<<'['<<arr[0];for(std::size_t _=1;_<_Nm;++_)os<<", "<<arr[_];return os<<']';}template<class Tup,std::size_t... I>void print(std::ostream&os,const Tup &x,std::index_sequence<I...>){(void)(int[]){(os<<std::get<I>(x)<<", ",0)...};}template<class... Args>std::ostream &operator<<(std::ostream&os,const std::tuple<Args...> &x) {static constexpr std::size_t N = sizeof...(Args);os<<"(";if constexpr(N>=2)print(os,x,std::make_index_sequence<N-1>());return os<<std::get<N-1>(x)<<")";}const std::string COLOR_RESET="\033[0m",BRIGHT_GREEN="\033[1;32m",BRIGHT_RED="\033[1;31m",BRIGHT_CYAN="\033[1;36m",NORMAL_CROSSED="\033[0;9;37m",ITALIC="\033[3m",BOLD="\033[1m",RED_BACKGROUND="\033[1;41m",NORMAL_FAINT="\033[0;2m";#define func_LINE_FILE NORMAL_FAINT<<" in "<<BOLD<<__func__<<NORMAL_FAINT<<ITALIC<<" (L"<<__LINE__<<") "<< __FILE__<<COLOR_RESET#define checkpoint() std::cerr<<BRIGHT_RED<<"< check point! >"<<func_LINE_FILE<<'\n'#define debug(x) std::cerr<<BRIGHT_CYAN<<#x<<COLOR_RESET<<" = "<<(x)<<func_LINE_FILE<<'\n'#define debugArray(x, n) do{std::cerr<<BRIGHT_CYAN<<#x<<COLOR_RESET<<" = ["<<x[0];for(int _=1;_<(int)(n);++_)std::cerr<<", "<<x[_];std::cerr<<"]"<<func_LINE_FILE<<'\n';}while(0)#define debugMatrix(x, h, w) do{std::cerr<<BRIGHT_CYAN<<#x<<"\n"<<COLOR_RESET<<"= ";for(int _=0;(_)<(int)(h);++_){std::cerr<<((_?" [":"[["));for(int __=0;__<(int)(w);++__)std::cerr<<((__?", ":""))<<x[_][__];std::cerr<<"]"<<(_+1==(int)(h)?"]":",\n");}std::cerr<<func_LINE_FILE<<'\n';}while(0)#endif// clang-format onclass StronglyConnectedComponents {std::vector<std::vector<int>> adj, rev;public:StronglyConnectedComponents(int n): adj(n), rev(n) {}void add_edge(int src, int dst) { adj[src].push_back(dst), rev[dst].push_back(src); }std::vector<std::vector<int>> get_block() const {const int n= adj.size();std::vector<std::vector<int>> blk;std::vector<int> ord(n), par(n, -2), dat(n, 0);int k= n;for (int s= 0; s < n; ++s)if (par[s] == -2) {par[s]= -1;for (int p= s; p >= 0;) {if (dat[p] == (int)adj[p].size()) {ord[--k]= p, p= par[p];continue;}if (int q= adj[p][dat[p]++]; par[q] == -2) par[q]= p, p= q;}}dat.assign(n, 1);for (int s: ord)if (dat[s]) {blk.resize(++k), dat[s]= 0, blk.back().push_back(s);for (int i= 0; i < (int)blk.back().size(); ++i)for (int v: rev[blk.back()[i]])if (dat[v]) dat[v]= 0, blk.back().push_back(v);}return blk;}std::vector<int> get_index(const std::vector<std::vector<int>> &blk) const {std::vector<int> index(adj.size());for (int i= blk.size(); i--;)for (int v: blk[i]) index[v]= i;return index;}std::vector<std::vector<int>> get_dag(const std::vector<int> &index, int num) const {std::vector<std::vector<int>> dag(num);std::vector<std::array<int, 2>> es;for (int i= adj.size(); i--;)for (int j: adj[i])if (int u= index[i], v= index[j]; u != v) es.push_back({u, v});std::sort(es.begin(), es.end()), es.erase(std::unique(es.begin(), es.end()), es.end());for (auto [u, v]: es) dag[u].push_back(v);return dag;}};class TwoSatisfiability {int sz;StronglyConnectedComponents scc;public:TwoSatisfiability(int n): sz(n), scc(2 * n) {}void add_if(int u, int v) { scc.add_edge(u, v), scc.add_edge(neg(v), neg(u)); } // u -> v <=> !v -> !uvoid add_or(int u, int v) { add_if(neg(u), v); } // u or v <=> !u -> vvoid add_nand(int u, int v) { add_if(u, neg(v)); } // u nand v <=> u -> !vvoid set_true(int u) { scc.add_edge(neg(u), u); } // u <=> !u -> uvoid set_false(int u) { scc.add_edge(u, neg(u)); } // !u <=> u -> !uinline int neg(int x) const { return x >= sz ? x - sz : x + sz; }std::vector<bool> solve() const {std::vector<int> I= scc.get_index(scc.get_block());std::vector<bool> ret(sz);for (int i= 0; i < sz; i++) {if (I[i] == I[neg(i)]) return {}; // no solutionret[i]= I[i] > I[neg(i)];}return ret;}};using namespace std;namespace yosupo_scc {signed main() {cin.tie(0);ios::sync_with_stdio(0);int N, M;cin >> N >> M;StronglyConnectedComponents scc(N);for (int i= 0; i < M; ++i) {int a, b;cin >> a >> b;scc.add_edge(a, b);}auto ans= scc.get_block();cout << ans.size() << '\n';for (const auto &blk: ans) {cout << blk.size();for (int v: blk) cout << " " << v;cout << '\n';}return 0;}}namespace yukicoder1293 {// https://yukicoder.me/problems/no/1293signed main() {cin.tie(0);ios::sync_with_stdio(0);int N, D, W;cin >> N >> D >> W;StronglyConnectedComponents scc(N + N);for (int i= 0; i < D; ++i) {int a, b;cin >> a >> b;--a, --b;scc.add_edge(a, b), scc.add_edge(b, a);}for (int i= 0; i < W; ++i) {int c, d;cin >> c >> d;c+= N - 1, d+= N - 1;scc.add_edge(c, d), scc.add_edge(d, c);}for (int i= 0; i < N; ++i) scc.add_edge(i, i + N);auto blk= scc.get_block();int C= blk.size();auto index= scc.get_index(blk);auto dag= scc.get_dag(index, C);long long dp[C];fill_n(dp, C, 0);for (int i= 0; i < N; ++i) ++dp[index[i]];for (int i= 0; i < C; ++i)for (int j: dag[i]) dp[j]+= dp[i];long long ans= 0;for (int i= 0; i < N; ++i) ans+= dp[index[i + N]] - 1;cout << ans << '\n';return 0;}}namespace yukicoder1813 {// https://yukicoder.me/problems/no/1813signed main() {cin.tie(0);ios::sync_with_stdio(0);int N, M;cin >> N >> M;StronglyConnectedComponents scc(N);for (int i= 0; i < M; ++i) {int A, B;cin >> A >> B;scc.add_edge(--A, --B);}auto blk= scc.get_block();int C= blk.size();if (C == 1) {cout << 0 << '\n';} else {auto dag= scc.get_dag(scc.get_index(blk), C);int cnt[2]= {0, 0};bool st[C];fill_n(st, C, true);for (int i= 0; i < C; ++i) {if (st[i]) ++cnt[0];for (int j: dag[i]) st[j]= false;if (!dag[i].size()) ++cnt[1];}cout << max(cnt[0], cnt[1]) << '\n';}return 0;}}signed main() {cin.tie(0);ios::sync_with_stdio(0);// yosupo_scc::main();// yukicoder1293::main();yukicoder1813::main();return 0;}