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

來源:互聯網
上載者:User

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_back22 #define mp make_pair23 #define fi first24 #define se second25 #define lowbit(x) (x&(-x))26 #define INF (1<<30)27 28 int maz[105][105];29 int main()30 {31     #ifndef ONLINE_JUDGE32     freopen("in","r",stdin);33     #endif34     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 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.