Codeforces1023F Mobile Phone Network 【并查集】【最小生成树】
题目大意:
给一些没安排权值的边和安排了权值的边,没被安排的边全要被选入最小生成树,问你最大能把它们的权值和安排成多少。
题目分析:
假设建好了树,那么树边与剩下的每一条边都能构成一个环,并且非树边的权值是环中最大的,所以钦定边权不大于非树边即可。用并查集维护一下。
代码:
#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 【并查集】【最小生成树】的更多相关文章
- 并查集 & 最小生成树详细讲解
并查集 & 最小生成树 并查集 Disjoint Sets 什么是并查集? 并查集,在一些有N个元素的集合应用问题中,我们通常是在开始时让每个元素构成一个单元素的集合,然后按一定顺序将 ...
- ACM: 继续畅通工程-并查集-最小生成树-解题报告
继续畅通工程 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Descri ...
- POJ 2236 Wireless Network (并查集)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 18066 Accepted: 761 ...
- [LA] 3027 - Corporative Network [并查集]
A very big corporation is developing its corporative network. In the beginning each of the N enterpr ...
- LA 3027 Corporative Network 并查集记录点到根的距离
Corporative Network Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu [S ...
- POJ2236 Wireless Network 并查集简单应用
Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have ...
- CodeForces892E 可撤销并查集/最小生成树
http://codeforces.com/problemset/problem/892/E 题意:给出一个 n 个点 m 条边的无向图,每条边有边权,共 Q 次询问,每次给出 ki 条边,问这些边 ...
- Wireless Network 并查集
An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wi ...
- hdu 1863 畅通工程 (并查集+最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1863 畅通工程 Time Limit: 1000/1000 MS (Java/Others) M ...
随机推荐
- 05 Django REST Framework 分页
01-分页模式 rest framework中提供了三种分页模式: from rest_framework.pagination import PageNumberPagination, LimitO ...
- A+B大数运算
基础加法大数运算: [https://vjudge.net/problem/HDU-1002] 题目: 输入两个长度不超过1000的整数求出sum. 思路: 由于数字很大不能直接加,用字符串形式输入, ...
- ElasticSearch Nosql
把 ElasticSearch 当成是 NoSQL 数据库 Elasticsearch 可以被当成一个 "NoSQL"-数据库来使用么? NoSQL 意味着在不同的环境下存在不同的 ...
- 在做stark中一些反射的问题。
hasattr(obj,name): 判断一个对象里面是否有name属性或者name方法,返回BOOL值,有name特性返回True, 否则返回False.需要注意的是name要用括号括起来 1 ...
- 解决selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
解决方案: 1.查看浏览器当前版本:chrome://version/. 2.到https://sites.google.com/a/chromium.org/chromedriver/downloa ...
- Spring Mvc和Spring Boot读取Profile方式
spring boot java代码中获取spring.profiles.active - u013042707的专栏 - CSDN博客https://blog.csdn.net/u013042707 ...
- oracle小记:dba_data_files
今天给表空间扩展的时候,使用了dba_data_files进行查询.查阅了网上的资料. 该系统系统中含有以下字段 每个字段的含义如下:
- ssm项目跨域访问
最近使用ssm开发了一个项目,为了项目的开发速度,采用的是前后端同时开发,所以前端文件没有集成在项目中,最后在调试时涉及到了跨域.跨域的解决方法很多,我采用的是最简单的一种,代码如下: 新建一个过滤器 ...
- 47.Majority Element I & II
Majority Element I 描述 给定一个整型数组,找出主元素,它在数组中的出现次数严格大于数组元素个数的二分之一. You may assume that the array is non ...
- 安装sqlprompt
特别说明:注册机会报毒,安装前请先关闭杀毒软件!下载好附件之后解压,打开SQLPrompt_7.2.0.241.exe按照提示安装完成.安装完成后断网!打开数据库,会在菜单栏中看到SQL Prompt ...