結果

問題 No.826 連絡網
ユーザー aa
提出日時 2019-05-04 01:04:20
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 652 ms / 2,000 ms
コード長 989 bytes
コンパイル時間 1,874 ms
コンパイル使用メモリ 168,368 KB
実行使用メモリ 11,088 KB
最終ジャッジ日時 2023-08-15 18:26:58
合計ジャッジ時間 7,917 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 4 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 418 ms
8,684 KB
testcase_13 AC 123 ms
5,088 KB
testcase_14 AC 282 ms
7,272 KB
testcase_15 AC 18 ms
4,376 KB
testcase_16 AC 166 ms
5,948 KB
testcase_17 AC 123 ms
5,056 KB
testcase_18 AC 89 ms
4,932 KB
testcase_19 AC 510 ms
9,480 KB
testcase_20 AC 499 ms
9,576 KB
testcase_21 AC 3 ms
4,376 KB
testcase_22 AC 125 ms
5,380 KB
testcase_23 AC 175 ms
5,852 KB
testcase_24 AC 59 ms
4,468 KB
testcase_25 AC 644 ms
10,768 KB
testcase_26 AC 75 ms
4,564 KB
testcase_27 AC 438 ms
8,764 KB
testcase_28 AC 308 ms
7,632 KB
testcase_29 AC 121 ms
5,196 KB
testcase_30 AC 652 ms
11,088 KB
testcase_31 AC 156 ms
5,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"

using namespace std;

struct unionfind
{
	//rankのオーバーフローに注意
	vector<int> par, rank;
	unionfind(int n)
	{
		par.resize(n);
		rank.resize(n);
		for (int i = 0; i < n; i++)
		{
			par[i] = i;
			rank[i] = 1;
		}
	}

	bool same(int u, int v)
	{
		return root(u) == root(v);
	}

	void unite(int u, int v)
	{
		u = root(u), v = root(v);
		if (u == v)
			return;
		if (rank[u] > rank[v])
			swap(u, v);
		par[u] = v;
		rank[v] += rank[u];
	}

	int root(int v)
	{
		if (par[v] == v)
			return v;
		return par[v] = root(par[v]);
	}
};

void solve(void)
{
	int n, p;
	cin >> n >> p;
	unionfind uf(n);
	for (int i = 2; i <= n; i++)
	{
		int x = i;
		for (int f = 2; f * f <= x; f++)
		{
			if (x % f == 0)
			{
				uf.unite(f-1, i-1);
			}
			while(x % f == 0)
			{
				x /= f;
			}
		}
		if (x > 1)
		{
			uf.unite(x-1, i-1);
		}
	}
	cout << uf.rank[uf.root(p - 1)] << endl;
}

int main()
{
	solve();
	//cout << "yui(*-v・)yui" << endl;
	return 0;
}
0