Submission #28590989


Source Code Expand

#include <atcoder/all>
using namespace atcoder;
// using mint = modint998244353;
// using mint = modint1000000007;
#include <bits/stdc++.h>
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define rep2(i,k,n) for (int i = (k); i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
// using P = pair<int,int>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;

// const ll INF = (ll)2e18+9;
const int INF = (int)2e9+7;
// const ll MOD = (ll)1e9+9;
template<typename T>
void chmin(T &a, T b) { a = min(a, b); }
template<typename T>
void chmax(T &a, T b) { a = max(a, b); }

template<typename T>
void print(vector<T> v) {
    int n = v.size();
    rep(i,n) {
        if (i == 0) cout << v[i];
        else cout << ' ' << v[i];
    }
    cout << endl;
}

struct Edge {
    ll from, to, cost;
    int type; // type = 0: original, type = 1: query
    int idx;
};

bool operator>(const Edge &a, const Edge &b) {
    return a.cost > b.cost;
}

void solve() {
    int n, m, q;
    cin >> n >> m >> q;

    vector<Edge> edges;
    // 元の辺の候補
    rep(i,m) {
        ll a, b, c;
        cin >> a >> b >> c;
        a--, b--;
        edges.push_back({a, b, c, 0, i});
    }

    // query による辺の候補
    rep(i, q) {
        ll u, v, w;
        cin >> u >> v >> w;
        u--, v--;
        edges.push_back({u, v, w, 1, i});
    }

    vector<vector<Edge>> graph(n);
    for (auto [from, to, cost, type, idx] : edges) {
        graph[from].push_back({from, to, cost, type, idx});
        graph[to].push_back({to, from, cost, type, idx});
    }


    // minimum spanning tree を構築
    vi visited(n, 0), ans(q, 0);
    visited[0] = 1;
    priority_queue<Edge, vector<Edge>, greater<Edge>> pq;
    for (auto edge : graph[0]) pq.push(edge);
    while (pq.size()) {
        auto edge = pq.top(); pq.pop();
        auto [from, to, cost, type, idx] = edge;
        if (visited[to]) continue;
        if (type == 1) {
            ans[idx] = 1;
            continue;
        }
        visited[to] = 1;
        for (auto nx : graph[to]) {
            if (visited[nx.to]) continue;
            pq.push(nx);
        }
    }

    rep(i,q) cout << (ans[i] ? "Yes" : "No") << endl;
}

int main() {
    solve();
    return 0;
}

Submission Info

Submission Time
Task E - MST + 1
User goropikari
Language C++ (GCC 9.2.1)
Score 500
Code Size 2490 Byte
Status AC
Exec Time 812 ms
Memory 78504 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 2
AC × 13
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt
All 00_sample_00.txt, 00_sample_01.txt, 01_tree_00.txt, 01_tree_01.txt, 01_tree_02.txt, 01_tree_03.txt, 01_tree_04.txt, 02_path_00.txt, 02_path_01.txt, 03_random_00.txt, 03_random_01.txt, 03_random_02.txt, 04_perfect_00.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 9 ms 3648 KiB
00_sample_01.txt AC 2 ms 3484 KiB
01_tree_00.txt AC 801 ms 68288 KiB
01_tree_01.txt AC 777 ms 68532 KiB
01_tree_02.txt AC 780 ms 68472 KiB
01_tree_03.txt AC 789 ms 68412 KiB
01_tree_04.txt AC 803 ms 68680 KiB
02_path_00.txt AC 812 ms 69952 KiB
02_path_01.txt AC 794 ms 70020 KiB
03_random_00.txt AC 767 ms 73904 KiB
03_random_01.txt AC 746 ms 74168 KiB
03_random_02.txt AC 736 ms 73972 KiB
04_perfect_00.txt AC 630 ms 78504 KiB