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级算法题 中位数定义为所有值从小到大排序后排在正中间的那个数,如果值有偶数个,通常取最中间的两个数值的平均 ...
随机推荐
- MySQL数据安全
MySQL服务器没有公网IP 也不能通过映射/NAT等方案让外部可访问 也不能绑定全部网卡,明确指定IP 授权时,务必限制IP段/域名/主机名,而不是全局 权限务必严格控制,不过度放权 绝对不能使用弱 ...
- java之aop使用及自定义注解
目的: 1.Java注解简介 2.Java元注解(重点) 3.自定义注解 案例一(获取类与方法上的注解值) 案例二(获取类属性上的注解属性值) 案例三(获取参数修饰注解对应的属性值) 4.Aop自定义 ...
- SSM整合所需的maven配置文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- hdu1171 灵活的运用背包问题咯。。。 还有!!!! 合理的计算数组的范围!! wa了好多次!
Problem Description Nowadays, we all know that Computer College is the biggest department in HDU. Bu ...
- Powershell学习笔记:(二)、基础知识
从Window7以后,WIndows系统都自带了Windows PowerShell. 自带版本如下 WIndow7 2.0 WIndow8 3.0 Window8.1 4.0 Win ...
- MySQL 乱码问题解决
修改 配置文件 只需留下 my.ini文件,然后修改其编码配置. 配置如下 # Example MySQL config file for large systems. # # This is for ...
- 【转载】 腾讯云通过设置安全组禁止某些IP访问你的服务器
有时候我们在运维网站的过程中会发现一些漏洞扫描者的IP信息,或者个人爬虫网站的IP信息,此时我们想禁止掉这些IP访问到你的服务器,可以通过腾讯云的安全组功能来设置禁止这些IP访问你的服务器,也可以通过 ...
- bootstrap-table分页
首先引用css: <link rel="stylesheet" type="text/css" href="css/bootstrap.css& ...
- HBase分布式搭建常见错误
[root@node001 bin]# hbase shell SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found bin ...
- c# 类嵌套