結果

問題 No.826 連絡網
ユーザー S97323424S97323424
提出日時 2020-10-11 12:25:15
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 887 ms / 2,000 ms
コード長 1,283 bytes
コンパイル時間 163 ms
コンパイル使用メモリ 5,332 KB
実行使用メモリ 36,048 KB
最終ジャッジ日時 2023-09-27 22:55:12
合計ジャッジ時間 9,822 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 5 ms
4,380 KB
testcase_04 AC 6 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 6 ms
4,376 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 7 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 5 ms
4,380 KB
testcase_12 AC 617 ms
35,208 KB
testcase_13 AC 220 ms
18,660 KB
testcase_14 AC 440 ms
36,048 KB
testcase_15 AC 42 ms
4,672 KB
testcase_16 AC 282 ms
18,784 KB
testcase_17 AC 227 ms
18,956 KB
testcase_18 AC 172 ms
10,672 KB
testcase_19 AC 720 ms
35,564 KB
testcase_20 AC 705 ms
35,512 KB
testcase_21 AC 8 ms
4,376 KB
testcase_22 AC 228 ms
20,188 KB
testcase_23 AC 296 ms
18,956 KB
testcase_24 AC 120 ms
10,692 KB
testcase_25 AC 887 ms
35,392 KB
testcase_26 AC 143 ms
10,676 KB
testcase_27 AC 625 ms
35,308 KB
testcase_28 AC 481 ms
35,084 KB
testcase_29 AC 222 ms
18,668 KB
testcase_30 AC 879 ms
35,804 KB
testcase_31 AC 258 ms
18,732 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local UnionFind={}

UnionFind.create=function(self,n)
    self.par={}
    self.rank={}
    self.size={}
    for i=1,n do
        self.par[i]=i
        self.rank[i]=1
        self.size[i]=1
    end
end

UnionFind.getroot=function(self,x)
    local nx=x
    while self.par[x]~=x do
        x=self.par[x]
    end
    while self.par[nx]~=x do
        self.par[nx],nx=x,self.par[nx]
    end
    return x
end

UnionFind.same=function(self,x,y)
    return self:getroot(x)==self:getroot(y)
end

UnionFind.unite=function(self,x,y)
    x=self:getroot(x)
    y=self:getroot(y)
    if x==y then
        return
    end
    if self.rank[x]>self.rank[y] then
        x,y=y,x
    elseif self.rank[x]==self.rank[y] then
        self.rank[x]=self.rank[x]+1
    end
    self.par[x]=y
    self.size[y]=self.size[y]+self.size[x]
end

UnionFind.getsize=function(self,x)
    return self.size[self:getroot(x)]
end

UnionFind.new=function(self,n)
    local obj={}
    setmetatable(obj,{__index=UnionFind})
    obj:create(n)
    return obj
end

----------

local n,p=io.read("*n","*n")
local uf=UnionFind:new(n)
local t={}
for i=1,n do
    t[i]=0
end
for i=2,n do
    if 1>t[i] then
        for j=2,math.floor(n/i) do
            t[i]=1
            uf:unite(i,i*j)
        end
    end
end
print(uf:getsize(p))
0