結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー purple_jwl
提出日時 2015-03-23 23:27:06
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 875 bytes
コンパイル時間 3,502 ms
コンパイル使用メモリ 159,376 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-29 00:25:28
合計ジャッジ時間 2,393 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define REP(i, x, n) for(int i = x; i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define F first
#define S second
#define mp make_pair

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

int rec(int p, int q, const vector<int> &amida) {
  if(p == q) return 1;
  return rec(amida[p], q, amida) + 1;
}

int main() {
  // ios_base::sync_with_stdio(false);
  int N, K;
  cin >> N >> K;
  vector<int> amida(N);
  rep(i, N) amida[i] = i;
  rep(i, K) {
    int x, y;
    cin >> x >> y;
    swap(amida[x - 1], amida[y - 1]);
  }

  vector<int> cnt(N);
  rep(i, N) {
    cnt[i] = rec(amida[i], i, amida);
  }

  ll ans = 1;
  rep(i, N) ans = ans * cnt[i] / __gcd(ans, (ll)cnt[i]);

  cout << ans << endl;
  
  return 0;
}
0