您的位置:首页 > 其它

codeforces 557D Vitaly and Cycle

2015-07-19 17:55 417 查看
新技能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

/*Author :usedrose  */
/*Created Time :2015/7/19 17:01:15*/
/*File Name :2.cpp*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <vector>
#include <string>
#include <ctime>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
#define MAXN 100110
#define OK cout << "ok" << endl;
#define o(a) cout << #a << " = " << a << endl
#define o1(a,b) cout << #a << " = " << a << "  " << #b << " = " << b << endl
using namespace std;
typedef long long LL;

int n, m;
bool marked[MAXN], ok;
int  sz[2];
int col[MAXN];
vector<int > G[MAXN];
LL ans;

void dfs(int v, int c)
{
marked[v] = true;
ans += sz[c-1];
sz[c-1]++;
col[v] = c;
for (int i = 0; i < G[v].size(); i++)
{
int u = G[v][i];
if (!marked[u])
{
col[u] = 3 - c;
dfs(u, 3 - c);
}
else if (col[u] == col[v])
ok = true;
}
}
int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n >> m;
int a, b;

for (int i = 0;i < m; ++ i) {
cin >> a >> b;
G[a].push_back(b);
G[b].push_back(a);
}
for (int i = 1; i <= n; ++ i)
if (!marked[i]) {
sz[0] = sz[1] = 0;
dfs(i, 1);
}
if (ok)
cout << 0 << " " << 1 << endl;
else if (ans == 0)
{
if (m == 0)
{
long long ans = n;
ans *= n - 1;
ans *= n - 2;
ans /= 6;
cout << 3 << " " << ans << endl;
}
else
cout << 2 << " " << (long long)m * (n - 2) << endl;

}
else
cout << 1 << " " << ans << endl;

return 0;
}


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/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: