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级算法题 中位数定义为所有值从小到大排序后排在正中间的那个数,如果值有偶数个,通常取最中间的两个数值的平均 ...
随机推荐
- Linux和Windows系统目录结构区别
Windows目录结构图 Linux目录结构图 我们所有的操作尽量都要在/home/username目录下进行. 快捷进入家目录方式是cd ~.
- ASCII,UTF-8,Unicode字符串相互转换
#include<string> #include<windows.h> #include<vector> using namespace std; //utf8 ...
- Spring Security Oauth2 : Possible CSRF detected
Spring Security Oauth2 : Possible CSRF detected 使用Spring Security 作为 Oauth2 授权服务器时,在授权服务器登录授权后,重定向到客 ...
- lambda的一些用法
lambda在函数中调用时可以不用传入形参,当需要时才传入参数,方便一些场合中的使用(当参数一直变化时,仍然需要调用函数,可以采用如下方式).如以下代码所示. import numpy as np d ...
- vue+iview的form表单校验总结
这篇文章时关于如何使用iview的form表单校验.主要学习如何使用form校验(以校验文字长度为例),以及如何动态添加校验规则和异步校验. 1.为需要校验的表单添加form标签 <!--注意: ...
- Nginx快速自查手册
本项目是一个 Nginx 极简教程,目的在于帮助新手快速入门 Nginx. demos 目录中的示例模拟了工作中的一些常用实战场景,并且都可以通过脚本一键式启动,让您可以快速看到演示效果. 概述 什么 ...
- Nginx的启动、停止等命令
Windows下Nginx的启动.停止等命令 在Windows下使用Nginx,我们需要掌握一些基本的操作命令,比如:启动.停止Nginx服务,重新载入Nginx等,下面我就进行一些简单的介绍.1.启 ...
- git bash push 本地的commit到远程 -- ssh keys设置
1. 检查是否已经创建 ssh keys git bash 下,cd ~/.ssh 如何出现“No such file or directory”,则表示需要创建一个ssh keys. 2. 创建新 ...
- 1+x证书学习日志——css 基本选择符
##css选择符 1:类型选择符 直接用标签名称当作选择符 特点:选中所有同类元素 2:id名称 ...
- ipv4与ipv6 Inet4Address类和Inet6Address类
在设置本地IP地址的时候,一些人会疑惑IPv4与IPv6的区别是什么?下面由学习啦小编为你分享ipv4与ipv6的区别的相关内容,希望对大家有所帮助. ipv4与ipv6的区别 在windows 7以 ...