题目
大佬的圆方树博客
正如大佬的圆方树博客所说。
对于一个点双我们新建一个方点然后就可以把图转成树,然后愉快的使用树上科技了。
AC Code:
#include<bits/stdc++.h>
#define maxn 200005
using namespace std;
int n,m,q;
int info[maxn],Prev[maxn],to[maxn],cnt_e;
void Node(int u,int v){ Prev[++cnt_e]=info[u],info[u]=cnt_e,to[cnt_e]=v; }
int w[maxn],tot;
vector<int>G[maxn];
int dfn[maxn],low[maxn],dfsn;
int vis[maxn];
stack<pair<int,int> >sta;
void dfs(int now,int ff){
dfn[now] = low[now] = ++dfsn;
for(int i=info[now];i;i=Prev[i])
if(!dfn[to[i]]){
sta.push(make_pair(now,to[i]));
dfs(to[i],now);
low[now] = min(low[now] , low[to[i]]);
if(low[to[i]] >= dfn[now]){
++tot;
for(;!sta.empty();){
int u=sta.top().first,v=sta.top().second;sta.pop();
if(vis[u] < tot) vis[u] = tot , G[tot].push_back(u) , G[u].push_back(tot);
if(vis[v] < tot) vis[v] = tot , G[tot].push_back(v) , G[v].push_back(tot);
if(u == now && v == to[i]) break;
}
}
}
else low[now]=min(low[now],dfn[to[i]]);
}
int siz[maxn],son[maxn],dep[maxn],fa[maxn],tp[maxn],id[maxn],pos[maxn],cnt;
void dfs2(int now,int ff){
siz[now] = 1 , son[now] = -1 , dep[now] = dep[fa[now] = ff] + 1;
for(int i=0,sz=G[now].size(),v;i<sz;i++)
if((v=G[now][i])!=ff){
dfs2(v,now);
siz[now] += siz[v];
if(son[now] == -1 || siz[son[now]] < siz[v])
son[now] = v;
}
}
void dfs3(int now,int ff){
pos[id[now] = ++cnt] = now;
if(son[now]!=-1) tp[son[now]]=tp[now],dfs3(son[now],now);
for(int i=0,sz=G[now].size(),v;i<sz;i++)
if((v=G[now][i])!=ff && v!=son[now]){
tp[v] = v;
dfs3(v,now);
}
}
multiset<int>st[maxn];
#define lc now << 1
#define rc now << 1 | 1
int Min[maxn<<3];
void upd(int now){ Min[now] = min(Min[lc],Min[rc]); }
void Build(int now,int l,int r){
if(l == r){ Min[now]=(pos[l]<=n?w[pos[l]]:*st[pos[l]].begin());return; }
int mid = (l+r) >> 1;
Build(lc,l,mid),Build(rc,mid+1,r);
upd(now);
}
void Insert(int now,int l,int r,int pos,int val){
if(l == r){ Min[now] = val;return; }
int mid = (l+r) >> 1;
if(pos <= mid) Insert(lc,l,mid,pos,val);
else Insert(rc,mid+1,r,pos,val);
upd(now);
}
int Query(int now,int l,int r,int ql,int qr){
if(l>qr||ql>r) return 0x3f3f3f3f;
if(ql<=l&&r<=qr){
//printf("%d %d %d %d\n",now,l,r,Min[now]);
return Min[now];}
int mid = (l+r) >> 1;
return min(Query(lc,l,mid,ql,qr),Query(rc,mid+1,r,ql,qr));
}
int main(){
//freopen("1.in","r",stdin);
scanf("%d%d%d",&n,&m,&q);tot = n;
for(int i=1;i<=n;i++) scanf("%d",&w[i]);
for(int i=1,u,v;i<=m;i++){
scanf("%d%d",&u,&v);
Node(u,v),Node(v,u);
}
dfs(1,0);
dfs2(1,0);tp[1]=1,dfs3(1,0);
for(int i=1;i<=n;i++)
st[fa[i]].insert(w[i]);
//for(int i=1;i<=n;i++)
// printf("&&%d\n",fa[i]);
Build(1,1,tot);
char s[2];
for(int a,b;q--;){
scanf("%s%d%d",s,&a,&b);
if(s[0] == 'C'){
st[fa[a]].erase(st[fa[a]].find(w[a]));
st[fa[a]].insert(w[a] = b);
Insert(1,1,tot,id[fa[a]],*st[fa[a]].begin());
Insert(1,1,tot,id[a],w[a]);
}
else{
int ans = 0x3f3f3f3f;
//printf("%d %d\n",a,b);
for(;tp[a]!=tp[b];){
if(dep[tp[a]] > dep[tp[b]]) swap(a,b);
ans = min(ans , Query(1,1,tot,id[tp[b]],id[b]));
//printf("!%d\n",ans);
b = fa[tp[b]];
}
if(id[a] > id[b]) swap(a,b);
ans = min(ans , Query(1,1,tot,id[a],id[b]));
//printf("@%d\n",ans);
if(a>n && fa[a])
ans = min(ans , w[fa[a]]);
//printf("$%d\n",ans);
printf("%d\n",ans);
}
}
}