【34.57%】【codeforces 557D】Vitaly and Cycle
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
Input
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.
Output
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.
Examples
input
4 4
1 2
1 3
4 2
4 3
output
1 2
input
3 3
1 2
2 3
3 1
output
0 1
input
3 0
output
3 1
Note
The simple cycle is a cycle that doesn’t contain any vertex twice.
【题目链接】:http://codeforces.com/contest/557/problem/D
【题解】
要存在一个奇数环。
则最多就添加3条边(3条边一定能构成一个环!)。
1.看看整张图变成了几个连通块,如果每个连通块里面的点的个数都为1,则添加边数为3,方案数为C(n,3)=n*(n-1)*(n-2)/6,这个时候对应的情况是边数m=0;->”3 C(N,3)”
2.每个连通块里面的点的个数的最大值为2;则连通块里面点的个数为2的情况就对应这个连通块里面只有一条边,而一条边由两个点构成,这条边上的两个点分别与其余n-2个点构成n-2个环(都是3个点的环),边的个数m就对应了连通块里面点的个数为2的情况,则方案为m*(n-2);->”2 m*(n-2)”
下面这种情况不是奇环(而是偶环),所以”2对2的情况可以排除”;
3.除了以上两种情况外;
如果在某个连通块里面不能进行二分图染色->则存在奇环。直接输出”0 1”;
( 有奇环就不能完成二分图染色);
如果都能进行二分图染色;
则记录每个连通块里面白点(0)和黑点(1)的个数;
设为cnt[0]和cnt[1];
则每有一个联通块;
答案递增C(cnt[0],2)+C(cnt[1],2);
可以看到每两个0之间连一条边都能构成一个奇数环;
很有趣的性质.
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)
#define pri(x) printf("%d",x)
#define prl(x) printf("%I64d",x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int MAXN = 1e5+10;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
int n,m;
int f[MAXN],num[MAXN],cnt[2];
int color[MAXN];
vector <int> g[MAXN];
queue <int> dl;
int ff(int x)
{
if (f[x]==x) return x;
else
f[x] = ff(f[x]);
return f[x];
}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
memset(color,255,sizeof color);
rei(n);rei(m);
rep1(i,1,n)
f[i] = i,num[i] = 1;
rep1(i,1,m)
{
int x,y;
rei(x);rei(y);
g[x].pb(y);
g[y].pb(x);
int r1 = ff(x),r2 = ff(y);
if (r1!=r2)
{
f[r1]=r2;
num[r2]+=num[r1];
}
}
int ma = 1;
LL ans = 0;
rep1(i,1,n)
{
int r = ff(i);
ma = max(ma,num[r]);
}
if (ma == 1)
{
printf("3 %I64d\n",1LL*n*(n-1)*(n-2)/6);
return 0;
}
else
if (ma==2)
{
printf("2 %I64d\n",1LL*(n-2)*m);
return 0;
}
else
{
rep1(i,1,n)
if (color[i]==-1)
{
memset(cnt,0,sizeof cnt);
color[i] = 0;
cnt[0] = 1;
dl.push(i);
bool ok = true;
while (!dl.empty())
{
int x = dl.front();
dl.pop();
int len = g[x].size();
rep1(j,0,len-1)
{
int y = g[x][j];
if (y==x) continue;
if (color[y]==-1)
{
color[y] = 1-color[x];
cnt[color[y]]++;
dl.push(y);
}
else
if (color[y]==color[x])
{
ok = false;
break;
}
}
if (!ok) break;
}
if (!ok)
{
printf("0 1\n");
return 0;
}
if (cnt[0]>=2)
ans+=1LL*cnt[0]*(cnt[0]-1)/2;
if (cnt[1]>=2)
ans+=1LL*cnt[1]*(cnt[1]-1)/2;
}
}
cout <<"1 "<< ans << endl;
return 0;
}
【34.57%】【codeforces 557D】Vitaly and Cycle的更多相关文章
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- codeforces 557 D. Vitaly and Cycle 组合数学 + 判断二分图
D. Vitaly and Cycle time limit per test 1 second memory limit per test 256 megabytes input sta ...
- 【57.97%】【codeforces Round #380A】Interview with Oleg
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【24.34%】【codeforces 560D】Equivalent Strings
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【34.88%】【codeforces 569C】Primes or Palindromes?
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- 【codeforces 793D】Presents in Bankopolis
[题目链接]:http://codeforces.com/contest/793/problem/D [题意] 给你n个点, 这n个点 从左到右1..n依序排; 然后给你m条有向边; 然后让你从中选出 ...
- 【codeforces 799D】Field expansion
[题目链接]:http://codeforces.com/contest/799/problem/D [题意] 给你长方形的两条边h,w; 你每次可以从n个数字中选出一个数字x; 然后把h或w乘上x; ...
- 【codeforces 750C】New Year and Rating(做法2)
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
随机推荐
- HTML中行内元素与块级元素有哪些及区别
二.行内元素与块级元素有什么不同? 块级元素和行内元素的区别是,块级元素会占一行显示,而行内元素可以在一行并排显示. 通过样式控制,它们可以相互转换. 1.尺寸-块级元素和行内元素之间的一个重要的不同 ...
- UVA 10306 e-Coins(全然背包: 二维限制条件)
UVA 10306 e-Coins(全然背包: 二维限制条件) option=com_onlinejudge&Itemid=8&page=show_problem&proble ...
- Android开发之经常使用开源库直接拿来用
1.from 代码家 整理比較好的源代码连接 **************************************************************************** ...
- oracle跨数据库跨用户訪问注意事项
java代码中不同意出现oracle的username.数据链路名. 跨用户.跨数据库的訪问必须在oracle中建同义词或视图来实现.在java代码中仅仅需当做当前用户下的对象处理.
- Autodesk 招聘Revit二次开发咨询顾问,与Autodesk全球团队紧密合作,提高职业生涯的好机会
朋友们, 因为我离开Autodesk的全职工作(变为部分时间工作),我的职位空出.急招这个职位.请踊跃把你周围的朋友推荐给Autodesk. 请将简历发给我转交给Autodesk 我的邮箱yexion ...
- BZOJ4182: Shopping(点分治,树上背包)
Description 马上就是小苗的生日了,为了给小苗准备礼物,小葱兴冲冲地来到了商店街.商店街有n个商店,并且它们之间的道路构成了一颗树的形状. 第i个商店只卖第i种物品,小苗对于这种物品的喜爱度 ...
- Linux启动过程总结
当我们按开机键后,主机就会执行: 1.POST(Power-On Self Test 加电自检). 2.读取BIOS中定义的开机设备启动程序,并加载MBR(主引导记录(Master Boot Reco ...
- Maven搭建hadoop环境报Missing artifact jdk.tools:jdk.tools:jar:1.7
今天,更新了工程,报错了. 项目中用了HBase,也有Hadoop相关的jar配置. pom文件, Missing artifact jdk.tools:jdk.tools:jar:1.7 Maven ...
- SpringMVC 传递相同名称的参数的最佳方法
华为云4核8G,高性能云服务器,免费试用 >>> SpringMVC 多个对象的相同字段参数传递解决方案,在SpringMVC中,有时需要传递多个对象(除了Model和web元素 ...
- HTML基础第八讲---序列卷标
转自:https://i.cnblogs.com/posts?categoryid=1121494 什么叫做序列卷标?其实,这是一个大家都蛮熟悉的东西,只是在网页中换个名称来称呼罢了,序列卷标的功能在 ...