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 题解的更多相关文章

  1. 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 ...

  2. codeforces 389 D. Fox and Minimal path(构造+思维)

    题目链接:https://vjudge.net/contest/175446#problem/J 题解:显然要用最多n个点构成的图要使的得到的最短路条数有1e9次个,显然要有几个数相乘容易想到2的几进 ...

  3. codeforces 388B Fox and Minimal path

    这个题目的突破口就是固定最短长度,然后以二进制的形式分层: 最后把需要的曾连起来: #include<cstdio> #include<cstring> #define max ...

  4. CF #228 div1 B. Fox and Minimal path

    题目链接:http://codeforces.com/problemset/problem/388/B 大意是用不超过1000个点构造一张边权为1的无向图,使得点1到点2的最短路的个数为给定值k,其中 ...

  5. Codeforces Round #228 (Div. 1) 388B Fox and Minimal path

    链接:http://codeforces.com/problemset/problem/388/B [题意] 给出一个整数K,构造出刚好含有K条从1到2的最短路的图. [分析] 由于是要自己构造图,当 ...

  6. 【字典树】【树】【二进制】bzoj1954/POJ3764The xor-longest Path 题解

        建立字典树是异或的一种处理方法. Description In an edge-weighted tree, the xor-length of a path p is defined as ...

  7. 洛谷 UVA12101 Prime Path 题解

    一道经典的BFS 用四个for搜索四位就行了,只要能推出怎么只变4位中的一位就很水了 #include<iostream> #include<cstring> #include ...

  8. CF1327D Infinite Path 题解

    原题链接 太坑了我谔谔 简要题意: 求一个排列的多少次幂能达到另一个排列.排列的幂定义见题.(其实不是新定义的,本来就是这么乘的) 很显然,这不像快速幂那样可以结合律. 既然这样,就从图入手. 将 \ ...

  9. 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 ...

  10. Codeforces389D(SummerTrainingDay01-J)

    D. Fox and Minimal path time limit per test:1 second memory limit per test:256 megabytes input:stand ...

随机推荐

  1. 微调用于多语言 ASR 的 MMS 适配器模型

    新内容 (06/2023): 这篇博文受到 "在多语言 ASR 上微调 XLS-R" 的强烈启发,可以看作是它的改进版本. Wav2Vec2 是自动语音识别 (ASR) 的预训练模 ...

  2. 微信小程序常用的view、text、button、image组件

    [黑马程序员前端微信小程序开发教程,微信小程序从基础到发布全流程_企业级商城实战(含uni-app项目多端部署)] https://www.bilibili.com/video/BV1834y1676 ...

  3. Tomcat启动时出现乱码的解决方式

    在网上下载了一个版本号为apache-tomcat-8.5.38的Tomcat,因为这个Tomcat一直没有用过,所以今天启动时出现了如下乱码: 解决方案: 找到Tomcat目录下conf文件夹中的l ...

  4. ARM Trusted Firmware——编译选项(二)

    @ 目录 1. 常用部分 2. 安全相关 2.1 签名 2.2 加密 2.3 哈希 2.4 中断 3.GICv3驱动程序选项 4. 调试选项 1. 常用部分 编译选项 解释 BL2 指定生成fip文件 ...

  5. 【译】Visual Studio 2022 中的 Web API 开发

    在 Visual Studio 2022 中,Web 开发人员的主要场景之一是使用 ASP.NET Core 创建 Web API.在 Visual Studio 2022 17.6 的最新预览版中, ...

  6. Magick.NET跨平台压缩图片的用法

    //首先NuGet安装:Magick.NET.Core,Magick.NET-Q16-AnyCPUusing ImageMagick; /// <summary> /// 压缩图片 /// ...

  7. VSCode插件:自动生成注释——koroFileHeader

    配置 用户设置打开 settings.json 添加如下代码: "fileheader.cursorMode": { }, "fileheader.customMade& ...

  8. Ubuntu新建Django工程错误:ModuleNotFoundError: No module named 'distutils.core'

    ubuntu18.04 默认没有安装 pip ,需要安装 python3-pip,即可解决 1 sudo apt-get install python3-pip 在shell脚本中输入以下命令: 1 ...

  9. mysql 命令安装

    1.   mysql  下载安装好压缩文件,下面我们进入正题,少废话. 09:39:112023-08-05 先到 mysql 官方网站下载:https://dev.mysql.com/downloa ...

  10. windows配置supervisor实现nginx自启

    前言 有些老项目的nginx部署在windows server上,而且服务器比较老旧,经常异常重启.鉴于个人并不熟悉windows server,因此配置supervisor自启nginx,实现win ...