A11249 | Harry Vs Voldemort
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
After destroying all of Voldemort's Horcruxes, Harry and Voldemort are up for the final battle. They each cast spells from their wands and the spells collide.
The battle scene is Hogwarts, which can be represented in the form of a tree. There are, in total, $n$ places in Hogwarts joined using $n-1$ undirected roads.
Ron, who was viewing this battle between Harry and Voldemort, wondered how many triplets of places $(u,v,w)$ are there such that if Harry is standing at place $u$ and Voldemort is standing at place $v$ , their spells collide at a place $w$ . This is possible for a triplet only when $u$ , $v$ and $w$ are distinct, and there exist paths from $u$ to $w$ and from $v$ to $w$ which do not pass through the same roads.
Now, due to the battle havoc, new paths are being added all the time. You have to tell Ron the answer after each addition.
Formally, you are given a tree with $n$ vertices and $n-1$ edges. $q$ new edges are being added between the nodes of the tree. After each addition you need to tell the number of triplets $(u,v,w)$ such that $u$ , $v$ and $w$ are distinct and there exist two paths, one between $u$ and $w$ , another between $v$ and $w$ such that these paths do not have an edge in common.
The battle scene is Hogwarts, which can be represented in the form of a tree. There are, in total, $n$ places in Hogwarts joined using $n-1$ undirected roads.
Ron, who was viewing this battle between Harry and Voldemort, wondered how many triplets of places $(u,v,w)$ are there such that if Harry is standing at place $u$ and Voldemort is standing at place $v$ , their spells collide at a place $w$ . This is possible for a triplet only when $u$ , $v$ and $w$ are distinct, and there exist paths from $u$ to $w$ and from $v$ to $w$ which do not pass through the same roads.
Now, due to the battle havoc, new paths are being added all the time. You have to tell Ron the answer after each addition.
Formally, you are given a tree with $n$ vertices and $n-1$ edges. $q$ new edges are being added between the nodes of the tree. After each addition you need to tell the number of triplets $(u,v,w)$ such that $u$ , $v$ and $w$ are distinct and there exist two paths, one between $u$ and $w$ , another between $v$ and $w$ such that these paths do not have an edge in common.
输入格式
First line contains an integer $n$ ( $1<=n<=10^{5}$ ), the number of places in Hogwarts.
Each of the next $n-1$ lines contains two space separated integers $u$ and $v$ ( $1<=u,v<=n$ ) indicating a road between places $u$ and $v$ . It is guaranteed that the given roads form a connected tree.
Next line contains a single integer $q$ ( $1<=q<=10^{5}$ ), the number of new edges being added.
Each of the next $q$ lines contains two space separated integers $u$ and $v$ ( $1<=u,v<=n$ ) representing the new road being added.
Note that it is possible that a newly added road connects places that were connected by a road before. Also, a newly added road may connect a place to itself.
Each of the next $n-1$ lines contains two space separated integers $u$ and $v$ ( $1<=u,v<=n$ ) indicating a road between places $u$ and $v$ . It is guaranteed that the given roads form a connected tree.
Next line contains a single integer $q$ ( $1<=q<=10^{5}$ ), the number of new edges being added.
Each of the next $q$ lines contains two space separated integers $u$ and $v$ ( $1<=u,v<=n$ ) representing the new road being added.
Note that it is possible that a newly added road connects places that were connected by a road before. Also, a newly added road may connect a place to itself.
输出格式
In the first line print the value for the number of triplets before any changes occurred.
After that print $q$ lines, a single integer $ans_{i}$ in each line containing the value for the number of triplets after $i$ -th edge addition.
After that print $q$ lines, a single integer $ans_{i}$ in each line containing the value for the number of triplets after $i$ -th edge addition.
输入输出样例
输入 #1
3 1 2 2 3 1 2 3
输出 #1
2 4
输入 #2
4 1 2 2 3 2 4 2 1 4 3 4
输出 #2
6 18 24
输入 #3
5 1 2 2 3 3 4 4 5 1 1 5
输出 #3
20 60
In the first sample case, for the initial tree, we have $(1,3,2)$ and $(3,1,2)$ as the only possible triplets $(u,v,w)$ .
After addition of edge from $2$ to $3$ , we have $(1,3,2)$ , $(3,1,2)$ , $(1,2,3)$ and $(2,1,3)$ as the possible triplets.
After addition of edge from $2$ to $3$ , we have $(1,3,2)$ , $(3,1,2)$ , $(1,2,3)$ and $(2,1,3)$ as the possible triplets.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted