結果
問題 | No.416 旅行会社 |
ユーザー | tottoripaper |
提出日時 | 2016-10-01 23:52:33 |
言語 | C++11 (gcc 11.4.0) |
結果 |
AC
|
実行時間 | 170 ms / 4,000 ms |
コード長 | 2,838 bytes |
コンパイル時間 | 1,560 ms |
コンパイル使用メモリ | 169,088 KB |
実行使用メモリ | 14,080 KB |
最終ジャッジ日時 | 2024-11-21 12:50:42 |
合計ジャッジ時間 | 4,808 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 65 ms
11,732 KB |
testcase_01 | AC | 4 ms
6,144 KB |
testcase_02 | AC | 3 ms
5,888 KB |
testcase_03 | AC | 4 ms
6,016 KB |
testcase_04 | AC | 3 ms
5,888 KB |
testcase_05 | AC | 3 ms
6,016 KB |
testcase_06 | AC | 4 ms
6,016 KB |
testcase_07 | AC | 4 ms
6,016 KB |
testcase_08 | AC | 5 ms
6,272 KB |
testcase_09 | AC | 12 ms
6,656 KB |
testcase_10 | AC | 72 ms
11,732 KB |
testcase_11 | AC | 71 ms
11,860 KB |
testcase_12 | AC | 74 ms
11,864 KB |
testcase_13 | AC | 64 ms
11,988 KB |
testcase_14 | AC | 168 ms
13,824 KB |
testcase_15 | AC | 167 ms
14,080 KB |
testcase_16 | AC | 170 ms
13,968 KB |
testcase_17 | AC | 165 ms
13,984 KB |
testcase_18 | AC | 169 ms
13,952 KB |
testcase_19 | AC | 113 ms
12,800 KB |
testcase_20 | AC | 112 ms
12,884 KB |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:57:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 57 | scanf("%d %d %d", &N, &M, &Q); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~ main.cpp:62:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 62 | scanf("%d %d", &fst(bridges[i]), &snd(bridges[i])); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ main.cpp:70:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 70 | 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]; std::vector<int> groups[100000]; int members[100000]; // group number of each member void init(int n){ for(int i=0;i<n;++i){ groups[i].emplace_back(i); members[i] = i; } } void merge(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; // printf("%d, ", w); } // puts(""); 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]; } int main(){ //* std::cin.tie(nullptr); std::ios::sync_with_stdio(false); /*/ /*/ scanf("%d %d %d", &N, &M, &Q); init(N); 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]){ merge(fst(bridges[i]), snd(bridges[i])); } } for(int i=0;i<M;++i){ if(same(0, i)){ res[i] = -1; } } for(int i=Q-1;i>=0;--i){ int u, v; tie(u, v) = brokenBridges[i]; // printf("Phase: %d, %d, %d\n", i, members[u], members[v]); // for(int i=0;i<N;++i){ // printf("%d: %d\n", i+1, members[i]); // } // puts("----"); // for(int i=0;i<N;++i){ // printf("%d: ", i); // for(int w : groups[i]){ // printf("%d, ", w + 1); // } // puts(""); // } if(!same(0, v)){swap(u, v);} if(!same(0, u) && same(0, v)){ // printf("Added: "); for(int w : groups[members[u]]){ // printf("%d, ", w + 1); res[w] = i + 1; } // puts(""); } // printf("merge: %d, %d\n", u + 1, v + 1); merge(u, v); } for(int i=1;i<N;++i){ printf("%d\n", res[i]); } }