女性为什么会肾结石| 紫色睡莲的花语是什么| 100元人民币什么时候发行的| 新生儿湿疹抹什么药膏| 36岁生日有什么讲究| 日值四离是什么意思| 胃病可以吃什么水果| 晚生是什么意思| 1月26日是什么星座| 国家发改委主任什么级别| 一个木一个舌读什么| 坐是什么结构| 韩信属什么生肖| 子官肌瘤吃什么食物| normal什么意思| 脸上长闭口是什么原因导致的| 什么油好| 什么而不| 俄狄浦斯情结是什么意思| 反胃想吐是什么原因| 增生性贫血是什么意思| 便秘了吃什么容易排便| 嗓子疼喝什么饮料| 豆汁是什么做的| seeyou是什么意思| 双鱼座的幸运色是什么颜色| 睡觉翻白眼是什么原因| 牙齿发酸是什么原因| 过度纵欲的后果是什么| 逸五行属什么| 什么是权力| 六月十二日是什么日子| 醉清风是什么意思| 广州有什么特产必带| 放屁臭是什么原因| 增强记忆力吃什么| 梦见白发是什么意思| 深水炸弹什么意思| 勃起不够硬吃什么药| 人为什么会感冒| 中科院是干什么的| 陆地上最大的动物是什么| 韭黄和韭菜有什么区别| 仓鼠吃什么| 和合是什么意思| 梦见打官司预示着什么| 什么药治便秘效果最好最快| 白细胞一个加号什么意思| 米线里的麻油是什么油| 什么东西蛋白质含量高| 黑松露是什么东西| 什么啊| 7月7号是什么节日| 糖尿病人吃什么水果好| 生理曲度变直什么意思| 什么叫活佛| 开业送什么礼物好| 什么叫双相障碍| kohler是什么品牌| 蒜气是什么病| 什么是法西斯主义| 六月初六是什么星座| 包皮是什么样子图片| 狗头什么意思| 男士脸黑用什么能美白| 貔貅是什么生肖| 肚子咕咕叫放屁多是什么原因| 长痔疮有什么症状| 胃溃疡有什么症状| 40岁适合什么工作| 腋毛上有白色附着物是什么| 肺火旺吃什么药| 抗战纪念日为什么是9月3日| 防腐剂是什么| 尾盘拉升意味着什么| 什么叫游走性关节疼痛| 胃胀吃什么药最有效| 247什么意思| 观落阴是什么意思| 睡觉为什么会打呼噜| 肺癌晚期有什么症状| 为什么长智齿| 肛裂是什么原因造成的| 月经期间喝什么好排毒排污血| 易经和周易有什么区别| 早谢是什么症状| 肠化生是什么症状| 为什么不建议切除脂肪瘤| 耳鸣吃什么药比较好| 酒后头疼吃什么| 荼靡是什么意思| 梦见雨伞是什么意思| 老子叫什么| 全脂奶粉是什么意思| 口臭药店买什么药吃| bjd是什么| ck什么意思| 什么叫腺肌瘤| 充电宝100wh是什么意思| 特发性震颤吃什么药| 什么品牌的洗衣机最好| siri是什么| 臭粉是什么东西| 腋下疼痛是什么原因| 水满则溢月盈则亏是什么意思| 为什么会拉肚子| 嘴角上扬是什么意思| 过期的洗面奶可以用来做什么| 黑洞是什么| 什么是点映| 品保是做什么的| 李子和什么不能一起吃| 今天股票为什么大跌| 4a广告公司什么意思| 11月22是什么星座| 多梦吃什么药| 6月份生日是什么星座| 心脏大是什么原因| 不孕不育有什么症状女| eps是什么意思| 雷暴是什么意思| 胃酸吃什么食物好得快| 瘪嘴是什么意思| 2017 年是什么年| wmf是什么牌子| 小二阳是什么意思| teal是什么颜色| 余事勿取是什么意思| 女性检查甲功是什么病| 女人上嘴唇有痣代表什么| 桃胶有什么功效与作用| 现在最好的避孕方法是什么| 八月底什么星座| 德国是什么民族| 一九七八年属什么生肖| 尿发黄是什么原因男性| 家蛇出现寓意什么| 男士蛋皮痒用什么药| 保育是什么意思| 早上口干口苦是什么原因| 腰椎间盘突出压迫神经吃什么药| 肿标五项查的是什么| 孤僻是什么意思| 累了喝什么缓解疲劳| 文联主席是什么级别| 尿痛什么原因| 扬字五行属什么| 西游记是一部什么小说| 关节间隙变窄什么意思| 棉是什么面料| g18k是什么金| 空调长时间不用再开注意什么| 有什么聚会玩的游戏| 拉稀肚子疼吃什么药| rds是什么意思| 嗓子疼咳嗽是什么原因| 白热化阶段是什么意思| 桃是什么生肖| 国防部是干什么的| 猴子吃什么食物| 清洁度lv是什么意思| 黄铜刮痧板有什么好处| 心率快吃什么药| 把你的心我的心串一串是什么歌| 水里有什么| 7月10日是什么星座| 坤造是什么意思| 丝瓜为什么会变黑| 化妆品有什么| 幼犬可以吃什么| 男性肾虚有什么症状| 大豆和黄豆有什么区别| 空腹喝啤酒有什么危害| 什么是汉服| 学士学位证书有什么用| 米酒发酸是什么原因| 生肖羊和什么生肖相冲| 古代上班叫什么| 汾酒是什么香型| 天的反义词是什么| 省委副书记什么级别| 查肝功能能查出什么病| 3月7日是什么星座| 头皮毛囊炎用什么洗发水| 你为什么背着我爱别人| 间接胆红素高是什么意思| 下焦湿热阴囊潮湿吃什么药| 婴儿吐泡泡是什么原因| 什么雪糕最好吃| 属猪的护身佛是什么佛| 草字头有什么字| 阴道感染有什么症状| 禁欲有什么好处| 无声无息是什么意思| 情窦初开是什么意思| 心烦意乱焦躁不安吃什么药| 人妖是什么| 前哨是什么意思| 试管婴儿是什么意思| 肛门周围痒是什么病| 手发痒是什么原因| 为什么不嫁丧妻之男| 吃什么降血糖快| 宣是什么意思| 什么是月经| 6月26什么星座| 虾肚子上的黑线是什么| ptt是什么| 不均质脂肪肝是什么意思| 奶头疼是什么原因| 助听器什么价位| 家里有壁虎是什么征兆| 食道好像有东西堵着是什么原因| 沉珂是什么意思| 忽必烈姓什么| 什么发型好看| 桃花是指什么生肖| 伦字五行属什么| 孕早期吃什么水果| 尾牙是什么意思| 紫色是什么颜色调出来的| 肝胆湿热喝什么茶| 崩大碗配什么煲汤最好| 豁达是什么意思| 打喷嚏流清鼻涕属于什么感冒| y什么意思| 戒指丢了暗示着什么| 信徒什么意思| 尿失禁是什么原因| 什么叫穿刺| 劫财代表什么| 尿微量白蛋白高吃什么药| 肾阳虚的表现是什么| 劳改犯是什么意思| 肝内小钙化灶是什么意思| 牙龈肿痛吃什么中成药| chick是什么意思| 伤口愈合慢是什么原因| 斯德哥尔摩是什么意思| 胃不好喝什么茶好| 地黄是什么| 什么是什么的家| 右肺上叶肺大泡是什么意思| 石乐读什么| 蚜虫长什么样| 身上长小肉揪是什么原因| 伤官伤尽是什么意思| 一直放屁什么原因| 孕妇可以吃什么鱼| 南京有什么好玩的| 普贤菩萨保佑什么生肖| 查肝挂什么科| 腿抽筋是什么问题| 打水光针有什么副作用| 道歉送什么花| 慢性肠胃炎吃什么药| 什么样的青蛙| 槟子是什么水果| 糖醋里脊是什么肉做的| 种植什么最赚钱农村| 为什么会内分泌失调| ccb是什么| 凝神是什么意思| 5月17日是什么星座| 百度
Показаны сообщения с ярлыком cses. Показать все сообщения
Показаны сообщения с ярлыком cses. Показать все сообщения

