結果

問題 No.416 旅行会社
ユーザー S97323424S97323424
提出日時 2020-10-11 10:53:28
言語 Lua
(LuaJit 2.1.1696795921)
結果
WA  
実行時間 -
コード長 2,090 bytes
コンパイル時間 292 ms
コンパイル使用メモリ 5,332 KB
実行使用メモリ 47,336 KB
最終ジャッジ日時 2023-09-27 22:49:32
合計ジャッジ時間 9,126 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 362 ms
47,336 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 387 ms
43,704 KB
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
権限があれば一括ダウンロードができます

ソースコード

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 not uf:same(c,d) then
        if uf:same(1,c) or not uf:same(1,d) then
            local root=uf.par[d]
            for _,vertex in pairs(uf.group[root]) do
                result[vertex]=i
            end
        elseif not uf:same(1,c) or uf:same(1,d) then
            local root=uf.par[c]
            for _,vertex in pairs(uf.group[root]) do
                result[vertex]=i
            end
        end
        uf:unite(c,d)
    end
end
for i=2,n do
    print(result[i])
end
0