Detachment HDU - 5976(染色法)

In a highly developed alien society, the habitats are almost infinite dimensional space.
In the history of this planet,there is an old puzzle.
You have a line segment with x units’ length representing one dimension.The line segment can be split into a number of small line segments: a1,a2, … (x= a1+a2+…) assigned to different dimensions. And then, the multidimensional space has been established. Now there are two requirements for this space:
1.Two different small line segments cannot be equal ( ai≠aj when i≠j).
2.Make this multidimensional space size s as large as possible (s= a1∗a2

*...).Note that it allows to keep one dimension.That's to say, the number of ai can be only one.
Now can you solve this question and find the maximum size of the space?(For the final number is too large,your answer will be modulo 10^9+7)

Input

The first line is an integer T,meaning the number of test cases.
Then T lines follow. Each line contains one integer x.
1≤T≤10^6, 1≤x≤10^9

Output

Maximum s you can get modulo 10^9+7. Note that we wants to be greatest product before modulo 10^9+7.

Sample Input

1
4

Sample Output

4

题意:一共有n支球队,m场比赛,已经知道x个强队的编号,y个弱队的编号,已知每场比赛

都有一个强队和一个弱队,且不会出现即使强队又是弱队的情况,问能不能将所有队伍按强队弱队分开

思路:

一共强弱两个阵营,一场比赛有一个强队和一个弱队,强队和弱队之间建边,如果能分开,则构成的图是二分图

且所有点均被染色,否则,不能分开

代码:

#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int N = 1005;
const int M = 10005;
vector<int>G[N];
queue<int>p;
int arr[M*2],vis[N];
int ans,id;
int n,m,x,y;
void init(){
	id=ans=0;
	memset(vis,0,sizeof(vis));
	for(int i=0;i<=n;i++)
	   G[i].clear();
	while(!p.empty()) p.pop();
}
int BFS(){
	while(!p.empty()){
		int u=p.front();
	//	printf("%d ",u);
		p.pop();
		ans++;
		for(int i=0;i<G[u].size();i++){
			int v=G[u][i];
			if(vis[v]==vis[u]) return 0;
			if(vis[v]) continue;
			vis[v]=-vis[u];
			p.push(v);
		}
	}
	return 1;
}
int main(){
	while(~scanf("%d%d%d%d",&n,&m,&x,&y)){
		init();
		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);
			arr[id++]=v;
			arr[id++]=u;
		}
	     for(int i=0;i<x;i++){
	     	int u;
	     	scanf("%d",&u);
	     	vis[u]=1;
	     	p.push(u);
		 }
		 for(int i=0;i<y;i++){
		 	int u;
	     	scanf("%d",&u);
	     	vis[u]=1;
	     	p.push(u);
		 }
		 int flag=BFS();
		 for(int i=0;i<id;i++){
		 	int u=arr[i];
		 	if(vis[u]) continue;
		 	vis[u]=1;
		 	p.push(u);
		 	flag=flag&&BFS();
		 }
		 if(ans==n&&flag) printf("YES\n");
		 else printf("NO\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/islittlehappy/article/details/81145338