結果

問題 No.416 旅行会社
ユーザー konsin_tokagekonsin_tokage
提出日時 2018-06-05 14:37:20
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 578 ms / 4,000 ms
コード長 1,076 bytes
コンパイル時間 777 ms
コンパイル使用メモリ 76,952 KB
実行使用メモリ 14,828 KB
最終ジャッジ日時 2024-05-08 15:39:07
合計ジャッジ時間 7,585 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 287 ms
10,112 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 7 ms
5,376 KB
testcase_09 AC 31 ms
5,376 KB
testcase_10 AC 294 ms
11,168 KB
testcase_11 AC 299 ms
11,036 KB
testcase_12 AC 285 ms
11,160 KB
testcase_13 AC 274 ms
10,752 KB
testcase_14 AC 569 ms
14,828 KB
testcase_15 AC 578 ms
14,700 KB
testcase_16 AC 576 ms
14,700 KB
testcase_17 AC 574 ms
14,700 KB
testcase_18 AC 566 ms
14,824 KB
testcase_19 AC 409 ms
13,892 KB
testcase_20 AC 401 ms
13,764 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <set>
using namespace std;
vector<vector<int>> g;
vector<int> r, s;
void dfs(int i, int x){
    s.push_back(i);
    while(!s.empty()){
        int j = s.back();
        s.pop_back();
        if(r[j]) continue;
        r[j] = x;
        for(int k: g[j]) s.push_back(k);
    }
}
int main(){
    int n, m, q, a, b;
    cin >> n >> m >> q;
    set<pair<int,int>> u;
    for(int i=0; i<m; ++i){
        cin >> a >> b;
        u.emplace(a-1, b-1);
    }
    vector<pair<int,int>> e;
    for(int i=0; i<q; ++i){
        cin >> a >> b;
        u.erase({a-1,b-1});
        e.emplace_back(a-1, b-1);
    }
    g.resize(n);
    for(auto p: u){
        int i = p.first, j = p.second;
        g[i].push_back(j);
        g[j].push_back(i);
    }
    r.resize(n);
    dfs(0, -1);
    while(q--){
        int i = e[q].first, j = e[q].second;
        g[i].push_back(j);
        g[j].push_back(i);
        if(!(r[i]==0&&r[j]==0)){
            dfs(i, q+1);
            dfs(j, q+1);
        }
    }
    for(int i=1; i<n; ++i) cout << r[i] << endl;
}
0