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级算法题 中位数定义为所有值从小到大排序后排在正中间的那个数,如果值有偶数个,通常取最中间的两个数值的平均 ...
随机推荐
- ubuntu下使用eclipse调试jni无法获取环境变量,本地库(java.library.path,LD_LIBRARY_PATH)等问题的解决。
首先要把本地库全部配置到LD_LIBRARY_PATH中. 然后一定要采用命令行方式启动eclipse(也可以写一个启动shell,通过桌面启动器打开这个shell),这样环境变量才会有效. 打开终端 ...
- SQL Server2008 查找用户登录日志
select loginname,accdate from sys.syslogins
- mysql8.0入坑体验
正常从官网下载,并且正常安装,直到安装完成.然后用navicate连接,发现报错信息如下所示Client does not support authentication protocol reques ...
- kafka的安装及使用(单节点)
介绍了linux环境下,kafka 服务的安装与配置 安装 jdk 环境 下载 kafka 源码包放到服务器,解压 开启 zookeeper 开启 kafka server 创建主题 开启生产者 开启 ...
- restTemplate源码解析(目录)
restTemplate是spring实现的,基于restful风格的http请求模板.使用restTemplate可以简化请求操作的复杂性,同时规范了代码风格.本系列文章,将根据以下目录顺序,从源码 ...
- 【转载】C#中PadLeft函数按特定字符补足字符串长度
在C#开发过程中字符串String类处理过程中,有时字符串长度不够时,需要在左侧指定特定的字符来补足字符串长度,此时可以使用String类下的PadLeft方法对字符串的左边进行按特定的字符和特定的长 ...
- python day3 int,str,list类型补充
目录 python day 3 1. int类小知识点 2. str类小知识点 3. list类小知识点 python day 3 (学习资料来自老男孩教育) 2019/10/06 1. int类小知 ...
- 读取普通java web项目下的WEB-INF目录下的配置文件(application.xml,xx.properties等配置文件)
一.在Java web工程WEB-INF下创建weixin.properties属性文件 weixin.properties属性文件里内容如下: 二.创建PropertiesUtils.java测试类 ...
- 宽字节 多字节 mbstowcs wcstombs
函数 size_t wcstombs(char *dest, const wchar_t *src, size_t n); //wide-character to a multibyte n:被写入到 ...
- cookie和session以及iOS cookie的查取
Cookie的工作原理 http是无状态的,这是什么意思呢?就是说,在没有cookie之前,你第一次访问这个页面和第二次访问这个页面, 服务器是不知道的,不知道前一次是你.那么问题来了,我怎么登录,登 ...