Codeforces Round #144 (Div. 2) C. Cycles

http://codeforces.com/contest/233/problem/C

题意:求一个具有k个三元环的无向图。

分析:先一个i个节点的无向完全图,其中C(i,3)<=k,剩下k-C(i,3)个三元环未构成,再加j条边(C(j,2)<=未构成的环)直到满足条件。

View Code
 1 /*
 2 Author:Zhaofa Fang
 3 Lang:C++
 4 */
 5 #include <cstdio>
 6 #include <cstdlib>
 7 #include <iostream>
 8 #include <cmath>
 9 #include <cstring>
10 #include <algorithm>
11 #include <string>
12 #include <utility>
13 #include <vector>
14 #include <queue>
15 #include <stack>
16 #include <map>
17 #include <set>
18 using namespace std;
19 typedef long long ll;
20 #define pii pair<int,int>
21 #define pb push_back
22 #define mp make_pair
23 #define fi first
24 #define se second
25 #define lowbit(x) (x&(-x))
26 #define INF (1<<30)
27 
28 int maz[105][105];
29 int main()
30 {
31     #ifndef ONLINE_JUDGE
32     freopen("in","r",stdin);
33     #endif
34     int K;
35     while(cin>>K)
36     {
37         memset(maz,0,sizeof(maz));
38         int i;
39         for(i=3;i*(i-1)*(i-2)/6<=K;i++);
40         i--;
41         K -= i*(i-1)*(i-2)/6;
42         for(int j=0;j<i;j++)
43             for(int k=0;k<i;k++)
44                 maz[j][k] = (j != k);
45         int n=i;
46         while(K)
47         {
48             int j;
49             for(j=2;j*(j-1)/2<=K;j++);
50             j--;
51             for(int k=0;k<j;k++)maz[n][k] = maz[k][n] = 1;
52             K -= (j-1)*j/2;
53             n++;
54         }
55         cout<<n<<endl;
56         for(int x=0;x<n;x++)
57         {
58             for(int y=0;y<n;y++)
59             printf("%d",maz[x][y]);
60             puts("");
61         }
62     }
63     return 0;
64 }
posted @ 2012-10-15 13:25  發_  阅读(156)  评论(0编辑  收藏  举报