Phone Call

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)

Problem Description
There are n houses in Bytetown, labeled by 1,2,...,n. In each house, there is a person living here. Little Q lives in house 1. There are n−1 bidirectional streets connecting these houses, forming a tree structure. In this problem, S(u,v) denotes the house set containing all the houses on the shortest path from house u to house v.

The Bytetown's phone line network consists of m different lines. The i-th line can be expressed as 5 integers ai,bi,ci,di,wi, which means for every two different houses u and v from set S(ai,bi)∪S(ci,di), u and v can have a phone call costing wi dollars.


Picture from Wikimedia Commons

Little Q is now planning to hold a big party in his house, so he wants to make as many as possible people known. Everyone known the message can make several phone calls to others to spread the message, but nobody can leave his house.

Please write a program to figure out the maximum number of people that can join the party and the minimum total cost to reach that maximum number. Little Q should be counted in the answer.

 
Input
The first line of the input contains an integer T(1≤T≤15), denoting the number of test cases.

In each test case, there are 2 integers n,m(1≤n,m≤100000) in the first line, denoting the number of houses and phone lines.

For the next n−1 lines, each line contains two integers u and v, denoting a bidirectional edge between node u and v.

For the next m lines, each line contains 5 integers ai,bi,ci,di,wi(1≤ai,bi,ci,di≤n,1≤wi≤109), denoting a phone line.

 
Output
For each test case, print a single line containing two integers, denoting the maximum number of people that can join the party and the minimum total cost to reach that maximum number.
 
Sample Input
1
5 2
1 2
1 3
2 4
2 5
1 3 2 4 100
2 2 4 2 10
 
Sample Output
4 210

Hint

Step 1 : 1 make a phone call to 2 using line 1, the cost is 100.
Step 2 : 1 make a phone call to 3 using line 1, the cost is 100.
Step 3 : 2 make a phone call to 4 using line 2, the cost is 10.

 

题解:

  将所有线路按代价从小到大排序,对于每条线路(a,b,c,d),首先把a到b路径上的点都合并到LCA,再把c到d路径上的点都合并到LCA,最后再把两个LCA合并即可。

  设f​i​​表示i点往上深度最大的一个可能不是和 i 在同一个连通块的祖先,每次沿着f跳即可。用路径压缩的并查集维护这个f即可得到优秀的复杂度。

  时间复杂度O(mlogm)。

#include <bits/stdc++.h>

inline long long read(){long long x=,f=;char ch=getchar();while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}return x*f;}

using namespace std;

typedef long long LL;

const int N = 2e5 + , inf = 1e9;

vector<int > G[N];
struct ss{int a,b,c,d,cost;}Q[N];
int cmp(ss s1,ss s2) {return s1.cost < s2.cost;}
int sz[N],dep[N],fa[N],son[N],indexS,top[N],pos[N],ff[N],f[N];
void dfs(int u) {
int k = ;sz[u] = ;dep[u] = dep[f[u]] + ;
for(auto to : G[u]) {
if(to == f[u]) continue;
f[to] = u;
dfs(to);
sz[u] += sz[to];
if(sz[to] > sz[k]) k = to;
}
if(k) son[u] = k;
}
void dfs(int u,int chain) {
int k = ;pos[u] = ++indexS;
top[u] = chain;
if(son[u])
dfs(son[u],chain);
for(auto to : G[u]) {
if(dep[to] > dep[u] && son[u] != to)
dfs(to,to);
}
}
int LCA(int x,int y) {
while(top[x] != top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x,y);
x = f[top[x]];
}
if(dep[x] > dep[y]) swap(x,y);
return x;
}
LL COST[N],CNT[N];
inline int finds2(int x) {return x == fa[x] ? x:fa[x] = finds2(fa[x]);}
inline int finds(int x) {return x == ff[x] ? x:ff[x] = finds(ff[x]);}
inline void merges(int x,int y,int c) {
int fx = finds2(x);
int fy = finds2(y);
if(dep[fx] < dep[fy]) swap(fx,fy);
if(fx == fy) return;
COST[fy] += COST[fx] + c;
CNT[fy] += CNT[fx];
fa[fx] = fy;
}
inline void go(int x,int zu,int c) {
while() {
x = finds(x);
if(dep[x] <= dep[zu]) return ;
merges(x,f[x],c);
ff[x] = f[x];
}
}
int n,m,T;
void init() {
for(int i = ; i <= n; ++i) G[i].clear(),top[i] = ,son[i] = ,dep[i] = ,fa[i] = ,pos[i] = ;
indexS = ;
}
int main() {
scanf("%d",&T);
while(T--) {
scanf("%d%d",&n,&m);
init();
for(int i = ; i < n; ++i){
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs();
dfs(,);
for(int i = ; i <= n; ++i) fa[i] = ff[i] = i,CNT[i] = ,COST[i] = ;
for(int i = ; i <= m; ++i)
scanf("%d%d%d%d%d",&Q[i].a,&Q[i].b,&Q[i].c,&Q[i].d,&Q[i].cost);
sort(Q+,Q+m+,cmp);
for(int i = ; i <= m; ++i) {
int lc = LCA(Q[i].a,Q[i].b);
int lb = LCA(Q[i].c,Q[i].d);
go(Q[i].a,lc,Q[i].cost);
go(Q[i].b,lc,Q[i].cost);
go(Q[i].c,lb,Q[i].cost);
go(Q[i].d,lb,Q[i].cost);
merges(lc,lb,Q[i].cost);
//cout<<lc<<" "<<lb<<endl;
}
printf("%lld %lld\n",CNT[finds2()],COST[finds2()]);
}
return ;
}

最新文章

  1. Is-A,Has-A,Use-A(转载)
  2. AppBox升级进行时 - 如何向OrderBy传递字符串参数(Entity Framework)
  3. innerHTML、innerText、outerHTML、outerText的区别
  4. 关于本地缓存localStorage
  5. 浅析jQuery删除节点的三个方法
  6. delphi之多线程编程
  7. 动手学习TCP:TCP特殊状态
  8. iOS webView的一些基本用法
  9. scope的参数范围
  10. java平台的常用资源
  11. Swift——(两)Swift访问元组
  12. JAVA框架面试题
  13. React-代码复用(mixin.hoc.render props)
  14. react-router简介
  15. flask 面试题
  16. Java访问权限修饰符public protected friendly private用法总结(转载好文Mark)
  17. keystore
  18. python datetime 模块
  19. Windows 10正式版的历史版本
  20. python简说(二十三)发邮件

热门文章

  1. jQuery设置 select、radio、checkbox 默认选中的值
  2. WEB学习 -相对定位、绝对定位、固定定位、z-index
  3. AC日记——爱情之路 codevs 2070
  4. NavBarControl(侧边导航栏)
  5. PAT 甲级 1087 All Roads Lead to Rome(SPFA+DP)
  6. JAVA通过使用sort方法排序
  7. mysql function
  8. 官方Java编码规范
  9. Mondiran创建连接
  10. host dig nslookup bind