Rosetta Problems

Pipe Fitter and the Fierce Dogs

https://atcoder.jp/contests/jag2016autumn/tasks/icpc2016autumn_h