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 ...
随机推荐
- 最近很火的开源培训系统,支持免费商用,3个月1000star!
项目简介 PlayEdu 开源培训系统自发布以来,3个月内帮助上千位开发者部署了私有化培训平台,并在 Github 上获得了1000star. 项目地址 Github 地址:https://githu ...
- Centos7快速安装Oracl11g
Centos7快速安装Oracle11g 一.解决虚拟机或低配置的云服务器上安装Oracle的方法有两种: 1)不用图形界面,采用静默方式安装,这种方法的技术难度比较大,Oracle的DBA经常采用这 ...
- Redis理论
什么是Redis Redis(Remote Dictionary Server)是使用C语言编写的,开源的(BSD许可)高性能非关系型(NoSQL)的键值对数据库. Redis可以存储键和五种不同类型 ...
- Windows商店开发者注册失败
前言 最近写了个小工具想上架Windows应用商店,但是在填写信息那一页总是失败,提示Error code 2201. Correlation ID 9d436e3a-94df-498a-b224-8 ...
- EchoMode的显示效果
1 import sys 2 from PyQt5.QtWidgets import QApplication, QLineEdit, QWidget, QFormLayout 3 4 class l ...
- 一文搞懂TCP的三次握手和四次挥手
目录 1.三次握手 2.四次挥手 3.11种状态名词解析 TCP的三次握手和四次挥手实质就是TCP通信的连接和断开. 三次握手:为了对每次发送的数据量进行跟踪与协商,确保数据段的发送和接收同步,根据所 ...
- 记一次因为C#官方扩展导致自动补全出错的情况 (C# & Godot)
现象 最近使用Vscode结合Godot使用时突然发现自动补全出问题了,发现一部分自动补全能弹出补全项目,但是确认后不起作用,还会吞掉弹出自动补全后输入的字符.大概是下图这样的感觉(截图时已修好,图为 ...
- [oracle]拆分多用户的公共表空间
前言 开发环境之前多个用户共用一个表空间,后期维护比较麻烦,因此需要将这些用户拆出来,一个用户一个表空间,以后清理这些用户也更方便. 大致思路:假设A.B.C用户共用一个表空间,将A.B.C的用户数据 ...
- 云原生可观测框架 OpenTelemetry 基础知识(架构/分布式追踪/指标/日志/采样/收集器)
什么是 OpenTelemetry? OpenTelemetry 是一个开源的可观测性框架,由云原生基金会(CNCF)托管.它是 OpenCensus 和 OpenTracing 项目的合并.旨在为所 ...
- css美化
编辑网页文本 span标签:能让某几个字凸显出来结构:span{color:red:} <span>123<span> 字体样式:一般设置两个字体.如果浏览器第一个字体不 ...