Two Flowers CodeChef - TWOFL
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的更多相关文章
- 【BZOJ-3514】Codechef MARCH14 GERALD07加强版 LinkCutTree + 主席树
3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 1288 Solved: 490 ...
- CF451E Devu and Flowers (隔板法 容斥原理 Lucas定理 求逆元)
Codeforces Round #258 (Div. 2) Devu and Flowers E. Devu and Flowers time limit per test 4 seconds me ...
- poj 3262 Protecting the Flowers
http://poj.org/problem?id=3262 Protecting the Flowers Time Limit: 2000MS Memory Limit: 65536K Tota ...
- 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 ...
- poj1157LITTLE SHOP OF FLOWERS
Description You want to arrange the window of your flower shop in a most pleasant way. You have F bu ...
- CF459B Pashmak and Flowers (水
Pashmak and Flowers Codeforces Round #261 (Div. 2) B. Pashmak and Flowers time limit per test 1 seco ...
- 【BZOJ4260】 Codechef REBXOR 可持久化Trie
看到异或就去想前缀和(⊙o⊙) 这个就是正反做一遍最大异或和更新答案 最大异或就是很经典的可持久化Trie,从高到低贪心 WA: val&(1<<(base-1))得到的并不直接是 ...
- 线段树或树状数组---Flowers
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=4325 Description As is known to all, the blooming tim ...
- sgu 104 Little shop of flowers 解题报告及测试数据
104. Little shop of flowers time limit per test: 0.25 sec. memory limit per test: 4096 KB 问题: 你想要将你的 ...
随机推荐
- LeetCode——Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- delphi中的HOOK [转贴]
按事件分类,有如下的几种常用类型的钩子: 1)键盘钩子可以监视各种键盘消息. 2)鼠标钩子可以监视各种鼠标消息. 3)外壳钩子可以监视各种Shell事件消息. 4)日志钩子可以记录从系统消息队列中取出 ...
- 将异常(getStackTrace)转化成String
方法一: private static String getStackMsg(Exception e) { StringBuffer sb = new StringBuffer(); StackTra ...
- Why you shouldn’t connect your mobile application to a database
BY CRAIG CHAPMAN · PUBLISHED 2015-07-02 · UPDATED 2015-07-02 Working at Embarcadero, I frequently ...
- vue中显示和隐藏导航
const router = new VueRouter({ mode: 'history', routes: [ { path: '/first', component: firstView, me ...
- (转)windows下的Expdp、Impdp使用
1 .Expdp windows下导出截图示范 2.impdp windows下导入截图示范 Impdp system/eas directory=EASBAK dumpfile=stjt_10060 ...
- codeforces 505C C. Mr. Kitayuta, the Treasure Hunter(dp)
题目链接: C. Mr. Kitayuta, the Treasure Hunter time limit per test 1 second memory limit per test 256 me ...
- 获取指定类型如枚举/属性上自定义attribue值
有时候我们用数字来区分一些类型,如1:中国银行,2:建设银行,3:工商银行,…….这时候我在代码中通常会定义枚举来与定义的一一对应,并在该枚举值上设置特性来表示所代表的含义,这样避免多处写一些数字来标 ...
- mack pro常用快捷键
fn + left / right / up / down 相当于 home/end/page up /page down delete 删除光标前一个字符 fn + delete 删除当前光标后一个 ...
- python-----windows下安装face_recognition库
如果直接在cmd命令界面 输入:pip install face_recognition 如下图所示: 如果第一次就会出现一系列的问题,解决此问题就安装如下步骤: 一.如果你本机没有安装vistual ...