結果
問題 | No.590 Replacement |
ユーザー | Pachicobue |
提出日時 | 2017-11-04 17:13:32 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,909 bytes |
コンパイル時間 | 2,497 ms |
コンパイル使用メモリ | 215,340 KB |
実行使用メモリ | 13,440 KB |
最終ジャッジ日時 | 2024-05-02 22:45:50 |
合計ジャッジ時間 | 4,776 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | AC | 2 ms
5,376 KB |
testcase_34 | AC | 2 ms
5,376 KB |
testcase_35 | AC | 1 ms
5,376 KB |
testcase_36 | WA | - |
testcase_37 | WA | - |
testcase_38 | AC | 33 ms
7,168 KB |
testcase_39 | AC | 32 ms
7,168 KB |
testcase_40 | WA | - |
testcase_41 | WA | - |
testcase_42 | AC | 35 ms
7,168 KB |
testcase_43 | AC | 36 ms
7,040 KB |
testcase_44 | AC | 53 ms
13,440 KB |
testcase_45 | WA | - |
testcase_46 | WA | - |
ソースコード
#include <bits/stdc++.h> #define show(x) cerr << #x << " = " << x << endl using namespace std; using ll = long long; using pii = pair<int, int>; using vi = vector<int>; template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) { os << "sz=" << v.size() << "\n["; for (const auto& p : v) { os << p << ","; } os << "]\n"; return os; } template <typename S, typename T> ostream& operator<<(ostream& os, const pair<S, T>& p) { os << "(" << p.first << "," << p.second << ")"; return os; } constexpr ll MOD = 1e9 + 7; template <typename T> constexpr T INF = numeric_limits<T>::max() / 100; class DisjointSets { public: DisjointSets(const int v) { m_parent.resize(v); m_rank.resize(v); m_size.resize(v); for (int i = 0; i < v; i++) { m_parent[i] = i; m_rank[i] = 0; m_size[i] = 1; } } bool same(const int a, const int b) { return find(a) == find(b); } int find(const int a) { if (m_parent[a] == a) { return a; } else { return m_parent[a] = find(m_parent[a]); } } void unite(const int a_, const int b_) { const int a = find(a_); const int b = find(b_); if (a == b) { return; } if (m_rank[a] > m_rank[b]) { m_parent[b] = a; m_size[a] += m_size[b]; } else { m_parent[a] = b; m_size[b] += m_size[a]; } if (m_rank[a] == m_rank[b]) { m_rank[b]++; } } int getSize(const int a) { return m_size[m_parent[a]]; } private: vector<int> m_parent; vector<int> m_rank; vector<int> m_size; }; struct Cycles { int acycle; int bcycle; int mode; bool operator<(const Cycles& c) const { return (acycle != c.acycle) ? acycle < c.acycle : (bcycle != c.bcycle) ? bcycle < c.bcycle : mode < c.mode; } }; ostream& operator<<(ostream& os, const Cycles& c) { os << "acycle = " << c.acycle << ", bcycle = " << c.bcycle << ", mode = " << c.mode << endl; return os; } int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; DisjointSets auf(N); vector<int> A(N); DisjointSets buf(N); vector<int> B(N); for (int i = 0; i < N; i++) { cin >> A[i]; A[i]--; auf.unite(i, A[i]); } for (int i = 0; i < N; i++) { cin >> B[i]; B[i]--; buf.unite(i, B[i]); } vector<int> aorder(N); vector<bool> aused(N, false); for (int i = 0; i < N; i++) { int pos = i; int cnt = 0; while (not aused[pos]) { aorder[pos] = cnt; aused[pos] = true; cnt++; pos = A[pos]; } } vector<int> border(N); vector<bool> bused(N, false); for (int i = 0; i < N; i++) { int pos = i; int cnt = 0; while (not bused[pos]) { border[pos] = cnt; bused[pos] = true; cnt++; pos = B[pos]; } } map<Cycles, int> num; for (int i = 0; i < N; i++) { const int asize = auf.getSize(i); const int bsize = buf.getSize(i); const int g = gcd(asize, bsize); num[Cycles{auf.find(i), buf.find(i), (((aorder[i] - border[i]) % g) + g % g)}]++; } ll sum = 0; for (const auto& p : num) { // show(p); const int acycle = p.first.acycle; const int bcycle = p.first.bcycle; const ll asize = auf.getSize(acycle); const ll bsize = buf.getSize(bcycle); const ll count = p.second; const ll size = lcm(asize, bsize) - count; sum += (size * (size + 1) / 2); sum %= MOD; } cout << sum << endl; return 0; }