Hdu-6035 Colorful Tree(dfs)
来源:互联网 发布:知轩藏书论坛 编辑:程序博客网 时间:2024/05/24 06:22
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 375 Accepted Submission(s): 126
Total Submission(s): 375 Accepted Submission(s): 126
Problem Description
There is a tree with n nodes, each of which has a type of color represented by an integer, where the color of node i is ci .
The path between each two different nodes is unique, of which we define the value as the number of different colors appearing in it.
Calculate the sum of values of all paths on the tree that hasn(n−1)2 paths in total.
The path between each two different nodes is unique, of which we define the value as the number of different colors appearing in it.
Calculate the sum of values of all paths on the tree that has
Input
The input contains multiple test cases.
For each test case, the first line contains one positive integersn , indicating the number of node. (2≤n≤200000)
Next line containsn integers where the i -th integer represents ci , the color of node i . (1≤ci≤n)
Each of the nextn−1 lines contains two positive integers x,y (1≤x,y≤n,x≠y) , meaning an edge between node x and node y .
It is guaranteed that these edges form a tree.
For each test case, the first line contains one positive integers
Next line contains
Each of the next
It is guaranteed that these edges form a tree.
Output
For each test case, output "Case #x : y " in one line (without quotes), where x indicates the case number starting from 1 and y denotes the answer of corresponding case.
Sample Input
31 2 11 22 361 2 1 3 2 11 21 32 42 53 6
Sample Output
Case #1: 6Case #2: 29分析:考虑每种颜色对答案的贡献,对于一种颜色我们考虑将其从图中扣去,那么只要计算剩下的每个连通快的大小就能算出这种颜色对答案的贡献了,这一步我们把同颜色的点按dfs排序后可以o(n)的递归做.#include<bits/stdc++.h>#define N 200005using namespace std;typedef long long ll;int n,x,y,cnt,Time,c[N],In[N],Out[N];ll tot;vector<int> G[N],color[N];void dfs(int u){ In[u] = ++cnt; for(int v : G[u]) if(!In[v]) dfs(v); Out[In[u]] = cnt;}ll got(ll x){ return x*(x-1)/2;}ll Count(int k,int s,int t,int l,int r){ int tot = r-l+1; ll ans = 0; while(s <= t) { int head = In[color[k][s]]+1,tail = Out[In[color[k][s]]]; tot -= tail - head + 2; s++; while(head <= tail) { int he = head,ta = Out[he]; int i = s; while(i <= t && In[color[k][i]] <= ta) i++; ans += Count(k,s,i-1,he,ta); s = i; head = ta + 1; } } return ans + got(tot);}struct cmp{ bool operator() (int x,int y) { return In[x] <= In[y]; }}Cmp;int main(){ cin.sync_with_stdio(false); while(cin>>n) { cnt = tot = 0; for(int i = 1;i <= n;i++) color[i].clear(),G[i].clear(),In[i] = Out[i] = 0; for(int i = 1;i <= n;i++) { cin>>c[i]; color[c[i]].push_back(i); } for(int i = 1;i < n;i++) { cin>>x>>y; G[x].push_back(y); G[y].push_back(x); } dfs(1); for(int i = 1;i <= n;i++) { sort(color[i].begin(),color[i].end(),Cmp); tot += n*(n-1ll)/2 - Count(i,0,color[i].size()-1,1,n); } cout<<"Case #"<<++Time<<": "<<tot<<endl; }}
阅读全文
0 0
- Hdu-6035 Colorful Tree(dfs)
- hdu 6035 Colorful Tree(dfs)
- hdu 6035 Colorful Tree(dfs)
- HDU 6035 Colorful Tree dfs
- HDU-6035 Colorful Tree(dfs序造树)
- HDU 6035-Colorful Tree(dfs+树)
- hdu 6035 Colorful Tree
- HDU 6035 colorful tree
- [HDU]-6035 Colorful Tree
- hdu 6035 Colorful Tree
- hdu--6035--Colorful Tree
- hdu 6035 Colorful Tree
- HDU 6035 Colorful Tree
- hdu 6035 Colorful Tree
- HDU 6035 Colorful Tree
- HDU 6035 Colorful Tree
- hdu 6035(Colorful Tree)
- HDU 6035 Colorful Tree (树形DP,dfs)
- Java面向对象入门讲解(深入浅出)
- 理解angular中的module和injector,即依赖注入
- Python学习17_边缘与轮廓
- bootstrapvalidato
- Spring Data Mongo单元测试Junit
- Hdu-6035 Colorful Tree(dfs)
- android开源库---Dagger2入门学习(简单使用)
- K-means 聚类算法的理解与案例实战
- Mac_java开发_重要但不常用命令集
- Hadoop基础教程-第10章 HBase:Hadoop数据库(10.5 HBase Shell)(草稿)
- 第十三天java学习笔记
- 04期:从JS垃圾回收机制和词源来透视闭包
- 哈夫曼树的构造过程
- 第十二天java学习笔记