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

本文介绍了一种算法,用于寻找一个无向图,该图恰好包含k个长度为3的简单环(三元环)。通过逐步构建图并模拟边的添加过程,确保最终图满足指定条件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;  
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值