CodeForces 557D Vitaly and Cycle(二分图)
来源:互联网 发布:java初学者推荐书籍 编辑:程序博客网 时间:2024/05/19 17:59
题目链接:http://codeforces.com/problemset/problem/557/D
题意:给一张无向图,问最少添加多少条边能使该图含有奇环,并给出添加最少边的方案数,该图无重边自环
思路:
1.如果没有边,那么任选三点连边可构成奇环
2.如果已经存在奇环,则无需添加,二分图染色判定即可
3.如果对于该图的所有联通块点数最多为2,那么可加两条边在两个联通块中
4.如果不满足以上三种情况,那只需加一条边在两个相同颜色的点间就行了
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <utility>#include <cmath>#include <queue>#include <set>#include <map>#include <climits>#include <functional>#include <deque>#include <ctime>#define lson l, mid, rt << 1#define rson mid + 1, r, rt << 1 | 1#pragma comment(linker, "/STACK:102400000,102400000")using namespace std;typedef long long ll;const int maxn = 100010;int cnt, head[maxn];struct edge{int to, nxt;} e[maxn << 1];void init(){cnt = 0;memset(head, -1, sizeof(head));}void add(int u, int v){e[cnt].to = v;e[cnt].nxt = head[u];head[u] = cnt++;}int col[maxn], num[3], size;bool check(int u, int c, int fa){size++;col[u] = c;num[col[u]]++;for (int i = head[u]; ~i; i = e[i].nxt){int v = e[i].to;if (v == fa) continue;if (col[v] != -1){if (col[u] == col[v])return false;}else if (!check(v, 1 - c, u))return false;}return true;}int main(){int n, m;while (~scanf("%d%d", &n, &m)){init();ll ans = 0;memset(col, -1, sizeof(col));for (int i = 0; i < m; i++){int u, v;scanf("%d%d", &u, &v);add(u, v);add(v, u);}if (m == 0){ans = (ll)n * (n - 1) * (n - 2) / 6;cout << 3 << " " << ans << endl;continue;}int flag = 0;ll ans1 = 0, ans2 = 0;for (int i = 1; i <= n; i++){memset(num, 0, sizeof(num));if (col[i] == -1){size = 0;if (!check(i, 0, -1)){flag = 1;break;}ll cnt1 = num[0], cnt2 = num[1];//printf("**************%I64d %I64d\n", cnt1, cnt2);if (size == 2)ans2 += n - 2;if (cnt1 >= 2)ans1 += cnt1 * (cnt1 - 1) / 2;if (cnt2 >= 2)ans1 += cnt2 * (cnt2 - 1) / 2;}}if (flag){cout << 0 << " " << 1 << endl;continue;}if (ans1)cout << 1 << " " << ans1 << endl;elsecout << 2 << " " << ans2 << endl;}return 0;}
0 0
- CodeForces 557D Vitaly and Cycle(二分图)
- Codeforces-557D Vitaly and Cycle(二分图染色)
- Codeforces 557D Vitaly and Cycle 【染色判二分图 + 组合数学】
- Codeforces 557D Vitaly and Cycle【二分图染色+思维】好题~
- Codeforces 557 D. Vitaly and Cycle
- 【34.57%】【codeforces 557D】Vitaly and Cycle
- Codeforces 557D - Vitaly and Cycle (DFS染色)
- codeforces 557D Vitaly and Cyclef(二分图染色)
- Codeforces Round #311 (Div. 2) D. Vitaly and Cycle 图论 二分图
- Codeforces Round #311 (Div. 2) D. Vitaly and Cycle 图论 二分图
- cf #311 D. Vitaly and Cycle (二分图染色)
- codeforces #311 557D D. Vitaly and Cycle (dfs+图论判断判断构造奇环)
- cf-557D Vitaly and Cycle
- Codeforces Round #311 (Div. 2) D. Vitaly and Cycle
- Codeforces Round #311 (Div. 2) D. Vitaly and Cycle
- Codeforces557D Vitaly and Cycle 【染色判二分图 + 组合数学】
- CodeForces 557DVitaly and Cycle(二分图求解)
- CodeForces - 518A Vitaly and Strings(水题)
- UIView 坐标转换
- 详解MySQL中EXPLAIN解释命令
- Maven 内置变量
- 无线互联学习第一周
- 超赞!聊聊Web App、Hybrid App与Native App的设计差异
- CodeForces 557D Vitaly and Cycle(二分图)
- Maven实战(一)坐标规划
- php excel文件导出之二 图像导出
- JSON.parse()和JSON.stringify()
- 小孩报数问题3750
- Linux(Red Hat6.4)下安装Oracle 10g 11g经验谈
- codeforces 558 D Guess Your Way Out! II
- OCP-1Z0-051-2015-33题
- js数据类型操作