链接
http://www.lydsy.com/JudgeOnline/problem.php?id=1095
题解
泥萌知道作为一个
OIer
因为被卡常数连续好几天没有
A
题,现在突然
A
了一道题的感受是啥样的吗?
我卡过去了!2333
这题啊,亦可赛艇。
做法
popoqqq
爷已经说得很详细了:
http://blog.youkuaiyun.com/popoqqq/article/details/44461423
一开始被卡常是因为用
map
存的距离,所以每次查询自带
log
,我用
clock()
函数计算了一下,发现光调用
map
的时间就占了程序总运行时间的
12
,因此我去模了
will7101
爷然后学会了哈希链表,然后套上之后就
A
掉了。(果然是被卡常了改过之后变成
27s
)
哦忘了说,这个算法的时间复杂度是
O((N+M)log22N)
代码
//点分治
#include <cstdio>
#include <algorithm>
#include <queue>
#include <map>
#include <cstdlib>
#include <cmath>
#define maxn 100005
#define mod 4894651ll
#define pq priority_queue
#define ll long long
#define forp for(int p=head[pos];p;p=nex[p])if(to[p]^pre and !grey[to[p]])
using namespace std;
struct nd{int id, w;}lis[maxn];
pq<nd> h2[maxn], h3, ans;
int N, col[maxn], size[maxn], list[maxn], deep[maxn], grey[maxn], G,
head[maxn], to[maxn<<1], nex[maxn<<1], w[maxn<<1], f[maxn], tot, cnt, C, Cl,
_head[mod+10], qwq;
ll sumG;
inline bool operator<(nd a, nd b){return a.w<b.w;}
inline void adde(int a, int b){to[++tot]=b;nex[tot]=head[a];head[a]=tot;}
struct hash
{
pq<nd> heap;
int next, x, y, belong, d;
}pool[maxn*20];
inline void hash_ins(int x, int y)
{
int a=((ll)x*N+y)%mod;
pool[++qwq].x=x, pool[qwq].y=y;
pool[qwq].next=_head[a], _head[a]=qwq;
}
inline int hash_find(int x, int y)
{
int a=((ll)x*N+y)%mod;
for(int p=_head[a];;p=pool[p].next)if(pool[p].x==x and pool[p].y==y)return p;
}
int dfs(int pos, int pre)
{
size[pos]=1;
list[++*list]=pos;
forp deep[to[p]]=deep[pos]+1, size[pos]+=dfs(to[p],pos);
return size[pos];
}
void findG(int pos, int pre, ll sum)
{
if(sum<sumG)sumG=sum, G=pos;
forp findG(to[p],pos,sum+*size-(size[to[p]]<<1));
}
void build(int pos)
{
int i, p, t;
*list=0, deep[pos]=0, *size=dfs(pos,-1);
G=pos;
for(sumG=0, i=1;i<=*list;i++)sumG+=deep[list[i]];
findG(G,-1,sumG);
*list=0, deep[G]=0, dfs(G,-1);
for(i=2;i<=*list;i++)f[list[i]]=G;
for(p=head[G];p;p=nex[p])if(!grey[to[p]])
{
*list=0, dfs(to[p],G);
for(i=1;i<=*list;i++)
{
hash_ins(G,list[i]);
pool[qwq].d=deep[list[i]], pool[qwq].belong=to[p];
}
}
hash_ins(G,G);
t=hash_find(G,G), pool[t].belong=G;
grey[G]=1;
for(p=head[G];p;p=nex[p])if(!grey[to[p]])build(to[p]);
}
inline int read(int x=0)
{
char c=getchar();
while(c<48 or c>57)c=getchar();
while(c>=48 and c<=57)x=(x<<1)+(x<<3)+c-48, c=getchar();
return x;
}
inline void qc(int y)
{
nd t;
pq<nd> &h=h2[y];
while(1)
{
while(h.size() and (pool[hash_find(y,h.top().id)].heap.size()==0 or h.top().w!=pool[hash_find(y,h.top().id)].heap.top().w))
h.pop();
if(h.size()==0)return;
t=h.top();h.pop();
while(h.size() and (pool[hash_find(y,h.top().id)].heap.size()==0 or h.top().w!=pool[hash_find(y,h.top().id)].heap.top().w))
h.pop();
if(h.size()==0){h.push(t);return;}
if(t.id!=h.top().id){h.push(t);return;}
}
}
inline int getans(int y)
{
nd t1=(nd){-1,-1}, t2=(nd){-1,-1};
qc(y);
if(h2[y].size())t1=h2[y].top(),h2[y].pop();
qc(y);
if(h2[y].size())t2=h2[y].top(),h2[y].pop();
if(t1.id!=-1)h2[y].push(t1);
if(t2.id!=-1)h2[y].push(t2);
if(t2.id==-1)return -1;
else return t1.w+t2.w;
}
inline void chg(int x)
{
int y, b, t, before, tm, pos;
bool flag;
col[x]^=1;
if(col[x])cnt--; else cnt++;
// tm=clock();
for(y=x;y;y=f[y])
{
b=pool[hash_find(y,x)].belong;
pq<nd> &h=pool[hash_find(y,b)].heap;
if(!col[x])h.push((nd){x,pool[hash_find(y,x)].d});
while(h.size() and col[h.top().id])h.pop();
if(h.size())h2[y].push((nd){b,h.top().w});
while(h2[y].size() and (pool[hash_find(y,h2[y].top().id)].heap.size()==0 or h2[y].top().w!=pool[hash_find(y,h2[y].top().id)].heap.top().w))
h2[y].pop();
if((t=getans(y))!=before)ans.push((nd){y,t}),C+=(int)log2(ans.size());
}
while(ans.size() and getans(ans.top().id)!=ans.top().w)ans.pop();
}
inline void print()
{
if(cnt==0)printf("-1\n");
else if(cnt==1)printf("0\n");
else printf("%d\n",ans.top().w);
}
void init()
{
int a, b, i;
N=read();
for(i=1;i<N;i++)a=read(), b=read(), adde(a,b), adde(b,a);
build(1);
for(i=1;i<=N;i++)col[i]=1;
for(i=1;i<=N;i++)chg(i);
}
int main()
{
init();
int Q=read();
char type[2];
cnt=N;
while(Q--)
{
scanf("%s",type);
if(*type=='G')print();
else chg(read());
}
cnt=0;
return 0;
}
本文介绍了一种使用点分治算法解决特定问题的方法,并通过一个具体题目进行了实践演示。作者详细记录了从初始思路到最终优化的过程,特别是针对哈希链表的应用,有效地解决了卡常数问题。
161

被折叠的 条评论
为什么被折叠?



