結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー kotatsugame
提出日時 2020-02-29 06:33:54
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 447 bytes
コンパイル時間 494 ms
コンパイル使用メモリ 64,000 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-10-13 19:48:31
合計ジャッジ時間 1,738 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:7:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
    7 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
using namespace std;
int N,K;
int T[100];
long gcd(long a,long b){return b?gcd(b,a%b):a;}
bool vis[100];
main()
{
	cin>>N>>K;
	for(int i=0;i<N;i++)T[i]=i;
	for(int i=0;i<K;i++)
	{
		int x,y;cin>>x>>y;
		T[x-1]^=T[y-1]^=T[x-1]^=T[y-1];
	}
	long ans=1;
	for(int i=0;i<N;i++)
	{
		if(vis[i])continue;
		int t=i,cnt=0;
		while(!vis[t])
		{
			vis[t]=true;
			t=T[t];
			cnt++;
		}
		ans=ans/gcd(ans,cnt)*cnt;
	}
	cout<<ans<<endl;
}
0