您的位置:首页 > 其它

Educational Codeforces Round 31 E. Binary Matrix 并查集

2017-11-10 16:58 375 查看
http://codeforces.com/problemset/problem/884/E

题意:n行,然后每一行有m/4个数,每个数是16进制,要拆成二进制,那么每个数就是4位二进制数。

比如A就是10,就是1010。现在得到每一行的二进制数,由01排列,构成联通块,问1形成的联通块有

多少个。

做法:对每一行和他的下一行联通,掏出并查集来维护他们的联通关系,对于每一行更新联通关系。

/// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
/// __.' ~. .~ `.__
/// .'// \./ \\`.
/// .'// | \\`.
/// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
/// .'//.-" `-. | .-' "-.\\`.
/// .'//______.============-.. \ | / ..-============.______\\`.
/// .'______________________________\|/______________________________`.
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
#include <stdlib.h>
#include <time.h>
#include <bitset>
using namespace std;

#define pi acos(-1)
#define s_1(x) scanf("%d",&x)
#define s_2(x,y) scanf("%d%d",&x,&y)
#define s_3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define s_4(x,y,z,X) scanf("%d%d%d%d",&x,&y,&z,&X)
#define S_1(x) scanf("%I64d",&x)
#define S_2(x,y) scanf("%I64d%I64d",&x,&y)
#define S_3(x,y,z) scanf("%I64d%I64d%I64d",&x,&y,&z)
#define PI acos(-1)
#define endl '\n'
#define srand() srand(time(0));
#define me(x,y) memset(x,y,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0); cin.tie(0);
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define fOR(n,x,i) for(int i=n;i>=x;i--)
#define fOr(n,x,i) for(int i=n;i>x;i--)
#define W while
#define sgn(x) ((x) < 0 ? -1 : (x) > 0)
#define bug printf("***********\n");
#define db double
#define ll long long
#define mp make_pair
#define pb push_back
typedef long long LL;
typedef pair <int, int> ii;
const int INF=0x3f3f3f3f;
const LL LINF=1e18+10;
//const int dx[]={-1,0,1,0,1,-1,-1,1};
//const int dy[]={0,1,0,-1,-1,1,-1,1};
const int maxn=(1<<14)+10;
const int maxx=1e6+10;
const double EPS=1e-10;
const double eps=1e-10;
const int mod=1e9+7;
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}

void Out(LL a){if(a < 0) { putchar('-'); a = -a; }if(a >= 10) Out(a / 10);putchar(a % 10 + '0');}
void print(LL a){ Out(a),puts("");}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
//cerr << "run time is " << clock() << endl;
/*
void readString(string &s)
{
static char str[maxx];
scanf("%s", str);
s = str;
}
*/
LL ans=0;
int father[2][maxn];
int mark[maxn],group[maxn];
bool line1[maxn],line2[maxn];
int fi(int *fa,int x)
{
return fa[x]==x?x:fa[x]=fi(fa,fa[x]);
}
void uion(int *fa,int u,int v)
{
int x1=fi(fa,u);
int x2=fi(fa,v);
if(x1!=x2)
fa[x1]=x2;
}
void init(int *fa)
{
FOR(0,maxn-1,i) fa[i]=i;
}
int n,m;
string s;
void solve()
{
close();
W(cin>>n>>m)
{
ans=0;
init(father[0]);
me(line1,0);
me(line2,0);
FOR(1,n+1,i)
{
me(line1,0);
me(mark,0);
me(group,-1);
if(i!=(n+1))
{
cin>>s;
for(int j=m/4-1;j>=0;j--)
{
char c;
c=s[m/4-1-j];
int num=(c<='9')?(c-'0'):(c-'A'+10);
line1[4*j+0]=num&1;
line1[4*j+1]=(num>>1)&1;
line1[4*j+2]=(num>>2)&1;
line1[4*j+3]=(num>>3)&1;
}
}
for(int j=0;j<m;j++)
if(line1[j]&&line2[j]) mark[fi(father[0],j)]=1;
for(int j=0;j<m;j++)
if(line2[j]&&!mark[fi(father[0],j)]) ans++,mark[fi(father[0],j)]=1;
init(father[1]);
for(int j=0;j<m;j++)
if(line1[j]&&line2[j])
{
int pos=fi(father[0],j);
if(group[pos]==-1) group[pos]=j;
else {uion(father[1],group[pos],j);group[pos]=j;}
}
for(int j=0;j<m-1;j++)
if(line1[j]&&line1[j+1]) uion(father[1],j,j+1);
swap(line1,line2);
swap(father[1],father[0]);
}
print(ans);
}
}

int main()
{
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
int t=1;
//init();
//s_1(t);
for(int cas=1;cas<=t;cas++)
{
//printf("Case #%d: ",cas);
solve();
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: