codeforces 557D Vitaly and Cycle

來源:互聯網
上載者:User

標籤:

新技能get

二分圖染色居然可以用來判斷奇偶環。。

如果能構成二分圖,說明沒有奇環

 

對於任意二分圖,其包含的環一定全部是偶環!(充要可證)

可以證明,含有奇數條邊的環一定有兩個在相同集合內的點有邊相連!

也就是說——二分圖的bfs子樹一定不含奇環!

 

下面討論摘自http://www.cnblogs.com/sagitta/p/4612214.html

 

首先討論添加邊條數為3——即原原圖邊數m為0時

ns=n*(n-1)*(n-2)/6

再討論添加邊條數為2——即原圖中所有邊都沒有公用端點/所有點度數<=1 時

ans=m*(n-2)

再討論添加邊數為0——即原圖中存在奇環時

ans=1

最後討論添加邊數為1——即原圖中只有樹以及偶環

ans=Σ[ (white[i]-1)*white[i]/2+(black[i]-1)*black[i]/2 ]

 

看的matrix大牛代碼

dfs中直接求ans

 1 /*Author :usedrose  */ 2 /*Created Time :2015/7/19 17:01:15*/ 3 /*File Name :2.cpp*/ 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 #include <sstream> 8 #include <cstdlib> 9 #include <cstring>10 #include <climits>11 #include <vector>12 #include <string>13 #include <ctime>14 #include <cmath>15 #include <deque>16 #include <queue>17 #include <stack>18 #include <set>19 #include <map>20 #define INF 0x3f3f3f3f21 #define eps 1e-822 #define pi acos(-1.0)23 #define MAXN 10011024 #define OK cout << "ok" << endl;25 #define o(a) cout << #a << " = " << a << endl26 #define o1(a,b) cout << #a << " = " << a << "  " << #b << " = " << b << endl27 using namespace std;28 typedef long long LL;29 30 int n, m;31 bool marked[MAXN], ok;32 int  sz[2];33 int col[MAXN];34 vector<int > G[MAXN];35 LL ans;36 37 void dfs(int v, int c)38 {39     marked[v] = true;40     ans += sz[c-1];41     sz[c-1]++;42     col[v] = c;43     for (int i = 0; i < G[v].size(); i++)44     {45         int u = G[v][i];46         if (!marked[u])47         {48             col[u] = 3 - c;49             dfs(u, 3 - c);50         }51         else if (col[u] == col[v])52             ok = true;53     }54 }55 int main()56 {57     //freopen("data.in","r",stdin);58     //freopen("data.out","w",stdout);59     cin.tie(0);60     ios::sync_with_stdio(false);61     cin >> n >> m;62     int a, b;63 64     for (int i = 0;i < m; ++ i) {65         cin >> a >> b;66         G[a].push_back(b);67         G[b].push_back(a);68     }69     for (int i = 1; i <= n; ++ i)70         if (!marked[i]) {71             sz[0] = sz[1] = 0;72             dfs(i, 1);73         }74     if (ok)75         cout << 0 << " " << 1 << endl;76     else if (ans == 0)77     {78         if (m == 0)79         {80             long long ans = n;81             ans *= n - 1;82             ans *= n - 2;83             ans /= 6;84             cout << 3 << " " << ans << endl;85         }86         else87             cout << 2 << " " << (long long)m * (n - 2) << endl;88 89     }90     else91         cout << 1 << " " << ans << endl;92     93     return 0;94 }
View Code

 

 

待做。。 

http://www.hardbird.net/hdu-5215-cycle%E4%BA%8C%E5%88%86%E5%9B%BE%E6%9F%93%E8%89%B2%E5%88%A4%E5%A5%87%E5%81%B6%E7%8E%AF/

codeforces 557D Vitaly and Cycle

聯繫我們

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