邻接矩阵可以存边,并查集也图的一部分

#include<bits/stdc++.h>
using namespace std;
int const N=1e2+7;
int const M=25e4+7;
int n,m,t;
int e[N][N],f[N];
int find(int x){
    return f[x]==x?x:f[x]=find(f[x]);
}
void merge(int x,int y){
    f[find(x)]=find(y);
}
int main(){
    cin >> n >> m >> t;
    for(int i=1;i<=n;++i) f[i]=i;
    for(int i=1,a,b,w;i<=m;++i){
        cin >> a >> b >> w;
        if(w==1) merge(a,b);
        else e[a][b]=e[b][a]=-1;
    }
    while(t--){
        int a,b;
        cin >> a >> b;
        if(find(a)==find(b)){
            if(e[a][b]!=-1) cout << "No problem\n";
            else cout << "OK but...\n";
        }
        else if(e[a][b]!=-1) cout << "OK\n";
        else cout << "No way\n";
    }
    return 0;
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务