Comments

ok thx~

why unordered_set slower than set......

why does my code get TLE for problem G.

#include <bits/stdc++.h>

using namespace std;

int t;
int n, a, b;





void dfs(int u, int now, int father, vector<vector<pair<int, int>>>& g, unordered_set<int>& st)
{
    if (father != -1)
    {
        st.insert(now);
    }
    for (auto [nxt, w]: g[u])
    {
        if (nxt != father)
        {
            dfs(nxt, now ^ w, u, g, st);
        }
    }
}


bool dfs2(int u, int now, int father, vector<vector<pair<int, int>>>& g, unordered_set<int>& st)
{
    if (st.count(now))
    {
        return true;
    }
    for (auto [nxt, w]: g[u])
    {
        if (nxt != father && nxt != b and dfs2(nxt, now ^ w, u, g, st))
        {
            return true;
        }
    }
    return false;
}



int main()
{
    ios::sync_with_stdio(false);
    cin >> t;
    while (t--)
    {
        cin >> n >> a >> b;
        vector<vector<pair<int, int>>> g(n + 1);
        unordered_set<int> st;
        for (int i = 0; i < n - 1; i++)
        {
            int u, v, e;
            cin >> u >> v >> e;
            g[u].push_back({v, e});
            g[v].push_back({u, e});
        }
        dfs(b, 0, -1, g, st);
        if (dfs2(a, 0, -1, g, st))
        {
            cout << "YES" << endl;
        }
        else
        {
            cout << "NO" << endl;
        }
    }

    return 0;
}