четверг, 4 января 2024 г.

Distinct Colors

I`ve solved yet another very funny CSES task - it looks very similar to another task called "Reachable Nodes" (my solution for it). The only difference is that we asked to count not unique nodes but colors of nodes. What can go wrong?

And this is where funny part begins - my patched solution got crashes. gdb didn`t showed nothing interesting. However I remember scary cryptic command to show stack usage:

print (char *)_environ - (char *)$sp
$1 = 8384904

Very close to default 8Mb (check ulimit -s). Wait, WHAT? Do we really have stack exhausting? Lets check - 8 * 1024 * 1024 = 8388608 bytes. Tree can have 200000 nodes. 8388608 / 200000 = ~42 bytes for each recursive DFS call. Seems to be true - in each call we store return address + stack frame RBP + 3 registers holding args (this, indexes of node and parent) - so at least 5 * 8 = 40 bytes. It`s so happened that some tests contain tree with very long stem from root till end, so yes - recursive DFS cannot visit all nodes in such tree. Solution is simple - we can emulate recursion with std::stack. As bonus for all nodes in stack we can use single bit mask to save space

Another unpleasant observation is that trees in tests ain't BINARY trees. When one picture is worth a thousand words:

Degree of node 2 is 4. This is main reason why function dfs has separate branch for processing joint nodes with only 2 descendants - bcs initially method is_fork returned only left and right

Source

воскресенье, 17 декабря 2023 г.

Filling Trominos

IMHO this is very hard task - only 104 accepted solutions. My solution is here

Google gives lots of links for trominos but they all for totally different task from Euler Project - in our case we have only L-shapes. So lets think about possible algorithm

It`s pretty obvious that we can make 2 x 3 or 3 x 2 rectangles with couple of L-trominos. So naive solution is just to check if one size is divisible by 2 and other by 3

