题目链接:Codeforces 557D Vitaly and Cycle
After Vitaly was expelled from the university, he became interested in the graph theory.
Vitaly especially liked the cycles of an odd length in which each vertex occurs at most once.
Vitaly was wondering how to solve the following problem. You are given an undirected graph consisting of n vertices and m edges, not necessarily connected, without parallel edges and loops. You need to find t — the minimum number of edges that must be added to the given graph in order to form a simple cycle of an odd length, consisting of more than one vertex. Moreover, he must find w — the number of ways to add t edges in order to form a cycle of an odd length (consisting of more than one vertex). It is prohibited to add loops or parallel edges.
Two ways to add edges to the graph are considered equal if they have the same sets of added edges.
Since Vitaly does not study at the university, he asked you to help him with this task.
The first line of the input contains two integers n and m ( — the number of vertices in the graph and the number of edges in the graph.
Next m lines contain the descriptions of the edges of the graph, one edge per line. Each edge is given by a pair of integers ai, bi(1 ≤ ai, bi ≤ n) — the vertices that are connected by the i-th edge. All numbers in the lines are separated by a single space.
It is guaranteed that the given graph doesn't contain any loops and parallel edges. The graph isn't necessarily connected.
Print in the first line of the output two space-separated integers t and w — the minimum number of edges that should be added to the graph to form a simple cycle of an odd length consisting of more than one vertex where each vertex occurs at most once, and the number of ways to do this.
4 4 1 2 1 3 4 2 4 3
1 2
3 3 1 2 2 3 3 1
0 1
3 0
3 1
The simple cycle is a cycle that doesn't contain any vertex twice.
题意:给定一个无向图,问你最少增加多少条边可以得到一个奇圈。问方案数。
思路:考虑奇圈的判定,如果连通图不是一个二分图,必定存在奇圈。这是一个充要条件。
首先比较坑的地方有两个:
1、无边时,需要连3条边,方案数为n * (n-1) * (n-2) / 6。
2、不存在节点数目大于2的连通分支时,需要连2条边,方案数为(n-2) * m。
其他情况,我们先判断每一个连通分支,有一个不是二分图,那么一定存在奇圈。反之,我们黑白染色每个连通分支里面的节点,对于任意的两个黑点,总可以得到一个奇圈,同理白点。那么我们就统计每个连通分支里面黑点数目cnt1[]、白点数目cnt2[],对整体贡献是cnt1[i] * (cnt1[i] - 1) / 2 + cnt2[i] * (cnt2[i] - 1) / 2。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5+5;
vector<int> G[maxn];
bool vis[maxn];
int color[maxn], cnt1[maxn], cnt2[maxn], num[maxn], pos[maxn];
bool judge(int u, int id)
{
if(!vis[u]) num[id]++;
pos[u] = id;vis[u] = true;
for(int i = 0; i < G[u].size(); i++)
{
int v = G[u][i];
if(color[u] == color[v]) return false;
if(!color[v])
{
color[v] = 3 - color[u];
if(!judge(v, id)) return false;
}
}
return true;
}
int main()
{
int n, m;
while(scanf("%d%d", &n, &m) != EOF)
{
if(m == 0)
{
printf("3 %I64d\n", 1LL*n*(n-1)*(n-2)/6);
continue;
}
for(int i = 1; i <= n; i++)
{
vis[i] = false;G[i].clear();
cnt1[i] = cnt2[i] = num[i] = color[i] = 0;
}
for(int i = 0; i < m; i++)
{
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
bool flag = false;
int top = 1;
for(int i = 1; i <= n; i++)
{
if(vis[i]) continue;
color[i] = 1;
if(!judge(i, top))
{
flag = true;
break;
}
top++;
}
if(flag)
{
printf("0 1\n");
continue;
}
for(int i = 1; i <= n; i++)
{
if(color[i] == 1)
cnt1[pos[i]]++;
else
cnt2[pos[i]]++;
}
ll ans = 0;
for(int i = 1; i < top; i++)
{
if(num[i] <= 2) continue;
ans += 1LL * cnt1[i] * (cnt1[i]-1) / 2 + 1LL * cnt2[i] * (cnt2[i]-1) / 2;
}
if(ans == 0)
printf("2 %I64d\n", 1LL * (n-2) * m);
else
printf("1 %I64d\n", ans);
}
return 0;
}