https://ac.nowcoder.com/acm/contest/330/G

C++版本一

题解:

std

最小生成树

看成一张图,就是把同类元素的试剂当作一个点之后,求这个图的最小生成树。
然后用你最喜欢的求MST的算法求解就好。注意判不连通的情况。

#include <bits/stdc++.h>
using namespace std;int main()
{int n, m ,k;cin >> n >> m >> k;vector<int> a(n);for (auto& t : a) cin >> t, --t;vector<int> fa(k);iota(fa.begin(), fa.end(), 0);function<int(int)> find = [&](int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); };auto unite = [&](int u, int v) { fa[find(u)] = find(v); };vector<tuple<int, int, int>> edges;for (int i = 0, u, v, c; i < m; i++){cin >> u >> v >> c;--u, --v;edges.emplace_back(c, a[u], a[v]);}sort(edges.begin(), edges.end());long long ans = 0;int cnt = 0;int c, u, v;for (auto& e : edges){tie(c, u, v) = e;if (find(u) == find(v)) continue;unite(u, v);ans += c;if (++cnt == k - 1) break;}if (cnt == k - 1)cout << ans << endl;elsecout << -1 << endl;
}扫一扫,把题目装进口袋

C++版本二

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=200000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,q,x,y,c;
ll ans,cnt,flag,temp;
int a[N];
int pre[N];
char str;
struct node{int x,y,c;bool operator<(const node&S)const{return c<S.c;}
}e[N];
int find(int x){if(x==pre[x])return x;return pre[x]=find(pre[x]);
}
int main()
{
#ifdef DEBUGfreopen("input.in", "r", stdin);//freopen("output.out", "w", stdout);
#endifscanf("%d%d%d",&n,&m,&k);//scanf("%d",&t);//while(t--){}for(int i=1;i<=n;i++)scanf("%d",&a[i]);for(int i=1;i<=m;i++){scanf("%d%d%d",&x,&y,&e[i].c);e[i].x=a[x];e[i].y=a[y];}sort(e+1,e+m+1);for(int i=1;i<=k;i++)pre[i]=i;for(int i=1;i<=m;i++){int fx=find(e[i].x);int fy=find(e[i].y);if(fx!=fy){pre[fx]=fy;ans+=e[i].c;}}for(int i=1;i<=k;i++)if(pre[i]==i)cnt++;if(cnt==1)cout << ans << endl;elsecout<<-1<<endl;//cout << "Hello world!" << endl;return 0;
}

Applese 的毒气炸弹相关推荐

  1. 【牛客 - 330G】Applese 的毒气炸弹(最小生成树,构造,判连通图)

    题干: 众所周知,Applese 是个很强的选手,它的化学一定很好. 今天他又AK了一套题觉得很无聊,于是想做个毒气炸弹玩. 毒气炸弹需要 k 种不同类型元素构成,Applese一共有 n 瓶含有这些 ...

  2. dml操作mysql_数据库DML操作(DCL了解)

    DQL:SELECT * FROM 表名 DML(数据操作语言,它是对表记录的操作(增.删.改)!) 1. 插入数据 * INTERT INTO 表名(列名1,列名2, ...) VALUES(列值1 ...

  3. Applese 的取石子游戏

    https://ac.nowcoder.com/acm/contest/330/A PHP版本一 题解: std 博弈 这是一道经典的博弈问题. 可以使用动态规划来解决:dp[i][j]表示进行了 i ...

  4. Applese 走迷宫(优先队列+bfs)

    链接:https://ac.nowcoder.com/acm/problem/22344 来源:牛客网 精通程序设计的 Applese 双写了一个游戏. 在这个游戏中,它被困在了一个 n×m 的迷宫中 ...

  5. Applese 的回文串(加一个字符的回文)

    链接:https://ac.nowcoder.com/acm/contest/330/I 来源:牛客网 自从 Applese 学会了字符串之后,精通各种字符串算法,比如--判断一个字符串是不是回文串. ...

  6. Applese 涂颜色(欧拉定理降幂+快速幂)

    链接:https://ac.nowcoder.com/acm/contest/330/E 来源:牛客网 精通程序设计的 Applese 叕写了一个游戏. 在这个游戏中,有一个 n 行 m 列的方阵.现 ...

  7. 【牛客 - 330I】Applese 的回文串(结论题,类似编辑距离,dp)

    题干: 自从 Applese 学会了字符串之后,精通各种字符串算法,比如--判断一个字符串是不是回文串. 这样的题目未免让它觉得太无聊,于是它想到了一个新的问题. 如何判断一个字符串在任意位置(包括最 ...

  8. 【牛客 -330E 】Applese 涂颜色(费马小定理,超级快速幂)

    题干: 精通程序设计的 Applese 叕写了一个游戏. 在这个游戏中,有一个 n 行 m 列的方阵.现在它要为这个方阵涂上黑白两种颜色.规定左右相邻两格的颜色不能相同.请你帮它统计一下有多少种涂色的 ...

  9. 【牛客 - 330F】Applese 的QQ群(拓扑排序,二分)

    题干: Applese 有一个QQ群.在这个群中,大家互相请教问题.如 b 向 a 请教过问题,就把 a 叫做是 b 的"老板".这样一个群中就会有很多老板. 同时规定:如果 a ...

最新文章

  1. Python脚本语言写法
  2. python键盘输入代码,python监控键盘输入实例代码
  3. mysql测试表格的年龄的语句是_MySQL查询语句练习题,测试基本够用了
  4. 识别JVM –比预期的要复杂
  5. LeetCode 1452. 收藏清单(std::includes判断子集)
  6. 如何让 Mac在运行 Windows时享受极致体验?Parallels Desktop 16给你答案!
  7. pyQt显示系统文件目录
  8. C#OOP之一面向对象简介
  9. java 模块化 soa_OSGI与SOA的千丝万缕
  10. android webview调js方法,Android中WebView与H5的交互,Native与JS方法互调
  11. SQL那些事儿(十一)--ODBC,OLE-DB,ADO.NET区别
  12. 计算机系统的工作方式,计算机的操作系统与工作方式
  13. day013内置函数一
  14. ubuntu 8.10 可用的源
  15. [System Design] 系统设计 (2) -- 数据库设计
  16. python包管理多个版本,python多版本以及各种包管理
  17. 非共识与小趋势——复盘罗辑思维7年演化之路
  18. java BMI指数计算
  19. iPhone没有声音,但插上耳机后声音正常的处理
  20. 项目启动报错 Error running ‘xxxApplication‘;Command line is too long,Shoerten command line for........

热门文章

  1. OPPO尝鲜android版本更新,安卓11迎来四大贴心升级,OPPO小米率先尝鲜
  2. gradle exclude_Gradle学习记录020 java工程的测试 part1
  3. 千里眼摄像头支持对象存储吗_【手机技术】专业相机应用Halide全面支持苹果iPhone SE 2人像模式...
  4. 查看登录oracle信息,记录Oracle用户的登录信息
  5. mysql里concat什么意思_mysql里concat函数和 ‘’|‘’ 符号一起用是表示什么?
  6. linux安装apache下载文件,Linux安装apache详解
  7. android动态添加xml布局,android – 如何动态添加视图已经在xml布局中声明的RelativeLayout?...
  8. Spring添加对缓存的支持
  9. 期末复习、化学反应工程科目(第一章)
  10. kaggle (自杀分析)