棋盘制作
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <set>
#include <string>
long n;long m;
long maxl[2][2010];
long maxr[2][2010];
long _l[2010];
long _r[2010];
long height[2010];
long map[2010][2010];
long sum[2010][2010];
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
const long inf = 0x7f7f7f7f;
long ans = -inf;
long ans2 = -inf;
void work()
{
for (long j=0;j<m+2;j++)
{
maxl[0][j] = 0;
maxr[0][j] = m;
height[j] = 0;
}
for (long i=1;i<n+1;i++)
{
_l[0] = 0;_r[m+1] = m;
for (long j=1;j<m+1;j++)
{
if (map[i][j])_l[j]=_l[j-1];
else _l[j]=j;
}
for (long j=m;j>0;j--)
{
if (map[i][j])_r[j]=_r[j+1];
else _r[j]=j-1;
}
for (long j=1;j<m+1;j++)
{
if (map[i][j])
{
maxl[i&1][j] = MAX(maxl[(i&1)^1][j],_l[j]);
maxr[i&1][j] = MIN(maxr[(i&1)^1][j],_r[j]);
long minedge = MIN(i-height[j],maxr[i&1][j]-maxl[i&1][j]);
ans = MAX(ans,sum[i][maxr[i&1][j]]+sum[height[j]][maxl[i&1][j]]
-sum[i][maxl[i&1][j]]-sum[height[j]][maxr[i&1][j]]);
ans2 = MAX(ans2,minedge*minedge);
}
else
{
height[j] = i;
maxl[i&1][j] = 0;
maxr[i&1][j] = m;
}
}
}
}
int main()
{
freopen("chess.in","r",stdin);
freopen("chess.out","w",stdout);
scanf("%ld%ld",&n,&m);
for (long i=1;i<n+1;i++)
for (long j=1;j<m+1;j++)
{
do map[i][j] = getchar()-'0';
while (map[i][j]&&map[i][j]-1);
}
for (long k=2;k<n+m+1;k+=2)
for (long i=MAX(k-m,1);i<k;i++)
map[i][k-i] ^= 1;
for (long i=1;i<n+1;i++)
for (long j=1;j<m+1;j++)
sum[i][j] = sum[i][j-1]+map[i][j];
for (long i=1;i<n+1;i++)
for (long j=1;j<m+1;j++)
sum[i][j] += sum[i-1][j];
work();
for (long j=0;j<m+2;j++)
sum[0][j] = 0;
for (long i=1;i<n+1;i++)
for (long j=1;j<m+1;j++)
map[i][j]^=1;
for (long i=1;i<n+1;i++)
for (long j=1;j<m+1;j++)
sum[i][j] = sum[i][j-1]+map[i][j];
for (long i=1;i<n+1;i++)
for (long j=1;j<m+1;j++)
sum[i][j] += sum[i-1][j];
work();
printf("%ld\n%ld",ans2,ans);
}
耍朋友
#include <cstdlib>
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define MIN(a,b) ((a)<(b)?(a):(b))
struct node
{
long ind;
node* nxt;
};
node* head[4010];
bool map[4010][4010];
long DFN[4010];
long LOW[4010];
long top = 0;
long Belong[4010];
long Stack[4010];
bool InStack[4010];
long Bcnt = 0;
long N;
void insert(long a,long b)
{
node* tmp = new node;
tmp -> ind = b;
tmp -> nxt = head[a];
head[a] = tmp;
}
long TIME = 0;
void Tarjan(long u)
{
DFN[u] = LOW[u] = ++TIME;
InStack[u] = true;
Stack[++top] = u;
for (node* vv=head[u];vv;vv=vv->nxt)
{
long v = vv->ind;
if (!DFN[v])
{
Tarjan(v);
LOW[u] = MIN(LOW[u],LOW[v]);
}
else if(InStack[v])
{
LOW[u] = MIN(LOW[u],DFN[v]);
}
}
if (DFN[u] == LOW[u])
{
++Bcnt;long v;
do
{
v = Stack[top--];
Belong[v] = Bcnt;
InStack[v] = false;
// Block[Bcnt][++Block[Bcnt][0]]=v;
}while (v-u);
}
}
int getint()
{
char tmp;long rs=0;bool sgn=1;
do tmp = getchar();
while (!isdigit(tmp)&&tmp-'-');
if (tmp == '-'){tmp=getchar();sgn=0;}
do rs = (rs<<3)+(rs<<1)+tmp-'0';
while (isdigit(tmp=getchar()));
return sgn?rs:-rs;
}
long ans[4010];
int main()
{
freopen("love.in","r",stdin);
freopen("love.out","w",stdout);
N = getint();
for (long i=1;i<N+1;i++)
{
long m = getint();
for (long j=1;j<m+1;j++)
{
long v = getint();
insert(i,v+N);
map[i][v+N] = true;
}
}
for (long i=1;i<N+1;i++)
{
long v = getint();
insert(v+N,i);
}
for (long i=1;i<2*N+1;i++)
{
if (!DFN[i])
{
Tarjan(i);
}
}
for (long i=1;i<N+1;i++)
{
ans[0] = 0;
for (long j=N+1;j<2*N+1;j++)
if (Belong[j]==Belong[i] && map[i][j])
ans[++ans[0]] = j;
printf("%ld ",ans[0]);
for (long j=1;j<ans[0]+1;j++)
{
printf("%ld ",ans[j]-N);
}
printf("\n");
}
return 0;
}
Tree
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cstdlib>
#include <map>
#include <vector>
#include <iostream>
#include <string>
struct node
{
long ind;
node* nxt;
long w;
};
node* head[50010];
long DFN[100010];
long DEPTH[100010];
long dist[50010];
long FIRST[50010];
long TIME = 0;
long DIST = 0;
long st[100010][20];
bool vis[50010];
void insert(long u,long v,long w)
{
node* nn = new node;
nn -> ind = v;
nn -> w = w;
nn -> nxt = head[u];
head[u] = nn;
nn = new node;
nn -> ind = u;
nn -> w = w;
nn -> nxt = head[v];
head[v] = nn;
}
void dfs(long u,long d)
{
++TIME;
DFN[TIME] = u;
DEPTH[TIME] = d;
FIRST[u] = TIME;
dist[u] = DIST;
for (node* vv=head[u];vv;vv=vv->nxt)
{
long v = vv->ind;
if (!vis[v])
{
vis[v] = true;
DIST += vv->w;
dfs(v,d+1);
DIST -= vv->w;
++TIME;
DFN[TIME] = u;
DEPTH[TIME] = d;
}
}
}
long lg2(long a)
{
long ans = 0;
while (a>>=1)
ans ++;
return ans;
}
long RMQ(long a,long b)
{
if (a > b){a^=b;b^=a;a^=b;}
long len = lg2(b-a+1);
if (DEPTH[st[a][len]]<DEPTH[st[b-(1<<len)+1][len]])
return st[a][len];
else
return st[b-(1<<len)+1][len];
}
int getint()
{
long rs=0;char tmp;bool sgn=1;
do tmp = getchar();
while (!isdigit(tmp)&&tmp-'-');
if (tmp == '-'){tmp=getchar();sgn=0;}
do rs = (rs<<3)+(rs<<1)+tmp-'0';
while (isdigit(tmp=getchar()));
return sgn?rs:-rs;
}
int main()
{
freopen("tree.in","r",stdin);
freopen("tree.out","w",stdout);
long n = getint();
for (long i=1;i<n;i++)
{
long u = getint()+1;
long v = getint()+1;
long w = getint();
insert(u,v,w);
}
for (long i=1;i<n+1;i++)
if (head[i])
{
vis[i] = true;
dfs(i,0);
break;
}
for (long i=1;i<TIME+1;i++)
{
st[i][0] = i;
}
for (long j=1;(1<<j)<TIME+1;j++)
{
for (long i=1;i+(1<<j)-1<TIME+1;i++)
{
if (DEPTH[st[i][j-1]]<DEPTH[st[i+(1<<(j-1))][j-1]])
st[i][j] = st[i][j-1];
else
st[i][j] = st[i+(1<<(j-1))][j-1];
}
}
long m = getint();
while (m--)
{
long a = getint()+1;
long b = getint()+1;
long lca = DFN[RMQ(FIRST[a],FIRST[b])];
printf("%ld\n",dist[a]+dist[b]-(dist[lca]<<1));
}
return 0;
}
地板
如果行数小于列数则交换行列。。这样会快得多。
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
long n;long m;
struct Node
{
long id;
long s;
Node* nxt;
};
typedef long long ll;
const long MaxS = 1000000;
const long Hmod = 100000;
Node mmry[MaxS];
Node* hash[Hmod];
ll f[2][MaxS];
long Sta[2][MaxS];
long cnt[2];
long ths = 1;
long pre = 0;
bool map[110][110];
inline long get_ID(long s)
{
long h = s%Hmod;
for (Node* vv=hash[h];vv;vv=vv->nxt)
if (vv->s == s)
return vv->id;
++cnt[ths];
mmry[cnt[ths]].id = cnt[ths];
mmry[cnt[ths]].s = s;
mmry[cnt[ths]].nxt = hash[h];
hash[h] = &mmry[cnt[ths]];
f[ths][cnt[ths]] = 0;
Sta[ths][cnt[ths]]=s;
return cnt[ths];
}
inline void clear()
{
memset(hash,0,sizeof hash);
cnt[ths] = 0;
}
inline void ADD(ll& a,ll& b)
{
a = (a+b);
if (a>20110519)
a-=20110520;
}
void work()
{
clear();
f[ths][get_ID(0)] = 1;
for (long i=1;i<n+1;i++)
{
for (long j=1;j<m+1;j++)
{
ths^=1;pre^=1;
clear();
long q = (m-j)<<1;
long p = q+2;
for (long k=1;k<cnt[pre]+1;k++)
{
long last = Sta[pre][k];
if (j == 1)
{
if (last & 3) continue;
last >>= 2;
}
long wp = (last>>p)&3;
long wq = (last>>q)&3;
long now = last - (wp<<p) - (wq<<q);
if (!map[i][j])
{
if (!wp && !wq)
ADD(f[ths][get_ID(now)],f[pre][k]);
continue;
}
if (!wp && !wq)
{
ADD(f[ths][get_ID(now|(1<<p))],f[pre][k]);
ADD(f[ths][get_ID(now|(1<<q))],f[pre][k]);
ADD(f[ths][get_ID(now|(3<<q)|(3<<p))],f[pre][k]);
continue;
}
if (wp && wq)
{
if (wp == 1 && wq == 1)
{
ADD(f[ths][get_ID(now)],f[pre][k]);
}
continue;
}
if (wp || wq)
{
ADD(f[ths][get_ID(now|(wq<<p)|(wp<<q))],f[pre][k]);
if (wp == 1)
{
ADD(f[ths][get_ID(now|(3<<p))],f[pre][k]);
continue;
}
if (wq == 1)
{
ADD(f[ths][get_ID(now|(3<<q))],f[pre][k]);
continue;
}
else
ADD(f[ths][get_ID(now)],f[pre][k]);
}
}
}
}
std::cout << f[ths][get_ID(0)];
}
int main()
{
freopen("floor.in","r",stdin);
freopen("floor.out","w",stdout);
scanf("%ld%ld",&n,&m);
if (n >= m)
{
for (long i=1;i<n+1;i++)
{
getchar();
for (long j=1;j<m+1;j++)
{
char tmp = getchar();
map[i][j] = (tmp=='_');
}
}
}
else
{
for (long i=1;i<n+1;i++)
{
getchar();
for (long j=1;j<m+1;j++)
{
char tmp = getchar();
map[j][i] = (tmp=='_');
}
}
long tmp = n;
n = m;
m = tmp;
}
work();
return 0;
}