codeforces-445【A思维】【B并查集】

本文解析了DZY系列算法题中的三道题目,包括棋盘放置棋子问题、化学试剂混合的最大危险系数问题以及图的密度最大子图问题。通过具体的代码实现展示了算法的设计与实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:点击打开链接

A. DZY Loves Chessboard
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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

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.

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.

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;
}

题目链接:点击打开链接

B. DZY Loves Chemistry
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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.

Output

Print a single integer — the maximum possible danger.

Examples
input
1 0
output
1
input
2 1
1 2
output
2
input
3 2
1 2
2 3
output
4
Note

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;
}
 

题目链接: 点击打开链接

C. DZY Loves Physics
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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.

Once DZY got a graph G, now he wants to find a connected induced subgraph G' of the graph, such that the density of G' is as large as possible.

An induced subgraph G'(V', E') of a graph G(V, E) is a graph that satisfies:

  • ;
  • edge  if and only if , and edge ;
  • the value of an edge in G' is the same as the value of the corresponding edge in G, 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

The first line contains two space-separated integers n (1 ≤ n ≤ 500). Integer n represents the number of nodes of the graph Gm represents the number of edges.

The second line contains 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.

Each of the next 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

Output a real number denoting the answer, with an absolute or relative error of at most 10 - 9.

Examples
input
1 0
1
output
0.000000000000000
input
2 1
1 2
1 2 1
output
3.000000000000000
input
5 6
13 56 73 98 17
1 2 56
1 3 29
1 4 42
2 3 95
2 4 88
3 4 63
output
2.965517241379311
Note

In the first sample, you can only choose an empty subgraph, or the subgraph containing only node 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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值