Fox and Minimal path 题解
题目大意
构造一张无向图,使得从 \(1\) 到 \(2\) 的最短路数量为 \(k\)。
思路分析
我们首先可以发现当 \(k = 2^t\) 时的构造方式:
其中只有 \(O(\log k)\) 个点。
当 \(k\not = 2^t\) 时,我们可以将 \(k\) 二进制拆分,对于每一个二进制位重复上述操作,就可以得到:
当然,为了保证最短路长度相同,少的部分需要用一条链补齐。
算一下发现点数为 \(O(\log^2k)\),当 \(k\) 达到极限数据 \(2^{29}-1\) 时点数会超过 \(1000\),无法通过。
我们发现,那些用链补齐的部分是相当浪费的,也就是说,我们可以将所有用于补齐的链合并成一条:
这样我们的点数虽然依然是 \(O(\log^2 k)\) 的,但减少了一部分点,在极限数据时只需要 \(900\) 个点,可以通过。
代码
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const int N = 2010, M = 50;
#define inf 0x3f3f3f3f
int n, k, len, tot = 3;
int G[N][N], a[M], need[M];
void add(int u, int v){
G[u][v] = G[v][u] = 1;
}
void solve(int x){
int num = tot;
if (x == 0) {
add(num, num + 1);
add(num + 1, num + 2);
add(num + 2, num + 3);
tot += 3;
if (x == len - 1) add(num + 3, 2);
else add(num + 3, need[len - 3]);
return ;
}
if (x == 1) {
add(num, num + 1);
add(num, num + 2);
add(num + 1, num + 3);
add(num + 2, num + 4);
add(num + 3, num + 5);
add(num + 4, num + 5);
tot += 5;
if (x == len - 1) add(num + 5, 2);
else add(num + 5, need[len - 3]);
return ;
}
add(num, num + 1);
add(num, num + 2);
tot += 2;
for (int i = 1; i < x; i ++) {
add(tot - 1, tot + 1);
add(tot - 1, tot + 2);
add(tot, tot + 1);
add(tot, tot + 2);
tot += 2;
}
add(tot - 1, tot + 1);
add(tot, tot + 1);
tot ++;
add(tot, need[len - x - 1]);
}
int main(){
cin >> k;
while (k) {
a[++ len] = (k & 1);
k >>= 1;
}
for (int i = 1; i <= len; i ++) add(tot, tot + 1), tot ++;
add(tot, 2);
need[0] = 2;
for (int i = 1; i <= len; i ++) need[len - i + 1] = i + 3;
for (int i = 1; i <= len; i ++)
if (a[i]) {
add(1, ++ tot);
solve(i - 1);
}
printf("%d\n", tot);
for (int i = 1; i <= tot; i ++) {
for (int j = 1; j <= tot; j ++)
printf("%c", G[i][j] ? 'Y' : 'N');
printf("\n");
}
return 0;
}
Fox and Minimal path 题解的更多相关文章
- Codeforces Round #228 (Div. 1) B. Fox and Minimal path 构造
B. Fox and Minimal path 题目连接: http://codeforces.com/contest/388/problem/B Description Fox Ciel wants ...
- codeforces 389 D. Fox and Minimal path(构造+思维)
题目链接:https://vjudge.net/contest/175446#problem/J 题解:显然要用最多n个点构成的图要使的得到的最短路条数有1e9次个,显然要有几个数相乘容易想到2的几进 ...
- codeforces 388B Fox and Minimal path
这个题目的突破口就是固定最短长度,然后以二进制的形式分层: 最后把需要的曾连起来: #include<cstdio> #include<cstring> #define max ...
- CF #228 div1 B. Fox and Minimal path
题目链接:http://codeforces.com/problemset/problem/388/B 大意是用不超过1000个点构造一张边权为1的无向图,使得点1到点2的最短路的个数为给定值k,其中 ...
- Codeforces Round #228 (Div. 1) 388B Fox and Minimal path
链接:http://codeforces.com/problemset/problem/388/B [题意] 给出一个整数K,构造出刚好含有K条从1到2的最短路的图. [分析] 由于是要自己构造图,当 ...
- 【字典树】【树】【二进制】bzoj1954/POJ3764The xor-longest Path 题解
建立字典树是异或的一种处理方法. Description In an edge-weighted tree, the xor-length of a path p is defined as ...
- 洛谷 UVA12101 Prime Path 题解
一道经典的BFS 用四个for搜索四位就行了,只要能推出怎么只变4位中的一位就很水了 #include<iostream> #include<cstring> #include ...
- CF1327D Infinite Path 题解
原题链接 太坑了我谔谔 简要题意: 求一个排列的多少次幂能达到另一个排列.排列的幂定义见题.(其实不是新定义的,本来就是这么乘的) 很显然,这不像快速幂那样可以结合律. 既然这样,就从图入手. 将 \ ...
- Codeforces Round #228 (Div. 1) B
B. Fox and Minimal path time limit per test 1 second memory limit per test 256 megabytes input stand ...
- Codeforces389D(SummerTrainingDay01-J)
D. Fox and Minimal path time limit per test:1 second memory limit per test:256 megabytes input:stand ...
随机推荐
- 加密流量识别检测(一)——在VM虚拟机上搭建指定拓扑
- 【阅读笔记】提升example-based SISR七个技巧
论文信息 [Seven ways to improve example-based single image super resolution]-Radu Timofte, 2016, CVPR 论文 ...
- Typecho博客部署一言接口
开始部署 下载代码上传至你的网站目录,把解压出来的文件夹改名为hitokoto 然后访问https://域名及文件路径/hitokoto查看效果 示例:https://sunpma.com/other ...
- docker 公有仓库与私有仓库常见操作
本文为博主原创,转载请注明出处: 自建一个Docker仓库,可以使用Docker官方提供的开源项目Docker Registry.以下是一些基本步骤: 安装Docker Registry: 在服务器上 ...
- tensorflow神经网络归一化方法
参考https://blog.csdn.net/chary8088/article/details/81542879
- C语言循环坑 -- continue的坑
文章目录 前言 一.continue语法 1.continue的作用 2.语法 二.大坑项目 题目 分析 正确写法 三.进坑调试 第一种 第二种 总结 前言 在使用continue和break时,会出 ...
- Kubernetes亲和性学习笔记
欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 本文是欣宸在学习Kubernetes调度器的 ...
- 大白话讲讲 Go 语言的 sync.Map(二)
上一篇文章 <大白话讲讲 Go 语言的 sync.Map(一)> 讲到 entry 数据结构,原因是 Go 语言标准库的 map 不是线程安全的,通过加一层抽象回避这个问题. 当一个 ke ...
- Linux 软件包:添加repo、升级内核、编译内核、交叉编译
添加 repo 增加 xxx.repo 文件 在/etc/yum.repos.d/目录下创建 add_openeuler_repo.repo 文件 [add_repo] name=add_repo b ...
- <学习笔记> 关于二项式反演
1 容斥原理的式子: \[|A1∪A2∪...∪An|=\sum_{1≤i≤n}|Ai|−\sum_{1≤i<j≤n}|Ai∩Aj|+...+(−1)^{n−1}×|A1∩A2∩...∩An| ...