zoukankan      html  css  js  c++  java
  • Codeforces Round #385 (Div. 2)A B C 模拟 水 并查集

    A. Hongcow Learns the Cyclic Shift
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Hongcow is learning to spell! One day, his teacher gives him a word that he needs to learn to spell. Being a dutiful student, he immediately learns how to spell the word.

    Hongcow has decided to try to make new words from this one. He starts by taking the word he just learned how to spell, and moves the last character of the word to the beginning of the word. He calls this a cyclic shift. He can apply cyclic shift many times. For example, consecutively applying cyclic shift operation to the word "abracadabra" Hongcow will get words "aabracadabr", "raabracadab" and so on.

    Hongcow is now wondering how many distinct words he can generate by doing the cyclic shift arbitrarily many times. The initial string is also counted.

    Input

    The first line of input will be a single string s (1 ≤ |s| ≤ 50), the word Hongcow initially learns how to spell. The string s consists only of lowercase English letters ('a'–'z').

    Output

    Output a single integer equal to the number of distinct strings that Hongcow can obtain by applying the cyclic shift arbitrarily many times to the given string.

    Examples
    Input
    abcd
    Output
    4
    Input
    bbb
    Output
    1
    Input
    yzyz
    Output
    2
    Note

    For the first sample, the strings Hongcow can generate are "abcd", "dabc", "cdab", and "bcda".

    For the second sample, no matter how many times Hongcow does the cyclic shift, Hongcow can only generate "bbb".

    For the third sample, the two strings Hongcow can generate are "yzyz" and "zyzy".

    题意:给你一个字符串 每次可以把最尾部的一个字符放到最前面 问形成的字符串有多少种?

    题解:模拟

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <algorithm>
     6 #include <stack>
     7 #include <queue>
     8 #include <cmath>
     9 #include <map>
    10 #define ll  __int64
    11 #define mod 1000000007
    12 #define dazhi 2147483647
    13 #define bug() printf("!!!!!!!")
    14 #define M 100005
    15 using namespace  std;
    16 char s[55];
    17 map<string,int>mp;
    18 int main()
    19 {
    20     scanf("%s",s);
    21     string str;
    22     str.assign(s);
    23     mp[s]=1;
    24     int ans=1;
    25     int len=strlen(s);
    26     int last=len-1;
    27     len--;
    28     while(len)
    29     {
    30         len--;
    31         char exm=s[last];
    32         for(int i=last;i>=1;i--)
    33         {
    34             s[i]=s[i-1];
    35         }
    36         s[0]=exm;
    37         str.assign(s);
    38         if(mp[str]==0)
    39         {
    40             mp[str]++;
    41             ans++;
    42         }
    43     }
    44     cout<<ans<<endl;
    45     return 0;
    46 }
    B. Hongcow Solves A Puzzle
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Hongcow likes solving puzzles.

    One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified.

    The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap.

    You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position.

    Input

    The first line of input will contain two integers n and m (1 ≤ n, m ≤ 500), the dimensions of the puzzle piece.

    The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space.

    It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region.

    Output

    Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise.

    Examples
    Input
    2 3
    XXX
    XXX
    Output
    YES
    Input
    2 2
    .X
    XX
    Output
    NO
    Input
    5 5
    .....
    ..X..
    .....
    .....
    .....
    Output
    YES
    Note

    For the first sample, one example of a rectangle we can form is as follows


    111222
    111222

    For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle.

    In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle:


    .....
    ..XX.
    .....
    .....
    .....

    题意:给你一个n*m的矩阵 由‘X’形成一个图形 问你是否可以用两个相同的图形拼成一个矩形(不可以旋转和翻折)
    题解:确定给你的图形是否为一个矩形即可
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <algorithm>
     6 #include <stack>
     7 #include <queue>
     8 #include <cmath>
     9 #include <map>
    10 #define ll  __int64
    11 #define mod 1000000007
    12 #define dazhi 2147483647
    13 #define bug() printf("!!!!!!!")
    14 #define M 100005
    15 using namespace  std;
    16 int n,m;
    17 char a[505][505];
    18 int main()
    19 {
    20     scanf("%d %d",&n,&m);
    21     getchar();
    22     for(int i=0;i<n;i++)
    23       scanf("%s",a[i]);
    24     int l=m-1,r=0,u=n-1,d=0;
    25     for(int i=0;i<n;i++)
    26     {
    27         for(int j=0;j<m;j++)
    28         {
    29             if(a[i][j]=='X')
    30             {
    31                 l=min(l,j);
    32                 r=max(r,j);
    33                 u=min(u,i);
    34                 d=max(d,i);
    35             }
    36         }
    37     }
    38     for(int i=u;i<=d;i++)
    39     {
    40         for(int j=l;j<=r;j++)
    41         {
    42             if(a[i][j]=='.')
    43             {
    44                 printf("NO
    ");
    45                 return 0;
    46             }
    47         }
    48     }
    49     printf("YES
    ");
    50     return 0;
    51 }
    C. Hongcow Builds A Nation
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Hongcow is ruler of the world. As ruler of the world, he wants to make it easier for people to travel by road within their own countries.

    The world can be modeled as an undirected graph with n nodes and m edges. k of the nodes are home to the governments of the k countries that make up the world.

    There is at most one edge connecting any two nodes and no edge connects a node to itself. Furthermore, for any two nodes corresponding to governments, there is no path between those two nodes. Any graph that satisfies all of these conditions is stable.

    Hongcow wants to add as many edges as possible to the graph while keeping it stable. Determine the maximum number of edges Hongcow can add.

    Input

    The first line of input will contain three integers n, m and k (1 ≤ n ≤ 1 000, 0 ≤ m ≤ 100 000, 1 ≤ k ≤ n) — the number of vertices and edges in the graph, and the number of vertices that are homes of the government.

    The next line of input will contain k integers c1, c2, ..., ck (1 ≤ ci ≤ n). These integers will be pairwise distinct and denote the nodes that are home to the governments in this world.

    The following m lines of input will contain two integers ui and vi (1 ≤ ui, vi ≤ n). This denotes an undirected edge between nodes ui and vi.

    It is guaranteed that the graph described by the input is stable.

    Output

    Output a single integer, the maximum number of edges Hongcow can add to the graph while keeping it stable.

    Examples
    Input
    4 1 2
    1 3
    1 2
    Output
    2
    Input
    3 3 1
    2
    1 2
    1 3
    2 3
    Output
    0
    Note

    For the first sample test, the graph looks like this:

    Vertices 1 and 3 are special. The optimal solution is to connect vertex 4 to vertices 1 and 2. This adds a total of 2 edges. We cannot add any more edges, since vertices 1 and 3 cannot have any path between them.

    For the second sample test, the graph looks like this:

    We cannot add any more edges to this graph. Note that we are not allowed to add self-loops, and the graph must be simple.
    题意:n个电 m条边 k个特殊点  要求k个点(我称为奇异点)互相不联通(已知的m条边不会使得k个点联通)  问你最多添加多少条边
    题解:并查集找到各个联通块  在k个点中找到最大的联通块 与其他无奇异点的联通块中的点连边 各个联通块中补边到完全图  各个非奇异点间补边
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <algorithm>
     6 #include <stack>
     7 #include <queue>
     8 #include <cmath>
     9 #include <map>
    10 #define ll  __int64
    11 #define mod 1000000007
    12 #define dazhi 2147483647
    13 #define bug() printf("!!!!!!!")
    14 #define M 100005
    15 using namespace  std;
    16 int n,m,k;
    17 int a[1005];
    18 int fa[1005];
    19 int l,r;
    20 map<int,int>mp;
    21 map<int,int>mpp;
    22 int find(int root)
    23 {
    24     if(root!=fa[root])
    25         return fa[root]=find(fa[root]);
    26     else
    27         return root;
    28 }
    29 void unio(int a,int b)
    30 {
    31     int aa=find(a);
    32     int bb=find(b);
    33     if(aa!=bb)
    34     {
    35         fa[aa]=bb;
    36     }
    37 }
    38 int main()
    39 {
    40     scanf("%d %d %d",&n,&m,&k);
    41     for(int i=1;i<=k;i++)
    42         scanf("%d",&a[i]);
    43     for(int i=1;i<=n;i++)
    44         fa[i]=i;
    45     for(int i=1;i<=m;i++)
    46     {
    47         scanf("%d %d",&l,&r);
    48         unio(l,r);
    49     }
    50     for(int i=1;i<=n;i++)
    51     {
    52         int x=find(i);
    53         mp[fa[i]]++;
    54     }
    55     int maxn=0;
    56     int sum=0;
    57     int re=0;
    58     for(int i=1;i<=k;i++)
    59     {
    60         maxn=max(maxn,mp[fa[a[i]]]);
    61         sum+=mp[fa[a[i]]];
    62         mpp[fa[a[i]]]=1;
    63         re=re+ mp[fa[a[i]]]*(mp[fa[a[i]]]-1)/2;
    64     }
    65     int b[1005];
    66     int jishu=0;
    67     int ans=0;
    68     for(int i=1;i<=n;i++)
    69     {
    70         if(mpp[fa[i]]==0)
    71         {
    72             b[jishu++]=mp[fa[i]];
    73             re=re+mp[fa[i]]*(mp[fa[i]]-1)/2;
    74             mpp[fa[i]]=1;
    75         }
    76     }
    77     for(int i=0;i<jishu;i++)
    78     {
    79         for(int j=i+1;j<jishu;j++)
    80         {
    81             ans+=(b[i]*b[j]);
    82         }
    83     }
    84     printf("%d
    ",re-m+(n-sum)*maxn+ans);
    85     return 0;
    86 }
  • 相关阅读:
    zabbix监控docker
    Ubuntu下Zabbix结合percona监控mysql数据
    centos7安装ftp
    Ubuntu 16.04 搭建 ELK
    ubuntu网卡配置及安装ssh服务
    CentOS7.5二进制安装MySQL-5.6.40
    生产环境MySQL数据库集群MHA上线实施方案
    Mysql主从复制
    GIt+jenkins代码自动上线
    虚拟机网卡丢失解决方法
  • 原文地址:https://www.cnblogs.com/hsd-/p/6591522.html
Copyright © 2011-2022 走看看