51nod 3 * problem
1640
题意:
一张无向图
在最小化最大边后求最大边权和
Slove:
sort
最小生成树
倒叙最大生成树
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string> using namespace std; #define LL long long #define gc getchar()
inline int read() {int x = , f = ; char c = gc;
while(c < '' || c > '') {if(c == '-') f = -; c = gc;}
while(c >= '' && c <= '') x = x * + c - '', c = gc; return x * f;}
inline LL read_LL() {LL x = ; char c = gc; while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc; return x;}
#undef gc const int N = 1e5 + ; int fa[N];
int A[N << ], U[N << ], V[N << ], W[N << ];
int n, m; bool Cmp(int a, int b) {return W[a] < W[b];} int Get(int x) {return fa[x] == x ? x : fa[x] = Get(fa[x]);} void Minst(int &R) {
for(int i = ; i <= n; i ++) fa[i] = i;
int js = ;
for(int i = ; i <= m; i ++) {
int fu = Get(U[A[i]]), fv = Get(V[A[i]]);
if(fu != fv) {
fa[fu] = fv;
js ++;
}
if(js == n - ) {
R = i;
while(W[A[R + ]] == W[A[i]]) R ++;
return ;
}
}
} inline long long Maxst(int R) {
for(int i = ; i <= n; i ++) fa[i] = i;
int js = ;
long long ret = ;
for(int i = R; i >= ; i --) {
int fu = Get(U[A[i]]), fv = Get(V[A[i]]);
if(fu != fv) {
fa[fu] = fv;
ret += W[A[i]];
js ++;
}
if(js == n - ) return ret;
}
} int main() {
n = read(), m = read();
for(int i = ; i <= m; i ++) A[i] = i, U[i] = read(), V[i] = read(), W[i] = read();
sort(A + , A + m + , Cmp);
int R;
Minst(R);
cout << Maxst(R);
return ;
}
1649
由于 1 - n 之间一定存在一种直接相连的道路
判断哪种直接相连
跑另外一种的最短路
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string> using namespace std; #define LL long long #define gc getchar()
inline int read() {int x = ; char c = gc; while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc; return x;}
inline LL read_LL() {LL x = ; char c = gc; while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc; return x;}
#undef gc const int N = , oo = ; int Map[N][N], Bmap[N][N];
int n, m; int main() {
n = read(), m = read();
for(int i = ; i <= n; i ++) for(int j = ; j <= n; j ++) Map[i][j] = oo;
for(int i = ; i <= n; i ++) Map[i][i] = ;
for(int i = ; i <= n; i ++) for(int j = ; j <= n; j ++) Bmap[i][j] = oo;
for(int i = ; i <= n; i ++) Bmap[i][i] = ;
for(int i = ; i <= m; i ++) {
int u = read(), v = read();
Map[u][v] = Map[v][u] = ;
}
if(Map[][n] == ) {
for(int i = ; i <= n; i ++)
for(int j = ; j <= n; j ++) {
if(i == j) continue;
if(Map[i][j] == oo) Bmap[i][j] = ;
}
for(int k = ; k <= n; k ++)
for(int i = ; i <= n; i ++)
for(int j = ; j <= n; j ++)
Bmap[i][j] = min(Bmap[i][j], Bmap[i][k] + Bmap[k][j]);
if(Bmap[][n] == oo) cout << -;
else cout << Bmap[][n];
} else {
for(int k = ; k <= n; k ++)
for(int i = ; i <= n; i ++)
for(int j = ; j <= n; j ++)
Map[i][j] = min(Map[i][j], Map[i][k] + Map[k][j]);
if(Map[][n] == oo) cout << -;
else cout << Map[][n];
}
return ;
}
1535
图是树的充要条件
$m = n - 1$ && 图联通
由于题目无自环
所以不存在二元环
并且若 $m >= n - 1$
则图联通
此时若 $m = n$
那么就会存在且只存在一个三元环(或更大)
因此只需判断 $n = m$ 即可
if(n == m) cout << "FHTAGN!";
else cout << "NO";
51nod 3 * problem的更多相关文章
- 51nod 算法马拉松 34 Problem D 区间求和2 (FFT加速卷积)
题目链接 51nod 算法马拉松 34 Problem D 在这个题中$2$这个质数比较特殊,所以我们先特判$2$的情况,然后仅考虑大于等于$3$的奇数即可. 首先考虑任意一个点对$(i, j)$ ...
- 51nod 1622 集合对[算法马拉松19 C]
题目链接:https://www.51nod.com/contest/problem.html#!problemId=1622 第一次参加算法马拉松,我就是去看大神们疯狂秒题,然后感受绝望的orz.. ...
- 51nod算法马拉松 contest7
A题 链接:http://www.51nod.com/contest/problem.html#!problemId=1417 推荐链接:http://blog.csdn.net/a837199685 ...
- pku 1401 Factorial 算数基本定理 && 51nod 1003 阶乘后面0的数量
链接:http://poj.org/problem?id=1401 题意:计算N!的末尾0的个数 思路:算数基本定理 有0,分解为2*5,寻找2*5的对数,2的因子个数大于5,转化为寻找因子5的个数. ...
- 51nod 1103 N的倍数(抽屉原理)
1103 N的倍数 题目来源: Ural 1302 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 一个长度为N的数组A,从A中选出若干个数,使得这些数的和是N的倍 ...
- 51nod 1486 大大走格子(容斥原理)
1486 大大走格子 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 有一个h行w列的棋盘,里面有一些格子是不能走的,现在要 ...
- 51nod 1204 Parity(并查集应用)
1204 Parity 题目来源: Ural 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 你的朋友写下一串包含1和0的串让你猜,你可以从中选择一个连续的子串 ...
- 51nod 1364 最大字典序排列(线段树)
1364 最大字典序排列基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 给出一个1至N的排列,允许你做不超过K次操作,每次操作可以将相邻的两个数交换,问能够得到的字 ...
- 51nod 1682 中位数计数
1682 中位数计数基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 中位数定义为所有值从小到大排序后排在正中间的那个数,如果值有偶数个,通常取最中间的两个数值的平均 ...
随机推荐
- vue.js+DRF跨域访问图片
一.背景 在前后端分离过程时,后端服务器只开放本地接口,而前端则开放IP接口,在DRF响应请求时,会把域名(locahost+port)响应给前端服务器,前端服务器把再把数据响应给浏览器,浏览器在解析 ...
- ASCII&UNICODE编码演化
ASCII 上个世纪60年代,美国制定了基于拉丁字母的一套电脑编码系统,取名为ASCII.它主要用于显示现代英语和其他西欧语言,是现今最通用的单字节编码系统. ASCII码使用指定的7位或8位二进制数 ...
- vue的data里面的值是数组时,在更改其某一项的时候,怎么触发视图的重新渲染?
1. 设置对象或数组的值:Vue.set(target,key,value) :2.删除对象或数组中元素: Vue.delete ( target,key) ;3. 数组对象直接修改属性,可以触发视图 ...
- TensorFlow实现Softmax Regression识别手写数字
本章已机器学习领域的Hello World任务----MNIST手写识别做为TensorFlow的开始.MNIST是一个非常简单的机器视觉数据集,是由几万张28像素*28像素的手写数字组成,这些图片只 ...
- oracle:sql添加合计语句(union)
转自:https://blog.csdn.net/qq_43563538/article/details/90370925 原表见下图: 查询全部的语句: SELECT * from BD_MARK_ ...
- laravel管理模型插入
post控制器public function comment(Post $post,Request $request){ try{ if(empty($request->content)){ E ...
- Linux开机自动启动服务
当我们的Linux,关机后,或者重启后,有些服务需要人工启动才能有.为了解决这个问题,我找了一个办法,用一个脚本,开机启动想启动的服务. 方法一:(强烈推荐) 1.写一个脚本auto.sh vim ...
- Apache老版本下载地址
有时候我们想做测试或者使用旧版本软件时,往往发现官方网站已经下架,以Apache为例: Apache旧版本下载地址:http://archive.apache.org/dist/httpd/ .... ...
- LNMP环境搭建BBS论坛及伪静态
我们在mysql备份 LNMP环境中的数据库迁移为独立的服务器的基础上搭建BBS论坛: [root@bqh-117 ~]# mysql -uroot -p123456 Welcome to the ...
- k8s中的网络(较详细汇总)
目录 一.网络前提条件-网络模型 二.需要解决的网络问题 1.容器和容器之间的网络 2.pod与pod之间的网络 同一台node节点上pod和pod通信 不同node节点上pod和pod通信 3.po ...