Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined)

來源:互聯網
上載者:User

標籤:++   can   bar   set   push   out   lin   image   one   

  A題,水題,判斷所有點是不是入度和出度都相同即可。

  B題, 題意搞懂了就是水題。

  C題。一方分數mod k以後有餘數,那麼另外一方至少需要贏一場。根據這個方法判斷即可。

  D題,構造題,不會= =。

  E題,題意是每次可以選一個節點,把兒子的兩條相同長度的鏈合并成新的一條相同長度的鏈。問最後是否可以成為一條直鏈,如果可以輸出最短的長度。見代碼即可:

 1 #include <stdio.h> 2 #include <algorithm> 3 #include <string.h> 4 #include <set> 5 #include <iostream> 6 #include <vector> 7 using namespace std; 8 const int N = 2e5 + 5; 9 10 int n,root;11 vector<int> G[N];12 int dfs(int u,int fa)13 {14     set<int> S;15     for(int i=0;i<G[u].size();i++)16     {17         int v = G[u][i];18         if(v == fa) continue;19         int t = dfs(v,u);20         if(t == -1) return -1;21         S.insert(t + 1);22     }23     if(S.size() == 0) return 0;24     if(S.size() == 1) return *S.begin();25     if(S.size() == 2 && fa == -1) return *S.begin() + *S.rbegin();26     root = u;27     return -1;28 }29 30 int main()31 {32     cin >> n;33     for(int i=1;i<n;i++)34     {35         int u,v; scanf("%d%d",&u,&v);36         G[u].push_back(v);37         G[v].push_back(u);38     }39     int ans = dfs(1,-1);40     if(ans == -1) ans = dfs(root,-1);41     if(ans == -1) return 0*puts("-1");42     while(ans % 2 == 0) ans >>= 1;43     printf("%d\n",ans);44     return 0;45 }
E

 

Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.