题解:P4334 [COI 2007] Policija
wjyppm1403 · · 题解
可能更坏的阅读体验
考虑建广义圆方树。
- 第二问:即判断圆方树两点路径上是否存在点
c ,可以用树剖解决。- 具体的,我们在树刨求
\operatorname{LCA}(x,y) 的时候,跳深度大的点x 的时候,若c 和当前要跳的点x 在一条链上并且深度比x 小,那么yes。否则一直判断,直到x,y 在同一链上的时候(仍令dep_x < dep_y ),判断x,y 和c 是否在一条链上并且dep_c \in [dep_x,dep_Y] 即可,成立为yes,否则为no。
- 具体的,我们在树刨求
- 第一问:首先判断
G_1 \to G_2 是否为割边(对应到圆方树就是点双大小为 2),若是,则判断其对应的方点是否在路径上,问题转化为第二问,不是则答案为yes。
时间复杂度
#include<bits/stdc++.h>
#define pir pair<int,int>
using namespace std;
constexpr int MN=1e6+15;
int n,m,q,ftot;
map<pir,int> mp;
vector<int> adj[MN],G[MN];
namespace YFTarjan{
int dfn[MN],low[MN],stk[MN],tp,dtot;
void init(){
ftot=n;
}
void tarjan(int u,int fa){
low[u]=dfn[u]=++dtot;
stk[++tp]=u;
for(auto v:adj[u]){
if(!dfn[v]){
tarjan(v,u);
low[u]=min(low[u],low[v]);
if(low[v]>=dfn[u]){
ftot++;
if(low[v]>dfn[u]) mp[minmax(u,v)]=ftot;
int x;
do{
x=stk[tp--];
G[ftot].push_back(x);
G[x].push_back(ftot);
}while(x!=v);
G[ftot].push_back(u);
G[u].push_back(ftot);
}
}else if(v!=fa) low[u]=min(low[u],dfn[v]);
}
}
}
namespace Tree{
int fa[MN],dep[MN],siz[MN],hson[MN],htop[MN],dfn[MN],dtot;
void dfs1(int u,int pre){
fa[u]=pre;
dep[u]=dep[pre]+1;
siz[u]=1;
for(auto v:G[u]){
if(v==pre) continue;
dfs1(v,u);
siz[u]+=siz[v];
if(siz[hson[u]]<siz[v]) hson[u]=v;
}
}
void dfs2(int u,int ltop){
htop[u]=ltop;
if(!hson[u]) return;
dfs2(hson[u],ltop);
for(auto v:G[u]){
if(v==fa[u]||v==hson[u]) continue;
dfs2(v,v);
}
}
bool lca(int x,int y,int c){
while(htop[x]!=htop[y]){
if(dep[htop[x]]<dep[htop[y]]) swap(x,y);
if(htop[x]==htop[c]&&dep[c]<=dep[x]) return 1;
x=fa[htop[x]];
}
if(dep[x]<dep[y]) swap(x,y);
if(htop[x]==htop[c]&&dep[c]>=dep[y]&&dep[c]<=dep[x]) return 1;
return 0;
}
}using namespace Tree;
int main(){
cin>>n>>m;
YFTarjan::init();
for(int i=1;i<=m;i++){
int u,v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
YFTarjan::tarjan(1,0);
dfs1(1,0);
dfs2(1,1);
cin>>q;
while(q--){
int op,a,b,c,d;
cin>>op;
if(op==1){
cin>>a>>b>>c>>d;
auto it=mp.find(minmax(c,d));
if(it==mp.end()) cout<<"yes\n";
else cout<<(lca(a,b,it->second)?"no\n":"yes\n");
}else{
cin>>a>>b>>c;
cout<<(lca(a,b,c)?"no\n":"yes\n");
}
}
return 0;
}