zoukankan      html  css  js  c++  java
  • Two Flowers CodeChef

    https://vjudge.net/problem/CodeChef-TWOFL

    先把颜色相同的合并成一个点,建好图,枚举要取的两种颜色(根据图中所有边决定哪些组合要枚举)即可

    错误记录:

    1.写了个假的对于诸如1 2 1 2这种数据只能找出3(前3个数)的答案的算法

    2.46行写成(i-1)*n+m

     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<cstring>
     4 #include<vector>
     5 #include<set>
     6 #include<map>
     7 using namespace std;
     8 #define fi first
     9 #define se second
    10 #define mp make_pair
    11 #define pb push_back
    12 typedef long long ll;
    13 typedef unsigned long long ull;
    14 typedef pair<int,int> pi;
    15 int n,m;
    16 int a[2010][2010],p[2010][2010];
    17 int fa[4000100],d[4000100];
    18 int sz[4000100],sz2[4000100];
    19 bool vis[4000100];
    20 int num[4000100],ta,ans;
    21 int dx[]={0,0,1,-1};
    22 int dy[]={1,-1,0,0};
    23 int find(int x)    {return x==fa[x]?x:fa[x]=find(fa[x]);}
    24 vector<int> e[4000100];
    25 //set<pi> s;
    26 map<pi,vector<pi>> ma;
    27 //vector<int> s2[4000100];
    28 vector<int> t;
    29 void ins(int a,int b)
    30 {
    31     int ta=d[a],tb=d[b];
    32     if(ta>tb)    swap(ta,tb);
    33     //s.insert(mp(ta,tb));
    34     if(a>b)    swap(a,b);
    35     ma[mp(ta,tb)].pb(mp(a,b));
    36 }
    37 int main()
    38 {
    39     int i,j,k,fx,fy,x,y;
    40     scanf("%d%d",&n,&m);
    41     for(i=1;i<=n;i++)
    42         for(j=1;j<=m;j++)
    43             scanf("%d",&a[i][j]);
    44     for(i=1;i<=n;i++)
    45         for(j=1;j<=m;j++)
    46             p[i][j]=(i-1)*m+j,d[p[i][j]]=a[i][j];
    47     for(i=1;i<=n*m;i++)    fa[i]=i,sz[i]=1;
    48     for(i=1;i<=n;i++)
    49         for(j=1;j<=m;j++)
    50             for(k=0;k<4;k++)
    51             {
    52                 x=i+dx[k];y=j+dy[k];
    53                 if(x>=1&&x<=n&&y>=1&&y<=m&&a[i][j]==a[x][y])
    54                 {
    55                     fx=find(p[i][j]);fy=find(p[x][y]);
    56                     if(fx!=fy)    fa[fy]=fx,sz[fx]+=sz[fy];
    57                 }
    58             }
    59     for(i=1;i<=n;i++)
    60         for(j=1;j<=m;j++)
    61         {
    62             fx=find(p[i][j]);//s2[d[fx]].pb(fx);
    63             for(k=0;k<4;k++)
    64             {
    65                 x=i+dx[k];y=j+dy[k];
    66                 if(x>=1&&x<=n&&y>=1&&y<=m)
    67                 {
    68                     fy=find(p[x][y]);
    69                     if(fx!=fy)    ins(fx,fy);
    70                 }
    71             }
    72         }
    73     for(i=1;i<=n*m;i++)    fa[i]=i,sz2[i]=sz[i],ans=max(ans,sz[i]);
    74     for(auto &xx:ma)
    75     {
    76         t.clear();
    77         for(auto &yy:xx.se)
    78         {
    79             fx=find(yy.fi);fy=find(yy.se);
    80             if(fx!=fy)    fa[fx]=fy,sz2[fy]+=sz2[fx],ans=max(ans,sz2[fy]),t.pb(fx),t.pb(fy);
    81         }
    82         for(auto &tt:t)    fa[tt]=tt,sz2[tt]=sz[tt];
    83     }
    84     printf("%d",ans);
    85     return 0;
    86 }
  • 相关阅读:
    求职方法论
    测试经验与教训_学习笔记
    测试架构师修炼之道_学习笔记
    Jmeter测试oracle
    Jmeter 非UI界面jmx脚本不能正常退出
    Jmeter参数化的理解
    jmeter 测试并发
    Jmeter测试数据库
    pytorch runtime error: CUDNN_STATUS_MAPPING_ERROR
    Python/pytorch 切换国内源/AttributeError: module 'torch.jit' has no attribute 'unused'/not a trusted or secure host
  • 原文地址:https://www.cnblogs.com/hehe54321/p/9280986.html
Copyright © 2011-2022 走看看