CF F. Royal Questions kruskal

Code:

#include<bits/stdc++.h>
#define ll long long
#define maxn 200004
using namespace std;
void setIO(string s) {
    string in=s+".in";
    freopen(in.c_str(),"r",stdin);
}
int n,m;
int p[maxn],tag[maxn];
void init() {
    for(int i=0;i<maxn;++i) p[i]=i;
}
int find(int x) {
    return p[x]==x?x:p[x]=find(p[x]);
}
struct Edge {
    int u,v,c;
}ed[maxn];
bool cmp(Edge a,Edge b) {
    return a.c>b.c;
}
int main() {
    // setIO("input");
    init();
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;++i) scanf("%d%d%d",&ed[i].u,&ed[i].v,&ed[i].c);
    sort(ed+1,ed+1+m,cmp);
    ll ans=0;
    for(int i=1;i<=m;++i) {
        int a=find(ed[i].u), b=find(ed[i].v);
        if(a!=b&&(!tag[a]||!tag[b])) ans+=1ll*ed[i].c, tag[a]|=tag[b], p[b]=a;
        else if(a==b && !tag[a]) ans+=1ll*ed[i].c, tag[a]=1;
    }
    printf("%I64d\n",ans);
    return 0;
}

  

原文地址:https://www.cnblogs.com/guangheli/p/11263392.html

时间: 2024-11-03 02:04:12

CF F. Royal Questions kruskal的相关文章

【CF875F】Royal Questions 最小生成基环树森林

[CF875F]Royal Questions 题意:国王的n个王子该结婚了!现在从外国来了m位公主,第i位公主的嫁妆是wi.由于进步思想的传播,每个公主在选择配偶的事情上是有自主权的,具体地,每个公主愿意从两个王子ai和bi中选取一个托付终生.而王子们就比较倒霉了,他们只能听从父王的安排.但是国王的目的并不在于为王子找到配偶,而是为了获得更多的嫁妆.现在国王可以任意安排哪个王子和哪个公主结婚(前提是公主愿意),并且不必为每个王子和公主都找到配偶,他想知道他最多能获得的总嫁妆是多少. 题解:标题

cf F. Shovels Shop

https://codeforces.com/contest/1154/problem/F 给定m个折扣 每个折扣的{x,y}的意思是每次购买如果买到确切的x只铲子就其中的最便宜的y只铲子免付: 先贪心一波,因为要买k只,而不管折扣怎么变,怎么买,我们都要买原价最便宜的k只铲子,这贪心是对的 接着就选择折扣offers ,明显要dp解决,贪心贪不出(复杂度太高): 设dp[i]表示买i只铲子的最小花费: 所以转移方程:dp[i]=min(f[i],(选择折扣的处理)): #include<bit

CF F. Shovels Shop(前缀和预处理+贪心+dp)

F. Shovels Shop time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output There are nn shovels in the nearby shop. The ii -th shovel costs aiai bourles. Misha has to buy exactly kk shovels. Each sho

Codeforces 875F Royal Questions (看题解)

我还以为是什么板子题呢... 我们把儿子当做点, 公主当做边, 然后就是求边权值最大基环树森林. #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL, int> #define PII pair<int, int> #de

POJ 1861:Network(最小生成树&amp;&amp;kruskal)

Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13266   Accepted: 5123   Special Judge Description Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the c

POJ 1251 Jungle Roads(kruskal)

Submit Status Practice POJ 1251 Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the l

kruskal 【MST】

const int MAXN = 110;//点 const int MAXM = 10000;//边 int f[MAXN];//并查集使用 struct Edge { int u, v, w; }edge[MAXN]; int tol;//边数 初始化 0 void addedge(int u,int v,int w) { edge[tol].u = u; edge[tol].v = v; edge[tol++].w = w; } bool cmp(Edge a,Edge b) { retu

HDU 4786(最小生成树 kruskal)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4786 Problem Description Coach Pang is interested in Fibonacci numbers while Uncle Yang wants him to do some research on Spanning Tree. So Coach Pang decides to solve the following problem: Consider a bi

POJ 1861:Network(最小生成树&amp;amp;&amp;amp;kruskal)

Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13266   Accepted: 5123   Special Judge Description Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the c