cf #311 D. Vitaly and Cycle (二分图染色)

题目:http://codeforces.com/contest/557/problem/D

题意:给定没有自环,没有重边的无向图。问最少添加几条边使得存在有奇数个节点的环。并且求出可行的方案数。

分析:

分情况讨论:

①图里面没有边,都是点,那么最少要加3条边。然后然选3个点作为环的顶点。

②图中最长的链的长度为1,那么最少要加2条边。以已知边最为环的一条边,再在剩余的顶点内选一个顶点。

③图中存在链长大于1的链,且不存在奇数个节点的环,那么最少添加1条边。将每个连通分量进行染色,对于每一个连通分量,假设黑色顶点x个,白色顶点y个,那么方案数就是C(x,2)+C(y,2)。

④图中存在奇数个节点的环,那么最少添加0条边,有1种方案。

代码:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
const LL INF = 1E9+9;
const int maxn = 1e6+7;
int n,m;

struct node
{
	int v,next;
}List[maxn];
int head[maxn],cnt;
bool visit[maxn];
void add(int u,int v)
{
	List[cnt].v=v;
	List[cnt].next=head[u];
	head[u]=cnt++;
}
int cs[maxn];
bool hasOddCycle;

void dfs(int cur,int pre,int len)
{
	if(hasOddCycle)
		return ;
	visit[cur]=true;
	for(int i=head[cur];~i;i=List[i].next)
	{
		int to=List[i].v;
		if(to==pre)
			continue ;
		if(visit[to])
		{
			int num=cs[to]+1+len;
		//	cout<<"num:"<<num<<endl;
			if(num&1)
			{
				hasOddCycle=true;
				return ;
			}
			continue ;
		}
		cs[to]=len+1;
		dfs(to,cur,len+1);
	}
}

int c1,c0;
void color(int cur,int cur_color)
{
//	printf("cur: %d\n",cur);
	cur_color?c1++:c0++;
	visit[cur]=true;
	for(int i=head[cur];~i;i=List[i].next)
	{
		int to=List[i].v;
		if(!visit[to])
			color(to,!cur_color);
	}
} 

int d[maxn];
int main()
{
	int i,j,u,v;
	bool Chain=0;
	scanf("%d%d",&n,&m);
	cnt=0;
	memset(head,-1,sizeof(head));
	for(i=1;i<=m;i++)
	{
		scanf("%d%d",&u,&v);
		add(u,v);
		add(v,u);
		d[u]++;
		d[v]++;
		if(d[u]>1 || d[v]>1)
			Chain=true;
	}
	if(m==0)
	{
		printf("3 %lld\n",n*(n-1ll)*(n-2ll)/6ll);
		return 0;
	}
	if(!Chain)
	{
		printf("2 %lld\n",m*(n-2ll));
		return 0;
	}
	for(i=1;i<=n;i++) if(!visit[i])
		dfs(i,-1,0);
	if(hasOddCycle)
	{
		printf("0 1\n");
		return 0;
	}
	LL ans=0;
	memset(visit,0,sizeof(visit));
	for(int i=1;i<=n;i++) if(!visit[i]) 
	{
		c1=c0=0;
		color(i,1);
	//	printf("c1: %d  c0: %d\n",c1,c0);
		ans+=c1*(c1-1ll)/2ll+c0*(c0-1ll)/2ll;
	}
	printf("1 %lld\n",ans);
	return 0;
}


The following is the data that you can add to your input file (as an example). Notice that the first line is going to be a line representing your own hobbies. In my case, it is the Vitaly,table tennis,chess,hacking line. Your goal is to create a class called Student. Every Student will contain a name (String) and an ArrayList<String> storing hobbies. Then, you will add all those students from the file into an ArrayList<Student>, with each Student having a separate name and ArrayList of hobbies. Here is an example file containing students (the first line will always represent yourself). NOTE: eventually, we will have a different file containing all our real names and hobbies so that we could find out with how many people each of us share the same hobby. Vitaly,table tennis,chess,hacking Sean,cooking,guitar,rainbow six Nolan,gym,piano,reading,video games Jack,cooking,swimming,music Ray,piano,video games,volleyball Emily,crochet,drawing,gardening,tuba,violin Hudson,anime,video games,trumpet Matt,piano,Reading,video games,traveling Alex,swimming,video games,saxophone Roman,piano,dancing,art Teddy,chess,lifting,swimming Sarah,baking,reading,singing,theatre Maya,violin,knitting,reading,billiards Amy,art,gaming,guitar,table tennis Daniel,video games,tennis,soccer,biking,trumpet Derek,cooking,flute,gaming,swimming,table tennis Daisey,video games,guitar,cleaning,drawing,animated shows,reading,shopping Lily,flute,ocarina,video games,baking Stella,roller skating,sudoku,watching baseball,harp Sophie,viola,ukulele,piano,video games
06-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值