标签:des blog http io os ar java for strong
Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 366 Accepted Submission(s): 163
思路:lct,就是有点繁琐。。==
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<map>
#include<ctime>
#include<bitset>
#define LL long long
#define INF 89898989
#define mod 20140518
#define maxn 100010
using namespace std;
int head[maxn],next1[maxn*2],to[maxn*2] ;
int top ,n ;
bool vi[maxn] ;
void Unit(int u,int v )
{
next1[top] = head[u] ;to[top] = v ;
head[u] = top++;
}
struct LCT
{
int w[maxn],add[maxn],Max1[maxn];
int ch[maxn][2],pre[maxn] ;
int set1[maxn],Max2[maxn];
int num1[maxn],num2[maxn];
int size1[maxn];
int rev[maxn] ;
void init()
{
memset(rev,0,sizeof(rev)) ;
memset(ch,0,sizeof(ch)) ;
memset(add,0,sizeof(add));
memset(pre,0,sizeof(pre)) ;
memset(size1,0,sizeof(size1));
memset(num2,1,sizeof(num2));
}
void update(int x )
{
size1[x] = size1[ch[x][1]]+1+size1[ch[x][0]];
Max1[x] = w[x] ;
num1[x]=1;
num2[x]=1;
Max2[x] = -INF;
int ls=ch[x][0],rs=ch[x][1];
hehe(Max1[x],num1[x],Max2[x],num2[x],ls) ;
hehe(Max1[x],num1[x],Max2[x],num2[x],rs) ;
}
void Add(int x ,int val)
{
if(x)
{
add[x] += val ;
w[x] += val ;
Max1[x] += val ;
if(Max2[x] != -INF)Max2[x] += val ;
}
}
void Set(int x,int val)
{
if(x)
{
add[x] = 0 ;
Max1[x] = val ;
num1[x] = size1[x];
Max2[x] = -INF ;
num2[x] = 1;
set1[x] = val;
w[x] = val ;
}
}
void down(int x )
{
if(set1[x] != INF)
{
Set(ch[x][0],set1[x]) ;
Set(ch[x][1],set1[x]) ;
set1[x] = INF;
}
if(add[x])
{
Add(ch[x][0],add[x]) ;
Add(ch[x][1],add[x]) ;
add[x]=0;
}
if(rev[x])
{
swap(ch[x][0],ch[x][1]) ;
rev[ch[x][0]] ^= 1;
rev[ch[x][1]] ^= 1;
rev[x] = 0;
}
}
bool isroot(int x )
{
if(ch[pre[x]][0] != x && ch[pre[x]][1] != x )
return true ;
return false;
}
void pushdown(int x )
{
if(!isroot(x)) pushdown(pre[x]) ;
down(x) ;
}
void rotate(int x,int f)
{
int y = pre[x],z = pre[y];
ch[y][!f] = ch[x][f];
pre[ ch[x][f] ] = y;
pre[x] = pre[y];
if(ch[z][0] == y)ch[z][0] = x;
else if(ch[z][1] == y)ch[z][1] = x;
pre[y] = x;
ch[x][f] = y;
update(y);
}
void splay(int x)
{
pushdown(x);
while(!isroot(x))
{
if(isroot(pre[x]))rotate(x,ch[pre[x]][0] == x);
else
{
int y = pre[x],z = pre[y];
int f = (ch[z][0] == y);
if(ch[y][f] == x)rotate(x,!f),rotate(x,f);
else rotate(y,f),rotate(x,f);
}
}
update(x);
}
void access(int u)
{
for(int f = 0 ; u ;u = pre[u])
{
splay(u);
ch[u][1] = f ;
update(u);
f = u ;
}
}
bool check(int a ,int b)
{
int x =a,y=b;
access(y) ;
for(int f = 0 ;x ;x = pre[x])
{
splay(x) ;
if(!pre[x]) break ;
f = x ;
}
for( ; ch[x][1] ; x = ch[x][1]);
return x == b ;
}
void make_root(int x )
{
access(x) ;
splay(x) ;
rev[x] ^= 1;
}
void cut(int x,int y )
{
make_root(x) ;
splay(y) ;
pre[ch[y][0]] = pre[y] ;
pre[y] = ch[y][0] = 0 ;
}
void hehe(int &m1,int &n1,int &m2,int &n2,int x )
{
if(!x) return ;
if(Max1[x]==m1)
{
n1 += num1[x] ;
if(Max2[x] > m2)
{
m2 = Max2[x] ;
n2 = num2[x] ;
}
else if(Max2[x]==m2)
{
n2 += num2[x] ;
}
}
else if(Max1[x] > m1)
{
if(m1 == Max2[x])
{
m2 = m1;
n2 = n1+num2[x] ;
}
else if(m1 > Max2[x])
{
m2 = m1;
n2 = n1;
}
else
{
m2 = Max2[x] ;
n2 = num2[x] ;
}
n1 = num1[x] ;
m1 = Max1[x] ;
}
else
{
if(m2==Max1[x])
{
n2 += num1[x] ;
}
else if(Max1[x] > m2)
{
m2 = Max1[x] ;
n2 = num1[x] ;
}
}
}
int find(int x,int y,int &num)
{
access(y) ;
for( int f = 0 ; x ; x = pre[x])
{
splay(x) ;
if(!pre[x])
{
int n1=1,m1=w[x],m2=-INF,n2=0;
hehe(m1,n1,m2,n2,f) ;
hehe(m1,n1,m2,n2,ch[x][1]) ;
num = n2 ;
return m2;
}
ch[x][1] = f ;
f = x ;
update(x) ;
}
return -1;
}
void add1(int x ,int y ,int add)
{
access(y) ;
for(int f = 0 ; x ; x = pre[x])
{
splay(x) ;
if(!pre[x])
{
w[x] += add ;
Max1[x] += add ;
if(Max2[x] != -INF) Max2[x] += add;
Add(f,add) ;
Add(ch[x][1],add) ;
return ;
}
ch[x][1] = f ;
f = x ;
update(x) ;
}
}
void SET(int x ,int y ,int s)
{
access(y) ;
for(int f = 0 ; x ; x = pre[x])
{
splay(x) ;
if(!pre[x])
{
w[x] = s;
set1[x] = INF;
Max1[x] = s;
num1[x] = size1[x] ;
num2[x] = 1;
Max2[x] = -INF;
Set(f,s) ;
Set(ch[x][1],s) ;
return ;
}
ch[x][1] = f ;
f = x ;
update(x) ;
}
}
void link(int x,int y )
{
make_root(x) ;
splay(x);
pre[x]=y;
}
}lct;
void dfs(int u,int f)
{
for(int i = head[u]; i != -1; i=next1[i])
{
int v = to[i] ;
if(v==f) continue ;
dfs(v,u);
lct.pre[v] = u;
}
}
int next_int() {
char ch;
int res;
bool neg;
while (ch = getchar(), !isdigit(ch) && ch != ‘-‘)
;
if (ch == ‘-‘) {
neg = true;
res = 0;
} else {
neg = false;
res = ch - ‘0‘;
}
while (ch = getchar(), isdigit(ch))
res = res * 10 + ch - ‘0‘;
return neg ? -res : res;
}
int main()
{
int n,m,i,j,k,num;
int T,case1=0,u,v,c ;
// freopen("in.txt","r",stdin);
cin >> T ;
while(T--)
{
scanf("%d%d",&n,&m) ;
lct.init();
for( i = 1 ; i <= n ;i++)
{
lct.w[i] = next_int() ;
lct.set1[i]=INF;
lct.Max2[i]=-INF;
}
top=0;
memset(head,-1,sizeof(head)) ;
for( i = 1 ; i < n ;i++)
{
u = next_int();
v = next_int();
Unit(u,v) ;
Unit(v,u) ;
}
dfs(1,-1);
printf("Case #%d:\n",++case1);
while(m--)
{
//scanf("%d%d%d",&u,&v,&c) ;
scanf("%d",&k) ;
if(k==1)
{
// scanf("%d%d",&u,&v) ;
u = next_int();
v = next_int();
lct.cut(u,v) ;
// scanf("%d%d",&u,&v) ;
u = next_int();
v = next_int();
lct.link(u,v) ;
}
else if(k==2)
{
// scanf("%d%d%d",&u,&v,&c) ;
u = next_int();
v = next_int();
c = next_int();
lct.SET(u,v,c) ;
}
else if(k==3)
{
scanf("%d%d%d",&u,&v,&c) ;
lct.add1(u,v,c) ;
}
else{
// scanf("%d%d",&u,&v) ;
u = next_int();
v = next_int();
j = lct.find(u,v,num) ;
if(j==-INF) puts("ALL SAME");
else printf("%d %d\n",j,num);
}
}
}
return 0 ;
}
标签:des blog http io os ar java for strong
原文地址:http://www.cnblogs.com/20120125llcai/p/4006563.html