题目链接:点击打开链接
DZY loves chessboard, and he enjoys playing with it.
He has a chessboard of n rows and m columns. Some cells of the chessboard are bad, others are good. For every good cell, DZY wants to put a chessman on it. Each chessman is either white or black. After putting all chessmen, DZY wants that no two chessmen with the same color are on two adjacent cells. Two cells are adjacent if and only if they share a common edge.
You task is to find any suitable placement of chessmen on the given chessboard.
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100).
Each of the next n lines contains a string of m characters: the j-th character of the i-th string is either "." or "-". A "." means that the corresponding cell (in the i-th row and the j-th column) is good, while a "-" means it is bad.
Output must contain n lines, each line must contain a string of m characters. The j-th character of the i-th string should be either "W", "B" or "-". Character "W" means the chessman on the cell is white, "B" means it is black, "-" means the cell is a bad cell.
If multiple answers exist, print any of them. It is guaranteed that at least one answer exists.
1 1 .
B
2 2 .. ..
BW WB
3 3 .-. --- --.
B-B --- --B
In the first sample, DZY puts a single black chessman. Of course putting a white one is also OK.
In the second sample, all 4 cells are good. No two same chessmen share an edge in the sample output.
In the third sample, no good cells are adjacent. So you can just put 3 chessmen, no matter what their colors are.
思路:其实就是开始的时候打个表,B 和 W 交叉放,碰见 “ . ” 就输出 B 或 W,否则就原样输出 “ - ”
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m;
char map[110][110];
int main()
{
while(~scanf("%d%d",&n,&m))
{
bool flag=1;
for(int i=0;i<n;i++)
{
scanf("%s",map[i]);
for(int j=0;j<m;j++)
{
if(map[i][j]=='.')
{
if((i+j)&1)
{
map[i][j]='B'; // B 与 W 可互换位置的
}
else
map[i][j]='W';
}
}
}
for(int i=0;i<n;i++)
{
puts(map[i]);
}
}
return 0;
}
题目链接:点击打开链接
DZY loves chemistry, and he enjoys mixing chemicals.
DZY has n chemicals, and m pairs of them will react. He wants to pour these chemicals into a test tube, and he needs to pour them in one by one, in any order.
Let's consider the danger of a test tube. Danger of an empty test tube is 1. And every time when DZY pours a chemical, if there are already one or more chemicals in the test tube that can react with it, the danger of the test tube will be multiplied by 2. Otherwise the danger remains as it is.
Find the maximum possible danger after pouring all the chemicals one by one in optimal order.
The first line contains two space-separated integers n and m .
Each of the next m lines contains two space-separated integers xi and yi (1 ≤ xi < yi ≤ n). These integers mean that the chemical xi will react with the chemical yi. Each pair of chemicals will appear at most once in the input.
Consider all the chemicals numbered from 1 to n in some order.
Print a single integer — the maximum possible danger.
1 0
1
2 1 1 2
2
3 2 1 2 2 3
4
In the first sample, there's only one way to pour, and the danger won't increase.
In the second sample, no matter we pour the 1st chemical first, or pour the 2nd chemical first, the answer is always 2.
In the third sample, there are four ways to achieve the maximum possible danger: 2-1-3, 2-3-1, 1-2-3 and 3-2-1 (that is the numbers of the chemicals in order of pouring).
大意:有 1 - n 种化学药剂 总共有 m 对试剂能反应,按不同的次序将 1 - n 种试剂滴入试管,如果正在滴入的试剂能与已经滴入的试剂反应,那么危险数 * 2 ,否则维持不变。问最后最大的危险系数是多少?
<pre name="code" class="cpp">#include<cstdio>
#include<algorithm>
#include<cstring>
#define LL long long
using namespace std;
int n,m,cnt;
int fa[100];
int find(int x)
{
if(x==fa[x]) return x;
return fa[x]=find(fa[x]);
}
void Union(int x,int y)
{
int nx=find(x);
int ny=find(y);
if(nx!=ny)
{
fa[nx]=ny;
cnt++;
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=1;i<=n;i++)
fa[i]=i;
cnt=0;
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
Union(a,b);
}
printf("%I64d\n",1LL<<cnt); // long long型输出
}
return 0;
}
题目链接: 点击打开链接
大意:给出一张图,图中的每个节点,每条边都有一个权值,现在有从中挑出一张子图,要求子图联通,并且被选中的任意两点,如果存在边,则一定要被选中。问密度(点的权值和 / 边的权值和)最大是多少?
思路:密度最大时一定是两个点连一条边,再加入任何的点和边都会使密度降低,枚举所有的连通边,找到最大密度就行了。
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m;
int val[510];
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=1;i<=n;i++)
scanf("%d",val+i);
double ans=0;
while(m--)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
ans=max(ans,(val[u]+val[v]+0.0)/w);
}
printf("%.10lf\n",ans);
}
return 0;
}