However with pen and paper you can quickly realize that you can for example fill rectangle 5 x 6:

aabaab
abbabb
ccddee
dcdced
ddccdd

Algo can look like (see function check2x3)

  • if one side of rectangle is divisible by 6 then another minus 2 should be divisible by 3
  • if one side of rectangle is divisible by 6 then another minus 3 should be divisible by 2

Submit our solution and from failed tests suddenly discovering that you also can have rectangle 9 x 5. Some details how this happens

So we can have maximal 3 groups of different shapes:

  • 9 x 5 rectangle (or even several if sides multiples of 5 & 9) - in my solution it stored in field has_95
  • 1 or 2 groups of 2 x 3 rectangles below 9 x 5 shape. 1 for case when you can fill this area with shapes 2 x 3 of the same orientation and 2 if you must mix vertical and horizontal rectangles - field trom
  • the same 1 or 2 groups on right of 9 x 5 shape - field right

Now the only remained problem is coloring

Rectangle 9 x 5 has 5 different colors but it is possible to arrange trominos in such way that on borders it will have only 4 colors and 5th is inside. For groups of 2 x 3 rectangles you need 4 colors if group size is 1 and yet 4 if size is 2. In worst case number of colors is 4 for 9 x 5 + 2 * 2 * 4 = 20 - so we can fit in A-Z

воскресенье, 12 ноября 2023 г.

my solutions for couple CSES tasks

CSES has two very similar by description tasks but with completely different solutions: "Critical Cities" (218 accepted solutions at time when I writing this) and "Visiting Cities" (381 accepted solutions)

Critical Cities

We are given an directed unweighted graph and seems that we need to find it`s dominators for example using Lengauer-Tarjan algo (with complexity O((V+E)log(V+E))
Then we could check each vertex in this dominators tree to see if it leads to target node, so overall complexity is O(V * (V+E)log(V+E))
 
This looks not very impressive IMHO. Lets try something completely different (c) Monty Python's Flying Circus. For example we could run wave (also known as Lee algorithm) from source to target and get some path with complexity O(V+E). Note that in worst case this path can contain all vertices. Lets mark all vertices in this path
Next we could continue to run waves but at this time ignoring edges from marked nodes and see what marked vertices are still reachable. For example on some step k we run wave from Vs and reached vertices Vi and Vj. We can conclude that all vertices in early found path between Vs and Vj are NOT critical cities. So we can repeat next step starting with Vj
This process can be repeated in worst case V times so overall complexity is O(V*(V+E))
 
My solution is here

 

Visiting Cities

At this time we are given an directed weighted graph and seems that simplest solution is to find all K-th shortest paths (for example with Yen algo) and make union of their vertices. Because I'm very lazy I decided to reuse some ready and presumably well-tested implementation of this algo. You can read about fabulous results here
 

After that I plunged into long thoughts until I decided to count how many paths of minimal length go through each vertex - actually we could run Dijkstra in both directions: from source to target and from target to source, counting number of paths with minimal length. And then we could select from this path vertices where product of direct counts with reverse equal to direct count on target (or reverse count on source) - it`s pretty obvious that you can`t avoid such vertices in any shortest path. Complexity of this solution is two times from Dijkstra algo (depending from implementation O(V^2) or O(V * log(V) + E * log(V)) using some kind of heap) + in worst case V checks for each vertices in first found shortest path

My solution is here

четверг, 9 ноября 2023 г.

kssp library, part 2

Previous part 

I was struck by the idea of how to reduce size of graph before enumerating all K-th shortest paths. We can use cut-points. By definition if we have cut-point in some shortest path it must be visited always - otherwise you can`t reach the destination. So algo is simple
  1. find with Dijkstra algo first shortest paths for whole graph
  2. find all cut-points in whole graph
  3. iterate over found shortest path - if current vertex is cut-point - we can run brute-force from previous cut-point till current

