Codeforces 557D - Vitaly and Cycle (DFS染色)
来源:互联网 发布:淘宝货架大全 编辑:程序博客网 时间:2024/05/17 22:24
很容易想到最多只需要添加3条边。
1.如果M==0,那么任选三个点连接3条边
2.如果所有连通分量大小均小于等于2,那么任选一个连通分量和一个点连2条边
3.如果本身存在奇圈(DFS过程中父节点和子节点颜色一样),那么答案是0 1
4.除此以外所有情况均可以只连一条边。
把图在DFS的过程中按照一黑一白染色,那么同一连通分量中的相同颜色的点连线均可构成奇圈。所以DFS时维护一下黑白点的数量即可。
注意,不同连通分量间的点颜色相同也不能连边。
代码:
#include <iostream>#include <cstring>#include <cstdio>#include <vector>using namespace std;#define maxn 100005#define maxm 100005#define INF 100000000#define LL long longbool vis[maxn];int N,M;vector <int> G[maxn];int odd,even;bool loop=0;bool bin=0;int ps[maxn];void dfs(int n,int cur){ if(G[n].size()>=2) bin=1; if(cur&1){ odd++; ps[n]=1;} else { even++; ps[n]=0;} for(int i=0;i<G[n].size();i++){ int nw=G[n][i]; if(!vis[nw]){ vis[nw]=1; dfs(nw,cur+1); } if(ps[nw]==ps[n]) loop=1; }}int main(){ scanf("%d%d",&N,&M); odd=even=0; for(int i=0;i<M;i++){ int s,t; scanf("%d%d",&s,&t); G[s].push_back(t); G[t].push_back(s); } if(M==0){ LL res=1ll*N*(N-1)*(N-2)/6; printf("3 %lld\n",res); return 0; } LL res=0; for(int i=1;i<=N;i++){ if(!vis[i]){ vis[i]=1; odd=even=0; dfs(i,0); res+=1ll*odd*(odd-1)/2+1ll*even*(even-1)/2; } } if(loop){ printf("0 1\n"); return 0; } if(!bin){ printf("2 %lld\n",1ll*M*(N-2)); return 0; } printf("1 %lld\n",res); return 0;}
0 0
- Codeforces 557D - Vitaly and Cycle (DFS染色)
- Codeforces-557D Vitaly and Cycle(二分图染色)
- Codeforces 557 D. Vitaly and Cycle
- 【34.57%】【codeforces 557D】Vitaly and Cycle
- Codeforces 557D Vitaly and Cycle 【染色判二分图 + 组合数学】
- Codeforces 557D Vitaly and Cycle【二分图染色+思维】好题~
- CodeForces 557D Vitaly and Cycle(二分图)
- codeforces #311 557D D. Vitaly and Cycle (dfs+图论判断判断构造奇环)
- codeforces 557D Vitaly and Cyclef(二分图染色)
- cf #311 D. Vitaly and Cycle (二分图染色)
- 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 Round #311 (Div. 2) D. Vitaly and Cycle 图论 二分图
- Codeforces Round #311 (Div. 2) D. Vitaly and Cycle 图论 二分图
- codeforces-776D-The Door Problem(染色+建边+dfs)
- Codeforces 263D Cycle in Graph【Dfs】
- qsort函数以及sort函数用法
- Hduoj1016【深搜】【水题】
- UVa 11582 - Colossal Fibonacci Numbers!(取模+周期)
- 实例:Cocos2d-js中使用纹理对象创建Sprite对象
- 天声人語 20150727
- Codeforces 557D - Vitaly and Cycle (DFS染色)
- Android控件开发之DatePicker
- Do not warn about base configuration if unnecessary
- HTTP协议详解
- 面试中如何谈自己做过的项目
- [HDU 1712] ACboy needs your help 分组背包
- C#知识点总结系列:1、C#中Hashtable、Dictionary详解以及写入和读取对比
- CodeForces 363A - Soroban
- hdu1576(乘法逆元)