题目大意:

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

代码:

 #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. 2018年计划小目标(9月)PMP

    从6.23结束敏捷的系统贯穿学习考试,6.24开始做传统项目管理,系统学习计划,打包报考,(╥╯^╰╥):ACP+实战+PMP+软考,历时两个月 每天上下班路上3个小时,听录音,(报的远程班,倍速听了 ...

  2. POJ - 3244-Difference between Triplets

    其实我最开始没有这道题...是做到UPC-11079-小P的决斗,训练结束后然后搜索了一波,才了解这个题的. 非常牛逼的题...这么多人做出来了...我好菜... 对于每对三元组Ta=(La,Ja,K ...

  3. codeforces#1090 D. New Year and the Permutation Concatenation(打表找规律)

    题意:给出一个n,生成n的所有全排列,将他们按顺序前后拼接在一起组成一个新的序列,问有多少个长度为n的连续的子序列和为(n+1)*n/2 题解:由于只有一个输入,第一感觉就是打表找规律,虽然表打出来了 ...

  4. Sagheer and Nubian Market CodeForces - 812C (二分)

    On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friend ...

  5. Jenkins Installing and migration

    JAVA_Zookeeper_hadoop - CSDN博客https://blog.csdn.net/wangmuming Installing Jenkins on Red Hat distrib ...

  6. 设置SQLServer数据库内存

    需要设置SQLServer数据库的内存配置.登录数据库,这里使用的是SQLServer2008,右键点击最上方的服务器名,在弹出的菜单中,点击属性] 打开服务器属性窗口.默认显示的是第一项[常规]内容 ...

  7. flutter-StatelessWidget与StatefulWidget

    StatelessWidget和StatefulWidget是flutter的基础组件,日常开发中自定义Widget都是选择继承这两者之一. 两者的区别在于状态的改变,StatelessWidget面 ...

  8. php redis常用方法代码例子

    1,connect 描述:实例连接到一个Redis.参数:host: string,port: int返回值:BOOL 成功返回:TRUE;失败返回:FALSE 示例: <?php $redis ...

  9. 5款Python程序员高频使用开发工具推荐

    很多Python学习者想必都会有如下感悟:最开始学习Python的时候,因为没有去探索好用的工具,吃了很多苦头.后来工作中深刻体会到,合理使用开发的工具的便利和高效.今天,我就把Python程序员使用 ...

  10. 阿里云服务器晚上运行定时任务报Too many connections

    1. 相关查询连接数的命令 mysql>show variables like '%max_connections%'; +-------------------------+--------- ...