Los solution to a problem Valley P2189 [small] Z sensor

This question is to examine the side when built, and looks like the search is not half dime \ (qwq \)

First, there is no room sensor can easily walk around, because we do not consider the order. So consider first of these points of each side to build up.

The next wave of analysis, for the first \ (i \) a room to get the information, we absolutely can not pass before the first \ (i + 1 \) ~ \ (k \) room. So, in addition to these rooms, we will connect all the remaining sides, and then determine the first \ (i-1 \) room whether to \ (i \) room can be.

Analyzing as long as the communication between the room, to maintain the machine by iced tea is better.

\(Code:\)

#include<bits/stdc++.h>
using namespace std;
inline int read(){
    register int s=0,f=1;
    register char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f*=-1;ch=getchar();}
    while(isdigit(ch))s=(s<<1)+(s<<3)+(ch^48),ch=getchar();
    return s*f;
}
const int max_n=100000+5;
int a[max_n],par[max_n],vis[max_n];//par表示祖先,vis表示哪些点可以连 
vector<int>Next[max_n];
int find(int x){
    if(x==par[x])return x;//找到祖先 
    return par[x]=find(par[x]);//路径压缩 
}
void merge(int x,int y){
    x=find(x),y=find(y);//找祖先 
    if(x==y)return;//同一个祖先?大雾 
    par[y]=x;
}
void add(int x){//找所有和x联通并可以连边的点 
    for(int i=0;i<Next[x].size();i++){
        if(vis[Next[x][i]]==0)merge(x,Next[x][i]);//可以连就连 
    }
}
int main(){
    int n=read(),m=read(),k=read(),T=read();
    while(m--){
        int x=read(),y=read();
        Next[x].push_back(y);
        Next[y].push_back(x);
    }
    while(T--){
        for(int i=1;i<=n;i++)par[i]=i;//冰茶机的初始化qwq 
        for(int i=1;i<=k;i++){
            a[i]=read(),vis[a[i]]=1;//标记这个点不能连 
        }
        for(int i=1;i<=n;i++){
            if(vis[i]==0)add(i);
        }
        vis[a[1]]=0,add(a[1]);
        for(int i=2;i<=k;i++){
            vis[a[i]]=0,add(a[i]);//连上这个点 
            if(find(a[i])!=find(a[i-1])){puts("No");break;}//连不同就凉了 
            if(i==k)puts("Yes"); 
        }
    }
    return 0;
}

So \ (50 \) line will cut off this road purple questionMcCaw

Guess you like

Origin www.cnblogs.com/Agonim/p/12080809.html