結果
| 問題 |
No.2428 Returning Shuffle
|
| コンテスト | |
| ユーザー |
nono00
|
| 提出日時 | 2023-08-18 22:07:13 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 5,742 bytes |
| コンパイル時間 | 2,754 ms |
| コンパイル使用メモリ | 250,812 KB |
| 実行使用メモリ | 11,008 KB |
| 最終ジャッジ日時 | 2024-11-28 07:22:06 |
| 合計ジャッジ時間 | 5,137 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 9 WA * 14 |
ソースコード
#include <bits/stdc++.h>
// graph/graph-template.hpp
// graph/graph.hpp
#include <cassert>
#include <vector>
// graph/edge.hpp
namespace nono {
// brief:
// - Edge構造体
//
// tparam:
// - `T`: 重みの型
//
// note:
// - `from`, `to`, `weight` がないと、他のライブラリが壊れる
template <class T = int>
struct Edge {
using value_type = T;
int from;
int to;
T weight;
int index;
// brief:
// - Edgeコンストラクタ
Edge(int from, int to, const T& weight = 1, int index = -1)
: from(from),
to(to),
weight(weight),
index(index) {}
friend auto operator<=>(const Edge<T>& lhs, const Edge<T>& rhs) {
return lhs.weight <=> rhs.weight;
}
};
} // namespace nono
namespace nono {
// brief:
// - グラフ構造体
//
// tparam:
// - `T`: 辺の重みの型
// - `directed`: 有向かどうか
//
// note:
// - 有向グラフのaliasとして `DiGraph` が存在する
// - 以下の条件が満たさないと壊れる
// \ \ 1. `size()` が頂点数を返す
// \ \ 2. `operator[]` が `range-based for` に対応している
template <class T = int, bool directed = false>
class Graph {
using EdgeType = Edge<T>;
using EdgesType = std::vector<EdgeType>;
public:
using value_type = EdgesType;
Graph() = default;
explicit Graph(int vertex_size)
: vertex_size_(vertex_size),
edge_size_(0),
adj_list_(vertex_size) {}
Graph(int vertex_size, const std::vector<EdgeType>& edges)
: vertex_size_(vertex_size),
edge_size_(0),
adj_list_(vertex_size) {
for (const auto& e: edges) {
adj_list_[e.from].emplace_back(e);
}
}
// brief:
// - 頂点数を取得する
int size() const {
return vertex_size_;
}
// brief:
// - 頂点 `i` に隣接する頂点を取得する
const std::vector<EdgeType>& operator[](int i) const {
return adj_list_[i];
}
// brief:
// - 頂点 `i` に隣接する頂点を取得する
std::vector<EdgeType>& operator[](int i) {
return adj_list_[i];
}
// brief:
// - グラフに辺を追加する
//
// note:
// - weightのデフォルト値は1
void add_edge(int from, int to, T weight = static_cast<T>(1), int index = -1) {
adj_list_[from].emplace_back(from, to, weight, index);
if (not directed) adj_list_[to].emplace_back(to, from, weight, index);
edge_size_++;
}
// brief:
// - 頂点 `i` の次数を取得する
//
// note:
// - 有向グラフの場合、出次数を取得する
int degree(int i) const {
return adj_list_[i].size();
}
// brief:
// - 頂点の次数の配列を取得する
//
// note:
// - 有向グラフの場合、出次数を取得する
std::vector<int> degree() const {
std::vector<int> result(vertex_size_);
for (int i = 0; i < vertex_size_; i++) {
result[i] = adj_list_[i].size();
}
return result;
}
// brief:
// - 辺の配列に取得する
//
// note:
// - 単純グラフでないと壊れる
std::vector<EdgeType> to_edges() const {
std::vector<EdgeType> edges(edge_size_);
int count = 0;
for (int u = 0; u < vertex_size_; u++) {
for (const auto& e: adj_list_[u]) {
if (directed || e.from <= e.to) {
edges[count] = e;
count++;
}
}
}
return edges;
}
private:
int vertex_size_;
int edge_size_;
std::vector<std::vector<EdgeType>> adj_list_;
};
// brief:
// - 有向グラフ構造体
//
// tparam:
// - `T`: 辺の重みの型
//
// note:
// - `Graph` のalias
template <class T = int>
using DiGraph = Graph<T, true>;
} // namespace nono
namespace nono {
constexpr long long mod = 998244353;
long long pow(long long base, long long exp, long long mod) {
long long result = 1;
while (exp > 0) {
if (exp & 1) {
result *= base;
result %= mod;
}
base *= base;
base %= mod;
exp >>= 1;
}
return result;
}
void solve() {
int n, m;
std::cin >> n >> m;
std::vector<int> a(n);
std::iota(a.begin(), a.end(), 0);
for (int i = 0; i < m; i++) {
int t;
std::cin >> t;
std::vector<int> s(t);
for (int j = 0; j < t; j++) {
std::cin >> s[j];
s[j]--;
}
int temp = a[s.back()];
for (int j = t - 1; j > 0; j--) {
a[s[j]] = a[s[j - 1]];
}
a[s.front()] = temp;
}
// for (int i = 0; i < n; i++) {
// std::cout << a[i] << ' ';
// }
// std::cout << std::endl;
std::vector<bool> used(n);
std::vector<int> cycle;
for (int i = 0; i < n; i++) {
if (not used[i]) {
int s = i;
int len = 0;
while (not used[s]) {
used[s] = true;
s = a[s];
len++;
}
cycle.push_back(len);
}
}
long long ans = 1;
long long gcd = 0;
for (auto v: cycle) {
ans *= v;
ans %= mod;
gcd = std::gcd(gcd, v);
}
// std::cout << ans << ' ' << gcd << std::endl;
std::cout << (ans * pow(gcd, mod - 2, mod) % mod) << std::endl;
}
} // namespace nono
int main() {
std::cin.tie(0)->sync_with_stdio(0);
std::cout << std::fixed << std::setprecision(15);
int t = 1;
// std::cin >> t;
while (t--) nono::solve();
}
nono00