Codeforces Round #254 (Div. 2) A,B

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.

Sample test(s)
input
1 1
.
output
B
input
2 2
..
..
output
BW
WB
input
3 3
.-.
---
--.
output
B-B
---
--B

Note

A题的题意是给你一个矩阵,‘-’代表坏的细胞,‘.’代表好的细胞,而我们需要将好的细胞变化为‘W’或‘B’,且每个相邻的细胞不能一样,开始我是直接处理用一个for循环遍历‘.’的4个方向,如果有’w‘直接跳出标记为’B‘,反之为’W‘,但是这样的做法是不行的,因为会有一个细胞的相邻的4个细胞会既有’W‘又有’B‘,所以先将所有的细胞都标记为好的,然后去填充’W‘和’B‘,然后把’-‘的位置标记一下,输出的时候按照标记数组输出。以下是代码。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
char s[200][200];
int vx[] = { 0, 0, 1, -1 };
int vy[] = { 1, -1, 0, 0 };
int vis[200][200];
int main()
{
    //freopen("i.txt", "r",stdin);
    //freopen("o.txt", "w",stdout);
    int n, m;
    
    while (~scanf("%d%d", &n, &m))
    {
        memset(s, 0, sizeof(s));
        memset(vis, 0, sizeof(vis));
        int tt;
        for (int i = 0; i < n; i++)
            scanf("%s", s[i]);
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; s[i][j]!='\0'; j++)
            {
                if (s[i][j] == '-')
                    vis[i][j] = 1;
                tt = 1;
                {
                    tt = 0;
                    for (int k = 0; k < 4; k++)
                    {
                        int x = i + vx[k];
                        int y = j + vy[k];
                        if (x < 0 || y < 0 || x >= n || y>m)
                            continue;
                        if (s[x][y] == 'B')
                        {
                            tt = 1;
                            break;
                        }
                    }
                }
                if (tt)
                {
                    s[i][j] = 'W';
                }
                else
                    s[i][j] = 'B';
            }
        }
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; s[i][j] != '\0'; j++)
            {
                printf("%c", vis[i][j] ? '-' : s[i][j]);
            }
            printf("\n");
        }
    }
    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.

Sample test(s)
input
1 0
output
1
input
2 1
1 2
output
2
input
3 2
1 2
2 3
output
4
Note
这一题是考查并查集的,貌似也可以用深搜.需要注意的是结果会很大,要用long long去定义。
#include<iostream>
#include<cstdio>
using namespace std;
int a[10000];
int n,m,c,d;
int find(int x)
{
    int r=x;
    while(r!=a[r])
        r=a[r];
    return r;
}
void unio(int x,int y)
{
    int fx,fy;
    fx=find(x);
    fy=find(y);
    if(fx!=fy)
        a[fx]=fy;
}
long long f(int x)
{
    long long ans=1;
    for(int i=1;i<=n-x;i++)
    {
        ans*=2;
    }
    return ans;
}
int main()
{
    
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=1;i<=n;i++)
            a[i]=i;
        while(m--)
        {
            scanf("%d%d",&c,&d);
            unio(c,d);
        }
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            if(a[i]==i)
                ans++;
        }
        printf("%lld\n",f(ans));
    }
    return 0;
}


你可能感兴趣的:(Codeforces Round #254 (Div. 2) A,B)