結果

問題 No.416 旅行会社
ユーザー S97323424S97323424
提出日時 2020-10-11 11:02:10
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 1,024 ms / 4,000 ms
コード長 1,971 bytes
コンパイル時間 204 ms
コンパイル使用メモリ 5,372 KB
実行使用メモリ 70,780 KB
最終ジャッジ日時 2023-08-21 10:38:03
合計ジャッジ時間 11,097 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 451 ms
42,960 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 5 ms
4,380 KB
testcase_08 AC 10 ms
4,380 KB
testcase_09 AC 42 ms
7,200 KB
testcase_10 AC 514 ms
43,716 KB
testcase_11 AC 477 ms
44,568 KB
testcase_12 AC 507 ms
43,404 KB
testcase_13 AC 452 ms
42,976 KB
testcase_14 AC 1,024 ms
70,192 KB
testcase_15 AC 1,015 ms
70,276 KB
testcase_16 AC 1,002 ms
70,780 KB
testcase_17 AC 1,022 ms
70,448 KB
testcase_18 AC 1,013 ms
70,268 KB
testcase_19 AC 658 ms
52,696 KB
testcase_20 AC 646 ms
52,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local UnionFind={}

UnionFind.create=function(self,n)
    self.par={}
    self.group={}
    for i=1,n do
        self.par[i]=i
        self.group[i]={i}
    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)
    local rx=self:getroot(x)
    local ry=self:getroot(y)
    if rx==ry then
        return
    end
    if #self.group[rx]<#self.group[ry] then
        rx,ry=ry,rx
    end
    for _,v in pairs(self.group[ry]) do
        self.par[v]=rx
        table.insert(self.group[rx],v)
    end
    self.group[ry]={}
end

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

----------

local n,m,q=io.read("*n","*n","*n","*l")
local map={}
for i=1,m do
    local edge=io.read()
    local a,b=edge:match("(%d+) (%d+)")
    map[edge]={tonumber(a),tonumber(b)}
end
local event={}
local bridge={}
for i=1,q do
    local edge=io.read()
    local c,d=edge:match("(%d+) (%d+)")
    event[edge]=true
    bridge[i]={tonumber(c),tonumber(d)}
end

local uf=UnionFind:new(n)
for edge,vertex in pairs(map) do
    if not event[edge] then
        local a,b=vertex[1],vertex[2]
        uf:unite(a,b)
    end
end

local result={}
local root=uf.par[1]
for _,vertex in pairs(uf.group[root]) do
    result[vertex]=-1
end
for i=q,1,-1 do
    local c,d=bridge[i][1],bridge[i][2]
    if uf:same(c,d) then
        goto continue
    end
    if uf:same(1,c) or uf:same(1,d) then
        if uf:same(1,d) then
            c,d=d,c
        end
        local root=uf.par[d]
        for _,vertex in pairs(uf.group[root]) do
            result[vertex]=i
        end
    end
    uf:unite(c,d)
    ::continue::
end
for i=2,n do
    print(result[i] or 0)
end
0