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 问题: 你想要将你的 ...
随机推荐
- vue class与style绑定、条件渲染、列表渲染
列表渲染 根据我例子的需要,先来说下,列表渲染使用到的是v-for指令,需要使用 item in items 形式的特殊语法,items 是源数据数组并且 item 是数组元素迭代的别名,具体使用方法 ...
- Eclipse搭建Web Service服务
1.建立动态Web工程(Dynamic Web Project),工程名为Server.编写类HelloWorld. package com.mysever; public class HelloWo ...
- AndroidCityPicker仿IOS选择效果
近期的一个项目由于android端与IOS端须要同步,所以在城市选择器这里做了一个相似IOS的CityPicker控件,当然由于本人水平问题显示效果比IOS上面还是有一定差距的.OK先让大家看下效果. ...
- monitor and move the log content to our big data system
Apache Flume HDFS Sink Tutorial | HowToProgram https://howtoprogram.xyz/2016/08/01/apache-flume-hdfs ...
- [RK3288][Android6.0] 调试笔记 --- 测试I2C设备正常传输方法【转】
本文转载自:http://blog.csdn.net/kris_fei/article/details/71515020 Platform: RockchipOS: Android 6.0Kernel ...
- 「网络流24题」「LuoguP4015」 运输问题
Description W 公司有 m 个仓库和 n 个零售商店.第 i 个仓库有 ai 个单位的货物:第 j 个零售商店需要 bj 个单位的货物. 货物供需平衡,即 ∑ai=∑bj . 从第 i ...
- 【USACO】 Balanced Photo
[题目链接] 点击打开链接 [算法] 树状数组 [代码] #include<bits/stdc++.h> using namespace std; int i,N,ans,l1,l2; ] ...
- Spring中Bean获取IOC容器服务的方法
Spring 依赖注入可以让所有的Bean对其IOC容器的存在是没有意识的,甚至可以将容器换成其它的.但实际开发中如果某个Bean对象要用到Spring 容器本身的功能资源,需要意识到IOC容器的存在 ...
- 详细的Ajax使用
1. ajax对xml的接收和处理 xml主要作用: 主要保存和传输数据 1. xml文档结构 dom操作xml getElementsByTagName(); //根据标签名获取元素 childNo ...
- CMDB资产采集笔记
一.资产采集四种方式 1. Agent方式 API:Django接收数据并入库 程序:放置在每台服务器 应用场景:针对服务器较多的公司 步骤一: #执行本地命令的库 import subprocess ...