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

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

错误记录:

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

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

 #include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<set>
#include<map>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pi;
int n,m;
int a[][],p[][];
int fa[],d[];
int sz[],sz2[];
bool vis[];
int num[],ta,ans;
int dx[]={,,,-};
int dy[]={,-,,};
int find(int x) {return x==fa[x]?x:fa[x]=find(fa[x]);}
vector<int> e[];
//set<pi> s;
map<pi,vector<pi>> ma;
//vector<int> s2[4000100];
vector<int> t;
void ins(int a,int b)
{
int ta=d[a],tb=d[b];
if(ta>tb) swap(ta,tb);
//s.insert(mp(ta,tb));
if(a>b) swap(a,b);
ma[mp(ta,tb)].pb(mp(a,b));
}
int main()
{
int i,j,k,fx,fy,x,y;
scanf("%d%d",&n,&m);
for(i=;i<=n;i++)
for(j=;j<=m;j++)
scanf("%d",&a[i][j]);
for(i=;i<=n;i++)
for(j=;j<=m;j++)
p[i][j]=(i-)*m+j,d[p[i][j]]=a[i][j];
for(i=;i<=n*m;i++) fa[i]=i,sz[i]=;
for(i=;i<=n;i++)
for(j=;j<=m;j++)
for(k=;k<;k++)
{
x=i+dx[k];y=j+dy[k];
if(x>=&&x<=n&&y>=&&y<=m&&a[i][j]==a[x][y])
{
fx=find(p[i][j]);fy=find(p[x][y]);
if(fx!=fy) fa[fy]=fx,sz[fx]+=sz[fy];
}
}
for(i=;i<=n;i++)
for(j=;j<=m;j++)
{
fx=find(p[i][j]);//s2[d[fx]].pb(fx);
for(k=;k<;k++)
{
x=i+dx[k];y=j+dy[k];
if(x>=&&x<=n&&y>=&&y<=m)
{
fy=find(p[x][y]);
if(fx!=fy) ins(fx,fy);
}
}
}
for(i=;i<=n*m;i++) fa[i]=i,sz2[i]=sz[i],ans=max(ans,sz[i]);
for(auto &xx:ma)
{
t.clear();
for(auto &yy:xx.se)
{
fx=find(yy.fi);fy=find(yy.se);
if(fx!=fy) fa[fx]=fy,sz2[fy]+=sz2[fx],ans=max(ans,sz2[fy]),t.pb(fx),t.pb(fy);
}
for(auto &tt:t) fa[tt]=tt,sz2[tt]=sz[tt];
}
printf("%d",ans);
return ;
}

Two Flowers CodeChef - TWOFL的更多相关文章

  1. 【BZOJ-3514】Codechef MARCH14 GERALD07加强版 LinkCutTree + 主席树

    3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 1288  Solved: 490 ...

  2. CF451E Devu and Flowers (隔板法 容斥原理 Lucas定理 求逆元)

    Codeforces Round #258 (Div. 2) Devu and Flowers E. Devu and Flowers time limit per test 4 seconds me ...

  3. poj 3262 Protecting the Flowers

    http://poj.org/problem?id=3262 Protecting the Flowers Time Limit: 2000MS   Memory Limit: 65536K Tota ...

  4. Codeforces Round #381 (Div. 2)B. Alyona and flowers(水题)

    B. Alyona and flowers Problem Description: Let's define a subarray as a segment of consecutive flowe ...

  5. poj1157LITTLE SHOP OF FLOWERS

    Description You want to arrange the window of your flower shop in a most pleasant way. You have F bu ...

  6. CF459B Pashmak and Flowers (水

    Pashmak and Flowers Codeforces Round #261 (Div. 2) B. Pashmak and Flowers time limit per test 1 seco ...

  7. 【BZOJ4260】 Codechef REBXOR 可持久化Trie

    看到异或就去想前缀和(⊙o⊙) 这个就是正反做一遍最大异或和更新答案 最大异或就是很经典的可持久化Trie,从高到低贪心 WA: val&(1<<(base-1))得到的并不直接是 ...

  8. 线段树或树状数组---Flowers

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=4325 Description As is known to all, the blooming tim ...

  9. sgu 104 Little shop of flowers 解题报告及测试数据

    104. Little shop of flowers time limit per test: 0.25 sec. memory limit per test: 4096 KB 问题: 你想要将你的 ...

随机推荐

  1. Hibernate_14_数据连接池的使用

    在主配置文件Hibernate.cfg.xml中设置: <!-- 设置默认的事务隔离级别: 隔离级别 相应的整数表示 READ UNCOMMITED 1 READ COMMITED 2 REPE ...

  2. 修复Xcode升级错误 — PCH File Error

    http://www.rockia.net/2013/03/fix-xcode-update-pch-file-error Error:PCH File Built From A Different ...

  3. redis中键值对中值的各种类型

    1 value的最基本的数据类型是String 2 如果value是一张图片 先对图片进行base64编码成一个字符串,然后再保存到redis中,用的时候进行base64解码即可. 这是base64的 ...

  4. 从数据源拉取数据,将数据内容与一组搜索项做比对 go func() chanel

    https://github.com/goinaction/code [root@hadoop3 sample]# go run main.go 2018/07/30 17:45:39 Registe ...

  5. C语言操作SQLite数据库

    SQLite头文件和源文件下载地址http://www.sqlite.org/download.html 以下是示例代码 #include <stdio.h> #include " ...

  6. 浏览器上的Qt Quick

    你想不想在浏览器上运行你的Qt Quick程序呢?在Qt 5.12之前,唯一的方法是使用Qt WebGL Streaming技术把界面镜像到浏览器上.但该方法有不少缺陷,下文会说.前不久随着Qt 5. ...

  7. QT实现FTP服务器(三)

    QFtpClient类的实现: #include "QFtpClient.h" #include <QDebug> #include <QThread> # ...

  8. #1241 : Best Route in a Grid

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个N行N列的非负整数方阵,从左上角(1,1)出发,只能向下或向右走,且不能到达值为0的方格,求出一条到达右下角的最佳 ...

  9. 动态的添加ImageView到LinearLayout中并居中显示

    ImageView imageView = new ImageView(mActivity); imageView.setImageResource(R.mipmap.gengduo); Linear ...

  10. Oracle :修改数据库服务器字符集

    最近,有现场反应,程序显示乱码.感觉很奇怪,该系统已经卖出去无数了.肯定是现场数据库字符集有问题,经过查看, 现场环境: window系统,oracle10g. 我们要求的数据库字符集是AL32UTF ...