Submission #34030393
Source Code Expand
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
import Control.Monad
import qualified Data.ByteString.Char8 as BS
import Data.Char
import Data.List
import Data.Array
import Control.Applicative
main = do
[n,m,e] <- bsGetLnInts
uvs <- replicateM e ((\(u:v:_) -> (u,v)) <$> bsGetLnInts)
[q] <- bsGetLnInts
xs <- replicateM q (head <$> bsGetLnInts)
let ans = abc264e n m e uvs q xs
mapM_ print ans
bsGetLnInts :: IO [Int]
bsGetLnInts = BS.getLine >>= return . unfoldr (BS.readInt . BS.dropWhile isSpace)
abc264e :: Int -> Int -> Int -> [(Int,Int)] -> Int -> [Int] -> [Int]
abc264e n m e uvs q xs = scanr (+) ansQ ccds
where
-- 先頭 N 個は都市、後半 M 個が発電所
uf0 = newUF ufF (n+m) (replicate n (1,False) ++ replicate m (0, True))
-- 電線の配列
uvA = listArray (1,e) uvs
-- 電線が切れないかチェック表
uvX = accumArray (&&) True (1,e) [(x,False) | x <- xs]
-- 切れない電線を配線する
uf1 = foldl' {-'-} uniteUF uf0 [uv | (uv,True) <- zip uvs (elems uvX)]
-- 最後まで通電している都市の数
ansQ = sum [cc | (_,(cc, True)) <- getCont uf1]
-- 逆順に Xi を繋いだufを作りつつ、都市数の増加を追跡
(_,ccds) = mapAccumR step uf1 (tail xs)
step uf xi
| a == b = (uf, 0)
| pp1 == pp2 = (uf1, 0)
| pp1 = (uf1, cc2)
| otherwise = (uf1, cc1) -- pp2 = True
where
uvx@(ux, vx) = uvA ! xi
(a, _, (cc1,pp1)) = getRoot uf ux
(b, _, (cc2,pp2)) = getRoot uf vx
uf1 = uniteUF uf uvx
ufF (cc1,pp1) (cc2,pp2) = (cc1 + cc2, pp1 || pp2)
-- UnionFind
type UnionFind a = (Array Int (Either Int (Int, a)), a -> a -> a)
-- 配列要素は 親id または ランク(正の数),ペイロード のペア
-- 1からNまでのN要素が独立した初期状態を作る
-- ペイロードの統合関数も保持する
newUF :: (a -> a -> a) -> Int -> [a] -> UnionFind a
newUF f n as = (listArray (1, n) [Right (1, a) | a <- as], f)
-- 補助関数 ノードの根まで辿り、代表id, rank, ペイロードを返す
getRoot :: UnionFind a -> Int -> (Int, Int, a)
getRoot uf@(ar, _) i =
case ar ! i of
Left k -> getRoot uf k
Right (r, v) -> (i, r, v)
idof (i, _, _) = i
-- ふたつのノードが同じ分割に属しているか判定する
findUF :: UnionFind a -> Int -> Int -> Bool
findUF uf i j = idof (getRoot uf i) == idof (getRoot uf j)
-- ふたつのノードが同じ分割に属していることを登録する
uniteUF :: UnionFind a -> (Int, Int) -> UnionFind a
uniteUF uf@(ar, f) (i, j)
| a == b = uf
| otherwise =
case compare r s of
GT -> (ar // [(b, Left a), (a, Right (r, f p q))], f)
LT -> (ar // [(a, Left b), (b, Right (s, f p q))], f)
EQ -> (ar // [(a, Left b), (b, Right (succ s, f p q))], f)
where
(a, r, p) = getRoot uf i
(b, s, q) = getRoot uf j
-- 全てのペイロードを、代表idとペアにして返す
getCont :: UnionFind a -> [(Int, a)]
getCont (ar,_) = [(i,p) | (i, Right (_, p)) <- assocs ar]
Submission Info
Submission Time |
|
Task |
E - Blackout 2 |
User |
joetheshootingst |
Language |
Haskell (GHC 8.8.3) |
Score |
0 |
Code Size |
3099 Byte |
Status |
TLE |
Exec Time |
3315 ms |
Memory |
303716 KB |
Compile Error
Loaded package environment from /home/contestant/.ghc/x86_64-linux-8.8.3/environments/default
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
0 / 500 |
Status |
|
|
Set Name |
Test Cases |
Sample |
sample_01.txt |
All |
sample_01.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt, test_22.txt, test_23.txt, test_24.txt, test_25.txt, test_26.txt, test_27.txt, test_28.txt, test_29.txt, test_30.txt, test_31.txt, test_32.txt, test_33.txt, test_34.txt, test_35.txt, test_36.txt, test_37.txt, test_38.txt, test_39.txt, test_40.txt |
Case Name |
Status |
Exec Time |
Memory |
sample_01.txt |
AC |
8 ms |
3816 KB |
test_01.txt |
TLE |
3311 ms |
127252 KB |
test_02.txt |
TLE |
3310 ms |
83640 KB |
test_03.txt |
AC |
1089 ms |
303716 KB |
test_04.txt |
TLE |
3310 ms |
77656 KB |
test_05.txt |
TLE |
3308 ms |
21708 KB |
test_06.txt |
TLE |
3309 ms |
38416 KB |
test_07.txt |
TLE |
3310 ms |
69132 KB |
test_08.txt |
AC |
853 ms |
232776 KB |
test_09.txt |
TLE |
3311 ms |
117904 KB |
test_10.txt |
TLE |
3311 ms |
119324 KB |
test_11.txt |
TLE |
3313 ms |
219060 KB |
test_12.txt |
TLE |
3309 ms |
48652 KB |
test_13.txt |
AC |
743 ms |
222716 KB |
test_14.txt |
TLE |
3311 ms |
116764 KB |
test_15.txt |
TLE |
3309 ms |
24812 KB |
test_16.txt |
TLE |
3309 ms |
39116 KB |
test_17.txt |
TLE |
3310 ms |
75740 KB |
test_18.txt |
AC |
1127 ms |
300396 KB |
test_19.txt |
TLE |
3311 ms |
123220 KB |
test_20.txt |
TLE |
3310 ms |
73000 KB |
test_21.txt |
TLE |
3315 ms |
265100 KB |
test_22.txt |
TLE |
3312 ms |
148792 KB |
test_23.txt |
AC |
1159 ms |
299352 KB |
test_24.txt |
TLE |
3312 ms |
148760 KB |
test_25.txt |
TLE |
3313 ms |
212272 KB |
test_26.txt |
TLE |
3311 ms |
141264 KB |
test_27.txt |
TLE |
3312 ms |
148808 KB |
test_28.txt |
AC |
1154 ms |
299096 KB |
test_29.txt |
TLE |
3312 ms |
148640 KB |
test_30.txt |
TLE |
3311 ms |
138440 KB |
test_31.txt |
TLE |
3314 ms |
248672 KB |
test_32.txt |
TLE |
3312 ms |
148864 KB |
test_33.txt |
AC |
1152 ms |
298944 KB |
test_34.txt |
TLE |
3312 ms |
148808 KB |
test_35.txt |
TLE |
3315 ms |
276760 KB |
test_36.txt |
TLE |
3315 ms |
271424 KB |
test_37.txt |
TLE |
3312 ms |
148696 KB |
test_38.txt |
AC |
1159 ms |
298952 KB |
test_39.txt |
TLE |
3312 ms |
148636 KB |
test_40.txt |
TLE |
3315 ms |
263444 KB |