题目大意:

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

代码:

 #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. WebApi集成Swagger

    1.新建一个WebApi空项目 2.新建一个Person实体类: public class Person { public int ID { get; set; } public string Use ...

  2. Jmeter(三十五)_分布式

    jmeter分布式简单步骤说明: 1:添加远程服务器IP到配置文件 在JMETER_HOME / bin / jmeter.properties中,找到名为“ remote_hosts ” 的属性,并 ...

  3. How to Install MemSQL

    MemSQL runs natively on 64-bit Linux operating systems. Your system hardware must have at least 4 CP ...

  4. H5 13-子元素选择器

    13-子元素选择器 p{ color: red; } */ /* #identity>p{ color: blue; } */ div>ul>li>p{ color: purp ...

  5. Individual Project

    这次我自己完成了一个小小的项目,课可以把这篇随笔当做一次实验报告,主要的内容是用JUnit进行单元测试.由于我的技术太弱了,就在博客园里“求师”,按照大神的方法慢慢把这些东西写了下啦来. 不知道怎么搞 ...

  6. windows中在vs code终端使用bash

    vs code Visual Studio Code的缩写,很好用的一个编辑器. 终端 vs code的终端(命令行)在windows中,默认使用的是powershell,如下图所示: powersh ...

  7. 【学习总结】【Java】Git学习-上传本地已有代码到GitHub

    < Git学成归来后的第一次实战 > 上传本地已有代码到GitHub 以之前学了一小半的Java基础教程代码为例 <深坑预警:在GitHub新建仓库那一步,不要勾选readme,不然 ...

  8. python自动化常见问题汇总

           1.如何提高selenium脚本的执行速度? Selenium脚本的执行速度受多方面因素的影响,如网速,操作步骤的繁琐程度,页面加载的速度,以及我们在脚本中设置的等待时间,运行脚本的线程 ...

  9. 三、Object 对象常用操作方法

    Object 构造方法 一.asign vs 扩展运算符 ... 1.共同点:都是浅拷贝 2.开发推荐 扩展运算符... let obj={ name: 'Tom', age: 18 }; let o ...

  10. class用法

    自 PHP 5.5 起,关键词 class 也可用于类名的解析.使用 ClassName::class 你可以获取一个字符串,包含了类 ClassName 的完全限定名称.这对使用了 命名空间 的类尤 ...