Results are crazy anyway - on the same test 7

  • Yen: 13.86s, 29787, 3501 cycles
  • Node Classification: 118.4s, 30013, 3501 cycles
  • Postponed Node Classification: 104.95s, 30013, 3501 cycles
  • PNC*: 120.33s, 30013, 3501 cycles
  • Parsimonious Sidetrack Based: 4.66s, 29980, 3501 cycles
  • Parsimonious Sidetrack Based v2: 4.79s, 29980, 3501 cycles
  • Parsimonious Sidetrack Based v3: 4.85s, 29980, 3501 cycles
  • Sidetrack Based: 4.18s, 29980, 3501 cycles
  • Sidetrack Based with update: 4.34s, 29980, 3501 cycles
At this time all algos worked to completion and again gave different results...
Source code

вторник, 7 ноября 2023 г.

kssp library

I`ve tried to solve CSES task "visiting cities"

Looks like you can use kind of brute-force - get 1st shortest paths, then all remained with the same cost and make union of cities in each path - nor elegant nor smart algorithm, just to estimate if this approach works at all

I remember from my university course "graph theory" about Yen`s algo to get K-th shortest path so choosed to use some ready (and hopefully well-tested) implementation - kssp library from INRIA (yep - famous place where OCaml was invented)

And then happened real madness - different algos gave me different result and moreover - they didn't match with "correct" results from CSES! Lets see what I got (all results for test 7, compilation options -O3 -DTIME -DNDEBUG):

  • Yen - 470s, 30157 cities
  • node classification - 4.37s, 30140 cities
  • postponed node classification - 3.83s, 30140 cities
  • postponed node classification with star - 3.76s, 30140 cities
  • sidetrack based - consumed 13Gb of memory and met with OOM killer
  • parsimonious sidetrack based - OOM again, perhaps bcs not enough parsimonious :-)

Source code

慢保申请有什么条件 孕激素是什么意思 三维彩超和四维彩超有什么区别 肌酸是什么东西 乙肝看什么科
尿酸高适合吃什么菜 3月16号是什么星座的 贫血喝什么口服液 10月28日什么星座 借条和欠条有什么区别
女字旁一个朱念什么 棘突是什么意思 有容乃大是什么意思 鼠妇是什么动物 乙酰磺胺酸钾是什么
卫生纸筒可以做什么 扫描件是什么意思 排卵期身体有什么症状表现吗 一什么葡萄 润喉咙什么东西最合适
2006属什么hcv9jop4ns4r.cn 属虎适合佩戴什么饰品hcv8jop5ns4r.cn 下午四点多是什么时辰jingluanji.com 沃尔玛是干什么的hcv7jop7ns2r.cn 吃什么东西能流产hcv8jop2ns1r.cn
乌鸡汤放什么材料hcv8jop9ns5r.cn 原生家庭是什么意思hcv9jop2ns4r.cn 脑委缩吃什么药能空制hcv9jop5ns7r.cn 外阴瘙痒涂什么药膏hcv8jop9ns4r.cn lena是什么意思hcv9jop3ns3r.cn
胸部正位片检查什么1949doufunao.com 世事无常什么意思mmeoe.com 怕空调冷风什么原因tiangongnft.com 粉底液是干什么用的hcv8jop8ns7r.cn 余什么意思hcv8jop3ns1r.cn
学生证件号码是什么hcv8jop5ns2r.cn 萌萌哒是什么意思hcv7jop9ns2r.cn 处暑吃什么hcv9jop3ns0r.cn 绿豆什么人不能吃hcv9jop0ns2r.cn 银杏是什么hcv8jop4ns1r.cn
百度