- 題源:王國(guó)——求策 - GYM
- 題意:給定左右各為 \(n\) 個(gè)點(diǎn)的完全二分圖\(^{\dag}\),其中不同屬性的兩點(diǎn)間存在唯一的敵對(duì)關(guān)系點(diǎn)對(duì) \((u,v)\) 。給定源點(diǎn) \(s\) 與終點(diǎn) \(t\),詢問圖中是否存在一條不同時(shí)包含互為敵對(duì)關(guān)系的兩點(diǎn)的路徑,使 \(s\) 到達(dá) \(t\)。
\(\dag\) 完全二分圖:二分圖是指兩種屬性的點(diǎn)位于兩側(cè),且僅不同屬性的點(diǎn)間存在邊的圖;完全二分圖是指對(duì)于每個(gè)點(diǎn),其均與另一屬性的點(diǎn)有且僅存在一條邊的特殊二分圖。
- 關(guān)鍵詞:思維(簽到)
- 題解:不難發(fā)現(xiàn),當(dāng) \(s\) 與 \(t\) 位于異側(cè)時(shí),僅需 \(s\) 不與 \(t\) 互為敵對(duì)關(guān)系即可到達(dá);當(dāng) \(s\) 與 \(t\) 位于同側(cè)時(shí),對(duì)側(cè)點(diǎn)僅有 \(2\) 個(gè)點(diǎn)分別互為敵對(duì)關(guān)系,因此當(dāng) \(n>2\) 時(shí)即一定可達(dá)。需注意 \(s=t\) 的情形,此時(shí)一定可達(dá)。
- 代碼:
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define int ll
#define endl "\n"
void solve(){
int n,s,t;cin>>n>>s>>t;
vector<int>a(n<<1|1);
for(int i=1;i<=n;i++) cin>>a[i],a[a[i]]=i;//注意敵對(duì)關(guān)系為雙向的
if(s==t){
cout<<"Yes"<<endl;
return;
}
if(s<=n&&t<=n||s>n&&t>n){//同側(cè)
if(n>2) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}else{//異側(cè)
if(a[s]!=t&&a[t]!=s) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0);
int t=1;cin>>t;
while(t--) solve();
return 0;
}