题目链接:点击打开链接
A. DZY Loves Chessboard
time limit per test
memory limit per test
input
output
DZY loves chessboard, and he enjoys playing with it.
n rows and m
You task is to find any suitable placement of chessmen on the given chessboard.
Input
n and m (1 ≤ n, m ≤ 100).
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
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.
Examples
input
1 1 .
output
B
input
2 2 .. ..
output
BW WB
input
3 3 .-. --- --.
output
B-B --- --B
Note
In the first sample, DZY puts a single black chessman. Of course putting a white one is also OK.
4
3
思路:其实就是开始的时候打个表,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;
}
题目链接:点击打开链接
B. DZY Loves Chemistry
time limit per test
memory limit per test
input
output
DZY loves chemistry, and he enjoys mixing chemicals.
n chemicals, and m
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.
Input
n and m
.
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.
1 to n
Output
Print a single integer — the maximum possible danger.
Examples
input
1 0
output
1
input
2 11 2
output
2
input
3 21 2 2 3
output
4
Note
In the first sample, there's only one way to pour, and the danger won't increase.
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;
}
题目链接:点击打开链接
C. DZY Loves Physics
time limit per test
memory limit per test
input
output
DZY loves Physics, and he enjoys calculating density.
Almost everything has density, even a graph. We define the density of a non-directed graph (nodes and edges of the graph have some values) as follows:
where v is the sum of the values of the nodes, e is the sum of the values of the edges.
G, now he wants to find a connected induced subgraph G' of the graph, such that the density of G'
G'(V', E') of a graph G(V, E)
- ;
- edge
- if and only if
- , and edge
- ;
- G' is the same as the value of the corresponding edge inG, so as the value of a node.
Help DZY to find the induced subgraph with maximum density. Note that the induced subgraph you choose must be connected.
Input
n (1 ≤ n ≤ 500),
. Integer n represents the number of nodes of the graph G, m
n space-separated integers xi (1 ≤ xi ≤ 106), where xi represents the value of the i-th node. Consider the graph nodes are numbered from 1 to n.
m lines contains three space-separated integers ai, bi, ci (1 ≤ ai < bi ≤ n; 1 ≤ ci ≤ 103), denoting an edge between node ai and bi with value ci. The graph won't contain multiple edges.
Output
10 - 9.
Examples
input
1 01
output
0.000000000000000
input
2 11 21 2 1
output
3.000000000000000
input
5 613 56 73 98 171 2 56 1 3 29 1 4 42 2 3 95 2 4 88 3 4 63
output
2.965517241379311
Note
1.
In the second sample, choosing the whole graph is optimal.
大意:给出一张图,图中的每个节点,每条边都有一个权值,现在有从中挑出一张子图,要求子图联通,并且被选中的任意两点,如果存在边,则一定要被选中。问密度(点的权值和 / 边的权值和)最大是多少?
思路:密度最大时一定是两个点连一条边,再加入任何的点和边都会使密度降低,枚举所有的连通边,找到最大密度就行了。
#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;
}