题目大意:

给一些没安排权值的边和安排了权值的边,没被安排的边全要被选入最小生成树,问你最大能把它们的权值和安排成多少。
题目分析:
假设建好了树,那么树边与剩下的每一条边都能构成一个环,并且非树边的权值是环中最大的,所以钦定边权不大于非树边即可。用并查集维护一下。

代码:

 #include<bits/stdc++.h>
using namespace std; const int maxn = ; int n,k,m;
struct edge{int from,to,w;}p1[maxn],p2[maxn];
int pre[maxn],arr[maxn],minn[maxn],dep[maxn],fa[maxn],fd[maxn];
vector<int> g[maxn]; int found(int x){
int rx = x; while(pre[rx] != rx) rx = pre[rx];
while(pre[x] != rx){int tmp = pre[x]; pre[x] = rx; x = tmp;}
return rx;
} void read(){
scanf("%d%d%d",&n,&k,&m);
for(int i=;i<=k;i++){scanf("%d%d",&p1[i].from,&p1[i].to);}
for(int i=;i<=m;i++){scanf("%d%d%d",&p2[i].from,&p2[i].to,&p2[i].w);}
} void dfs(int now,int dp,int f){
dep[now] = dp; fa[now] = f;
for(int i=;i<g[now].size();i++){
int to;int z = g[now][i];
if(z > ){
if(p1[z].to == now) to = p1[z].from;
else to = p1[z].to;
}else{
if(p2[-z].to == now) to = p2[-z].from;
else to = p2[-z].to;
}
if(to == f) continue;
if(z) fd[to] = z;
dfs(to,dp+,now);
}
} void work(){
for(int i=;i<=n;i++) pre[i] = i;
for(int i=;i<=k;i++){
if(found(p1[i].from) != found(p1[i].to)){
pre[found(p1[i].from)] = found(p1[i].to);
g[p1[i].from].push_back(i); g[p1[i].to].push_back(i);
}
}
for(int i=;i<=m;i++){
if(found(p2[i].from) != found(p2[i].to)){
pre[found(p2[i].from)] = found(p2[i].to);
g[p2[i].from].push_back(-i); g[p2[i].to].push_back(-i);
arr[i] = ;
}
}
dfs(,,);
for(int i=;i<=n;i++) minn[i] = ,pre[i]=i;
for(int i=;i<=m;i++){
if(arr[i] == ){
int u = found(p2[i].from),v = found(p2[i].to);
while(u != v){
if(dep[u] >= dep[v]){
int z = found(fa[u]);
if(z!=)pre[u] = z; minn[fd[u]] = p2[i].w; u = z;
}else{
int z = found(fa[v]);
if(z!=)pre[v] = z; minn[fd[v]] = p2[i].w; v = z;
}
}
}
}
long long ans = ;
for(int i=;i<=k;i++){
if(minn[i] == ) {puts("-1");return;}
else ans += minn[i];
}
printf("%I64d",ans);
} int main(){
read();
work();
return ;
}

Codeforces1023F Mobile Phone Network 【并查集】【最小生成树】的更多相关文章

  1. 并查集 & 最小生成树详细讲解

    并查集 & 最小生成树 并查集 Disjoint Sets 什么是并查集?     并查集,在一些有N个元素的集合应用问题中,我们通常是在开始时让每个元素构成一个单元素的集合,然后按一定顺序将 ...

  2. ACM: 继续畅通工程-并查集-最小生成树-解题报告

    继续畅通工程 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Descri ...

  3. POJ 2236 Wireless Network (并查集)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 18066   Accepted: 761 ...

  4. [LA] 3027 - Corporative Network [并查集]

    A very big corporation is developing its corporative network. In the beginning each of the N enterpr ...

  5. LA 3027 Corporative Network 并查集记录点到根的距离

    Corporative Network Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [S ...

  6. POJ2236 Wireless Network 并查集简单应用

    Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have ...

  7. CodeForces892E 可撤销并查集/最小生成树

    http://codeforces.com/problemset/problem/892/E 题意:给出一个 n 个点 m 条边的无向图,每条边有边权,共 Q 次询问,每次给出 ki​ 条边,问这些边 ...

  8. Wireless Network 并查集

    An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wi ...

  9. hdu 1863 畅通工程 (并查集+最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1863 畅通工程 Time Limit: 1000/1000 MS (Java/Others)    M ...

随机推荐

  1. NLP是什么

    NLP是什么 而在计算机领域, NLP(Natural Language Processing),也就是人们常说的「自然语言处理」,就是研究如何让计算机读懂人类语言. 这包括,既要能让计算机理解自然语 ...

  2. tensorflow-gpu安装的一些注意

    按正确的顺序安装,严格安装特定的版本 1,下载和安装严格版本的cuda和cuDnn,其他版本的不干活.比如要求9.0你就不能装9.1.https://www.tensorflow.org/instal ...

  3. Python全栈开发之路 【第三篇】:Python基础之字符编码和文件操作

    本节内容 一.三元运算 三元运算又称三目运算,是对简单的条件语句的简写,如: 简单条件语句: if 条件成立: val = 1 else: val = 2 改成三元运算: val = 1 if 条件成 ...

  4. 1. FPGA内部的逻辑资源

    CLB(包括LUT.加法器.寄存器.MUX(多路选择器)) 时钟网络资源(全局时钟网络,区域时钟网络,IO时钟网络),理解时钟网络的本质和意义 时钟处理单元(PLL,DCM),理解时钟网络资源和时钟处 ...

  5. Django model操作

    一.各种查询统计操作   def all(self) # 获取所有的数据对象 def filter(self, *args, **kwargs) # 条件查询 # 条件可以是:参数,字典,Q def ...

  6. kubectl常用命令汇总

    #查看k8s的所有node节点 kubectl get node #查看ns的pod kubectl get pod --all-namespaces -o wide kubectl get pod ...

  7. # 【Python3练习题 004】输入某年某月某日,判断这一天是这一年的第几天?

    # [Python练习题 004]输入某年某月某日,判断这一天是这一年的第几天? # 思路:先判断是否为闰年,这关系到 2 月份的天数.# 之后再根据月份值把前几个月的天数累积加起来,最后再加上个“日 ...

  8. react的项目坑

    首先在构造页面时 应该将页面的结构分析好. 在处理数据异步时 将数据结构进行完全的简单结构化. 使用redux时 注意返回的数据是深拷贝还是浅拷贝 否则会产生 数组不为空但是没有值的问题 使用自制数据 ...

  9. VS2015 + OPENCV + CUDA 安装流程

    VS2015  https://blog.csdn.net/guxiaonuan/article/details/73775519?locationNum=2&fps=1 OPENCV htt ...

  10. Laravel 核心--Facades 门面

    Laravel 核心--Facades 门面 伊Summer 关注  0.1 2017.08.12 19:07* 字数 2017 阅读 1089评论 0喜欢 5 介绍 Facades 为应用的 IoC ...