记录编号 589112 评测结果 AAAAAAAAAAAAAAAAAAAA
题目名称 不是一道路径查询问题 最终得分 100
用户昵称 Gravatar┭┮﹏┭┮ 是否通过 通过
代码语言 C++ 运行时间 1.511 s
提交时间 2024-07-03 14:35:55 内存使用 71.27 MiB
显示代码纯文本
#include <bits/stdc++.h> 
using namespace std;
#define ll long long
const int N = 5e5+10;


ll read(){
   ll x = 0,f = 1;char c = getchar();
   for(;c < '0' || c > '9';c = getchar())if(c == '-')f = -1;
   for(;c >= '0' && c <= '9';c = getchar())x = (x<<1) + (x<<3) + c-'0';
   return x * f;
}

int n,m,q;ll V;

struct made{
    int x,y;ll z;
}e[N<<2];


bool v[N];
ll s[70],cnt;
struct DSU{
    int fa[N];
    void build(){for(int i = 1;i <= n;i++)fa[i] = i;}
    int find(int x){
        if(x == fa[x])return x;
        return fa[x] = find(fa[x]);
    }
    void merge(int x,int y){
        x = find(x),y = find(y);
        if(x == y)return;
        fa[y] = x;
    }
}d[70];


int main(){
    freopen("Paths.in","r",stdin);
    freopen("Paths.out","w",stdout);
    n = read(),m = read(),q = read(),V = read();
    s[++cnt] = V;
    for(int i = 0;i <= 60;i++)
        if((V & (1ll<<i)) == 0)s[++cnt] = (V - (V & ((1ll<<i)-1))) | (1ll<<i);
    for(int i = 1;i <= m;i++){
        int x = read(),y = read();ll z = read();
        e[i] = {x,y,z};
    }
    for(int i = 1;i <= cnt;i++){
        memset(v,0,sizeof v);
        d[i].build();
        for(int j = 1;j <= m;j++)
            if((e[j].z & s[i]) == s[i])d[i].merge(e[j].x,e[j].y);
    }
    for(int i = 1;i <= q;i++){
        int u = read(),v = read();
        bool f = 0;
        for(int j = 1;j <= cnt;j++)
            if(d[j].find(u) == d[j].find(v)){f = 1;break;}
        if(f)printf("Yes\n");
        else printf("No\n");
    }
    
    
    
    return cerr<<clock()<<"ms"<<endl,0;
    
}