結果
問題 | No.416 旅行会社 |
ユーザー | tottoripaper |
提出日時 | 2016-10-02 00:07:27 |
言語 | C++11 (gcc 11.4.0) |
結果 |
AC
|
実行時間 | 166 ms / 4,000 ms |
コード長 | 2,465 bytes |
コンパイル時間 | 1,539 ms |
コンパイル使用メモリ | 169,704 KB |
実行使用メモリ | 14,208 KB |
最終ジャッジ日時 | 2024-05-08 15:24:56 |
合計ジャッジ時間 | 4,438 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 62 ms
11,732 KB |
testcase_01 | AC | 10 ms
9,472 KB |
testcase_02 | AC | 9 ms
9,472 KB |
testcase_03 | AC | 9 ms
9,600 KB |
testcase_04 | AC | 9 ms
9,600 KB |
testcase_05 | AC | 9 ms
9,472 KB |
testcase_06 | AC | 9 ms
9,600 KB |
testcase_07 | AC | 9 ms
9,600 KB |
testcase_08 | AC | 11 ms
9,600 KB |
testcase_09 | AC | 17 ms
9,856 KB |
testcase_10 | AC | 65 ms
11,860 KB |
testcase_11 | AC | 70 ms
11,860 KB |
testcase_12 | AC | 72 ms
11,984 KB |
testcase_13 | AC | 63 ms
11,856 KB |
testcase_14 | AC | 166 ms
13,824 KB |
testcase_15 | AC | 159 ms
14,208 KB |
testcase_16 | AC | 153 ms
13,964 KB |
testcase_17 | AC | 155 ms
13,972 KB |
testcase_18 | AC | 159 ms
13,824 KB |
testcase_19 | AC | 102 ms
12,672 KB |
testcase_20 | AC | 105 ms
12,756 KB |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:62:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 62 | scanf("%d %d %d", &N, &M, &Q); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~ main.cpp:65:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 65 | scanf("%d %d", &fst(bridges[i]), &snd(bridges[i])); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ main.cpp:73:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 73 | scanf("%d %d", &fst(brokenBridges[i]), &snd(brokenBridges[i])); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ソースコード
#include <bits/stdc++.h> using namespace std; #define fst(t) std::get<0>(t) #define snd(t) std::get<1>(t) #define thd(t) std::get<2>(t) using ll = long long; using P = std::tuple<int,int>; const int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1}, dy[8] = {0, 0, -1, 1, -1, 1, -1, 1}; int N, M, Q; P bridges[200000], brokenBridges[200000]; bool isBroken[200000]; int res[100000]; template <int n> struct UnionFind2{ UnionFind2(){ init(); } void init(){ for(int i=0;i<n;++i){ groups[i].clear(); groups[i].emplace_back(i); members[i] = i; } } void unite(int u, int v){ u = members[u]; v = members[v]; if(u == v){return;} if(groups[u].size() > groups[v].size()){ std::swap(u, v); } for(int w : groups[u]){ members[w] = v; } groups[v].insert(groups[v].end(), groups[u].begin(), groups[u].end()); groups[u].clear(); } bool same(int u, int v){ return members[u] == members[v]; } std::vector<int> groups[n]; // group number of each member int members[n]; }; UnionFind2<100000> uf; int main(){ //* std::cin.tie(nullptr); std::ios::sync_with_stdio(false); /*/ /*/ scanf("%d %d %d", &N, &M, &Q); for(int i=0;i<M;++i){ scanf("%d %d", &fst(bridges[i]), &snd(bridges[i])); --fst(bridges[i]); --snd(bridges[i]); } sort(bridges, bridges+M); for(int i=0;i<Q;++i){ scanf("%d %d", &fst(brokenBridges[i]), &snd(brokenBridges[i])); --fst(brokenBridges[i]); --snd(brokenBridges[i]); auto it = lower_bound(bridges, bridges+M, brokenBridges[i]); if(it != bridges + M && *it == brokenBridges[i]){ isBroken[it-bridges] = true; } } for(int i=0;i<M;++i){ if(!isBroken[i]){ uf.unite(fst(bridges[i]), snd(bridges[i])); } } for(int i=0;i<N;++i){ if(uf.same(0, i)){ res[i] = -1; } } for(int i=Q-1;i>=0;--i){ int u, v; tie(u, v) = brokenBridges[i]; if(!uf.same(0, v)){swap(u, v);} if(!uf.same(0, u) && uf.same(0, v)){ for(int w : uf.groups[uf.members[u]]){ res[w] = i + 1; } } uf.unite(u, v); } for(int i=1;i<N;++i){ printf("%d\n", res[i]); } }