this problem is from PAT, which website is http://pat.zju.edu.cn/contests/pat-a-practise/1050. firstly i think i can use double circulation to solve it ,however the result of two examples is proofed to be running time out. So as the problem said, it…