Codeforces 388B Fox and Minimal path(构造最短路条数为N的图)
来源:互联网 发布:u盘在mac上文件不在 编辑:程序博客网 时间:2024/05/04 16:06
Fox Ciel wants to write a task for a programming contest. The task is: "You are given a simple undirected graph withn vertexes. Each its edge has unit length. You should calculate the number of shortest paths between vertex 1 and vertex 2."
Same with some writers, she wants to make an example with some certain output: for example, her birthday or the number of her boyfriend. Can you help her to make a test case with answer equal exactly tok?
The first line contains a single integer k (1 ≤ k ≤ 109).
You should output a graph G with n vertexes (2 ≤ n ≤ 1000). There must be exactlyk shortest paths between vertex 1 and vertex 2 of the graph.
The first line must contain an integer n. Then adjacency matrixG with n rows andn columns must follow. Each element of the matrix must be 'N' or 'Y'. IfGij is 'Y', then graphG has a edge connecting vertex i and vertex j. Consider the graph vertexes are numbered from 1 ton.
The graph must be undirected and simple: Gii = 'N' andGij = Gji must hold. And there must be at least one path between vertex 1 and vertex 2. It's guaranteed that the answer exists. If there multiple correct answers, you can output any of them.
2
4NNYYNNYYYYNNYYNN
9
8NNYYYNNNNNNNNYYYYNNNNYYYYNNNNYYYYNNNNYYYNYYYYNNNNYYYYNNNNYYYYNNN
1
2NYYN
In first example, there are 2 shortest paths: 1-3-2 and 1-4-2.
In second example, there are 9 shortest paths: 1-3-6-2, 1-3-7-2, 1-3-8-2, 1-4-6-2, 1-4-7-2, 1-4-8-2, 1-5-6-2, 1-5-7-2, 1-5-8-2.
题目大意:
输入一个数字N,构造一个最短路径数为N的图,以邻接矩阵的形式输出。
解题思路:
开始想的是因数分解,不过这样对于非常大的质数会超过1000个点的限制。后来在吃饭的时候灵光一闪想到的正确的方法( ̄▽ ̄)
任何一个数都可以用2进制表示,而且1e9也才二十几位而已。所以我们就可以把N拆成2^a+2^b+2^c…。于是我们就可以构造一条主链,表示主要路径,一条辅链,表示2^x。如图
因为主链已经表示了1,所以我们要把N-=1,然后对N进行转化为二进制数,如果第i位为1就把主链第i+1层和辅链第i层的一个节点连接,相当于增加了2^I条路径。这样就可以得到满足题目要求的图了。
AC代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn=1000+3;int N,size;//需要路径数、图的层数bool G[maxn][maxn];//邻接矩阵void add_edge(int a,int b)//加边{ G[a][b]=G[b][a]=true;}int main(){ scanf("%d",&N); --N; int tmp=N; while(tmp>0) { ++size; tmp/=2; } if(size==0)//N==1时特判 { printf("2\nNY\nYN\n"); return 0; } //构造主链和辅链 add_edge(1,3); add_edge(1,4); add_edge(1,5); add_edge(size*3,2); for(int i=1;i<size;++i) { add_edge(i*3,(i+1)*3); add_edge(i*3+1,(i+1)*3+1); add_edge(i*3+2,(i+1)*3+2); add_edge(i*3+1,(i+1)*3+2); add_edge(i*3+2,(i+1)*3+1); } int now=0; while(N>0) { ++now; if(N&1)//增加2^now条路径 { if(now==size) add_edge(2,3*now+1); else add_edge(3*(now+1),3*now+1); } N/=2; } size=size*3+2; printf("%d\n",size); for(int i=1;i<=size;++i)//输出邻接矩阵 { for(int j=1;j<=size;++j) putchar(G[i][j]?'Y':'N'); putchar('\n'); } return 0;}
- Codeforces 388B Fox and Minimal path(构造最短路条数为N的图)
- Codeforces 388B Fox and Minimal path(构造最短路条数为N的图)
- Codeforces 388B Fox and Minimal path(构造)
- Codeforces 388B Fox and Minimal path(构造)
- codeforces 388B Fox and Minimal path
- CodeForces 388B Fox and Minimal path
- Codeforces B. Fox and Minimal path
- Codeforces 388B Fox and Minimal path 构图
- Codeforces Round #228 (Div. 1)-B-Fox and Minimal path
- Codeforces Round #228 Div1 B Fox and Minimal path
- Codeforces Round #228 (Div. 1) B. Fox and Minimal path
- Codeforces 389D Fox and Minimal path【构造+二进制思维】好题!
- Codeforces 389D Fox and Minimal path【构造+二进制思维】好题!
- cf 389D Fox and Minimal path 【构造】
- Codeforces Round #228 (Div. 2) D - Fox and Minimal path
- Codeforces #228 D2D / D1B:Fox and Minimal path
- CF 好题集锦 PART 0 & #388 B Fox and Minimal path 题解
- Codeforces 449B Jzzhu and Cities(最短路)
- iOS架构:MVVM+RAC
- ReactNative官网例子练习——(二)
- 怎么处理微信web页面字体自动变大
- Scrollview 嵌套 listview 无法滚动到顶部解决方案
- 数据库基础知识1
- Codeforces 388B Fox and Minimal path(构造最短路条数为N的图)
- Struts2学习笔记六(命名空间、文件上传、文件下载)
- 软件测试术语
- GWT —文件上传(导入)
- 利用matlab中的printf函数和fopen函数debug
- 证书格式区别 & pvk & spc & cer
- C# Func的用法
- 初探+load和+initialize方法
- Double-click me not: Malicious proxy settings in OLE Embedded Script