Codeforces-----233C---Cycles模拟

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

John Doe started thinking about graphs. After some thought he decided that he wants to paint an undirected graph, containing exactlyk cycles of length 3.

A cycle of length 3 is an unordered group of three distinct graph verticesa, b andc, such that each pair of them is connected by a graph edge.

John has been painting for long, but he has not been a success. Help him find such graph. Note that the number of vertices there shouldn't exceed100, or else John will have problems painting it.

Input

A single line contains an integer k (1 ≤ k ≤ 105) — the number of cycles of length3 in the required graph.

Output

In the first line print integer n (3 ≤ n ≤ 100) — the number of vertices in the found graph. In each of nextn lines print n characters "0" and "1": thei-th character of the j-th line should equal "0", if verticesi and j do not have an edge between them, otherwise it should equal "1". Note that as the required graph is undirected, thei-th character of the j-th line must equal the j-th character of thei-th line. The graph shouldn't contain self-loops, so thei-th character of the i-th line must equal "0" for alli.

Examples
Input
1
Output
3
011
101
110
Input
10
Output
5
01111
10111
11011
11101
11110
一幅图中的点,问能构成n个三元环的点的关系

依次模拟加入边的关系(即是否两点相连),判断加入此关系之后所增加的三元环数目是否合适,再决定是否加入

pos[i][j] = 1,代表i,j两点相连,即有边

判断方法是查找当前存在多少对与当前两顶点相连的边

如:要判断4与5是否相连,需统计1,4和1,5是否同时相连,若相连,连接4,5后,三元环数目+1,

再统计2,4和2,5……

……

类推即可

显然如果有n对与当前两顶点(即当前边)同时相连的两边对数,则当前两顶点相连可以多形成n个三元环

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<set> 
#include<queue>
#include<vector>
#include<functional>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define CL(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long LL;
const int maxn = 1e3+10;
const int MOD = 1e9+7; 
int pos[105][105];
int main(){ 
    int n, t, i, kcase = 1;
    scanf("%d", &n);
    pos[1][2] = pos[2][1] = 1;
    for(i = 3; i <= 100; i++){
    	for(int j = 1; j < i; j++){
    		int c = 0;
    		for(int k = 1; k < j; k++){//c统计与当前边同时相连的两边对数,即三元环数目
    			if(pos[k][j] && pos[k][i]) c++;
			}
			if(c <= n){//若添加此边后,增加的三元环不超过要求,更新
				n -= c;
				pos[i][j] = pos[j][i] = 1;
			}
			if(!n) break;
		}
		if(!n) break;
	}
	printf("%d\n", i);
	for(int m = 1; m <= i; m++){
		for(int n = 1; n <= i; n++) printf("%d", pos[m][n]);
		printf("\n");
	}
    return 0;  
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值