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. appium支持的版本

    appium 支持4.2以上的版本 2.3-4.1的版本的支持通过Selendroid实现

  2. Testng 运行报错:"Total tests run: 0, Failures: 0, Skips: 0"以及找不到class文件的问题

    "Total tests run: 0, Failures: 0, Skips: 0" This means that there were no tests executed a ...

  3. HDU 3639 Hawk-and-Chicken

    Hawk-and-Chicken Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. 基于mac系统的apacheserver的使用流程

    打开终端.输入下面命令:sudo apachectl start 此时Apache已经开启.在浏览器中输入本地ip地址能够看到it works! 打开前往----电脑------Macintosh H ...

  5. eclipse4.3 解决没有check out as maven project

    最近想工作之余写点测试demo,习惯了公司的开发环境,便决定自己搭建开发环境,首先是找到好用的eclipse,就是能够使用eclipse创建maven project工程,该工程能够被eclipse的 ...

  6. HDU3652 B-number —— 数位DP

    题目链接:https://vjudge.net/problem/HDU-3652 B-number Time Limit: 2000/1000 MS (Java/Others)    Memory L ...

  7. SpringBoot快速HelloWorld入门

    1.新建maven项目 2.pom.xml 里添加SpringBoot所依赖的jar包 <parent> <groupId>org.springframework.boot&l ...

  8. windows server2012之部署HTTPS安全站点

    现在的互联网越来越重视网络安全方面的内容,像我们日常生活中浏览的网上银行网站等涉及安全的你都会发现有https 的标志出现,在URL前加https://前缀表明是用SSL加密的. 你的电脑与服务器之间 ...

  9. 【POJ 2407】 Relatives

    [题目链接] 点击打开链接 [算法] 欧拉函数 [代码] #include <algorithm> #include <bitset> #include <cctype& ...

  10. 【BZOJ 2721】 樱花

    [题目链接] 点击打开链接 [算法] 令n!=z,因为1 / x + 1 / y = 1 / z,所以x,y>z,不妨令y = z + d 则1 / x + 1 / (z + d) = 1 / ...