Skip to content
c++
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int N=3e5+5;
int n,m,a,b;
struct Edge{
    int u,v,w;
}e[N<<1];
int head[N],to[N<<1],nxt[N<<1],w[N<<1],tot=1;
int dfn[N],low[N],timer;
int stk[N],top;
int bel[N],ebcc_cnt;
bool val[N];

void add(int u,int v,int z=1){
    nxt[++tot]=head[u];
    head[u]=tot;
    to[tot]=v; 
    w[tot]=z;
}
void tarjan(int u,int e){
    dfn[u]=low[u]=++timer;
    stk[++top]=u;
    for(int i=head[u];i;i=nxt[i]){
        if((i^1)==e)continue;
        int v=to[i];
        if(!dfn[v]){
            tarjan(v,i);
            low[u]=std::min(low[u],low[v]);
        }else if(dfn[v]<dfn[u]){
            low[u]=std::min(low[u],dfn[v]);
        }
    }
    if(low[u]==dfn[u]){
        ++ebcc_cnt;
        do{
            bel[stk[top]]=ebcc_cnt;
        }while(stk[top--]!=u);
    }
}
void cand(){
    tot=0;
    for(int i=1;i<=ebcc_cnt;i++)head[i]=0;

    for(int i=0;i<m;i++){
        int u=bel[e[i].u],v=bel[e[i].v];
        if(u==v){
            if(e[i].w){
                val[u]=true;
            }
        }else{
            add(u,v,e[i].w);
            add(v,u,e[i].w);
        }
    }
}

bool dfs(int u,int fa,bool ok){
    ok|=val[u];
    if(u==bel[b])return ok;
    for(int i=head[u];i;i=nxt[i]){
        int v=to[i];
        if(v!=fa){
            if(dfs(v,u,ok||w[i])){
                return true;
            }
        }
    }
    return false;
}

void fc() {
    std::cin>>n>>m;
    for(int i=0;i<m;i++){
        std::cin>>e[i].u>>e[i].v>>e[i].w;
        add(e[i].u,e[i].v);add(e[i].v,e[i].u);
    }
    tarjan(1,0);
    cand();

    std::cin>>a>>b;
    std::cout<<(dfs(bel[a],0,false)?"YES":"NO");
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    //std::cin>>t;
    while (t--) fc();
    return 0;
}