結果
問題 | No.101 ぐるぐる!あみだくじ! |
ユーザー |
![]() |
提出日時 | 2016-04-01 00:29:04 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2 ms / 5,000 ms |
コード長 | 914 bytes |
コンパイル時間 | 591 ms |
コンパイル使用メモリ | 60,144 KB |
実行使用メモリ | 6,824 KB |
最終ジャッジ日時 | 2024-10-02 08:43:43 |
合計ジャッジ時間 | 1,696 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 37 |
ソースコード
#include <iostream> #include <vector> #include <algorithm> using namespace std; #define RREP(i,s,e) for (i = s; i >= e; i--) #define rrep(i,n) RREP(i,n,0) #define REP(i,s,e) for (i = s; i < e; i++) #define rep(i,n) REP(i,0,n) #define INF 100000000 typedef long long ll; int c[101]; int s[101]; int dfs(int x, int d) { if (c[x] > 0) return c[x]; else if (c[x] == -1) { c[x] = d; return d; } else { c[x] = -1; return c[x] = dfs(s[x],d+1); } } int gcd(int a, int b) { return b == 0 ? a : gcd(b,a%b); } ll lcm(ll a, ll b) { return a * b / gcd(a,b); } int main() { int i, n, k, ans; cin >> n >> k; REP (i,1,n+1) s[i] = i; rep (i,k) { int x, y; cin >> x >> y; swap(s[x],s[y]); } REP (i,1,n+1) dfs(i,0); ans = 1; REP (i,1,n+1) ans = lcm(ans,c[i]); cout << ans << endl; return 0; }