The Quest for the Sacred Groves
시간 제한1초메모리 제한2048 MB
주어진 트리에서 순열의 연속 부분 구간이 유도하는 부분 그래프가 연결되도록 하는 구간의 개수를 센다.
문제
In the heart of ancient Romania, where dense forests meet towering mountains, there lies the enchanted kingdom of Vatra Codrilor. The kingdom is protected by sacred groves, numbered from to , each watched over by a guardian spirit. These groves are connected by secret paths known only to the wise elders, forming a vast and ancient tree of life. The paths are pure and free of any treacherous loops, ensuring that a traveler can always find their way through the kingdom without getting lost. Formally, the secret paths form a tree.
One night, as the full moon rises, the guardians receive a divine message from the Dacian gods. The message is an ancient scroll with a sacred list called . Formally, is a sequence of length where every number from to appears exactly once. This list tells the guardians the order in which they must stand in the final battle to protect the forest.
However, the wise guardians know that there's more to this list. For each subsegment of the list, if the groves are all connected through the secret paths without involving any other groves, the guardians from these groves can meet together and harness the power of the forest's magic.
Your challenge is to help the guardians discover how many such subsegments exist in the sacred list . Can you count the magical segments in the dance of the guardians, ensuring the power of the groves remains strong and united?
입력
The first line contains an integer ().
Each of the next lines contains two integers, and (), denoting an edge of the tree.
The last line contains the distinct integers ().
출력
You need to write a single line with an integer: the number of subsegments such that and the groves form a connected undirected graph.