John Doe started thinking about graphs. After some thought he decided that he wants to paint an undirected graph, containing exactly
KCycles of length
3.
A cycle of length 3 is an unordered group of three distinct graph vertices
A,BAnd
C, 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 exceed
100, or else John will have problems painting it.
Input
A single line contains an integerK(1 digit ≤ DigitKLimit ≤ limit 105)-the number of cycles of length
3 in the required graph.
Output
In the first line print integerN(3 cores ≤ CoresNLimit ≤ limit 100)-the number of vertices in the found graph. In each of next
NLines printNCharacters "0" and "1":
I-Th character of
J-Th line shoshould equal "0", if vertices
IAndJDo not have an edge between them, otherwise it shocould equal "1". Note that as the required graph is undirected,
I-Th character of
J-Th line must equalJ-Th character of
I-Th line. The graph shouldn't contain self-loops, so
I-Th character of
I-Th line must equal "0" for all
I.
Sample test (s) Input
1
Output
3011101110
Input
10
Output
50111110111110111110111110
Question:
It is known that three sides in the graph can form a triangle. K triangles (k <10 ^ 5) are given to calculate the adjacent matrix of the graph. Number of nodes (n) <= 100;
Solution:
First, use the permutation and combination method to determine the minimum full graph. After calculation, each time a vertex is added, the two vertices in the full graph form a new triangle.
In this way, the image is greedy.
#include<cstdio>#include<cstring>#include<cstdlib>using namespace std;int main(){ int f[111][111]; memset(f,0,sizeof(f)); f[0][0]=f[1][0]=f[1][1]=f[2][2]=f[2][0]=1; f[2][1]=2; for( int i=3;i<=100;i++ ) { for( int j=0;j<=3;j++ ) f[i][j]=f[i-1][j-1]+f[i-1][j]; } int n; while( scanf("%d",&n)!=EOF ) { int i,j,ans,m[111][111]; memset( m,0,sizeof(m) ); for( i=2;n>=f[i+1][3];i++ ) ; n-=f[i][3]; //printf("n=%d %d\n",n,i); ans=i; for( i=1;i<=ans;i++ ) for( j=1;j<=ans;j++ ) m[i][j]=i==j?0:1; while( n ) { for( i=2;n>=f[i+1][2];i++ ) ; n-=f[i][2]; ans++; for( j=1;j<=i;j++ ) m[ans][j]=m[j][ans]=1; } printf("%d\n",ans); for( i=1;i<=ans;i++ ) { for( j=1;j<=ans;j++ ) printf("%d",m[i][j]); printf("\n"); } } return 0;}