結果
| 問題 |
No.416 旅行会社
|
| コンテスト | |
| ユーザー |
rsk0315
|
| 提出日時 | 2019-03-17 20:10:09 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,940 bytes |
| コンパイル時間 | 1,024 ms |
| コンパイル使用メモリ | 75,568 KB |
| 最終ジャッジ日時 | 2025-01-06 22:35:54 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 13 WA * 8 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:49:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
49 | scanf("%zu %zu %zu", &N, &M, &Q);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:55:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
55 | scanf("%zu %zu", &p.first, &p.second);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:63:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
63 | scanf("%zu %zu", &p.first, &p.second);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ソースコード
#include <cstdio>
#include <cstdint>
#include <cassert>
#include <vector>
#include <utility>
#include <algorithm>
#include <set>
#include <tuple>
#include <numeric>
class quick_find {
std::vector<std::vector<size_t>> groups;
std::vector<size_t> index;
public:
quick_find(size_t n): index(n) {
std::iota(index.begin(), index.end(), 0);
for (size_t i = 0; i < n; ++i)
groups.emplace_back(1, i);
}
bool unite(size_t u, size_t v) {
if (index[u] == index[v]) return false;
if (groups[index[u]].size() > groups[index[v]].size())
std::swap(u, v);
size_t pi = index[u];
for (size_t i: groups[index[u]]) {
index[i] = index[v];
groups[index[v]].push_back(i);
}
groups[pi].clear();
return true;
}
const std::vector<size_t>& components(size_t v) const {
return groups[index[v]];
}
bool connected(size_t u, size_t v) const {
return index[u] == index[v];
}
};
int main() {
size_t N, M, Q;
scanf("%zu %zu %zu", &N, &M, &Q);
using edge = std::pair<size_t, size_t>;
std::vector<edge> ab(M);
for (auto& p: ab) {
scanf("%zu %zu", &p.first, &p.second);
--p.first;
--p.second;
}
std::vector<edge> cd(Q);
std::set<edge> es;
for (auto& p: cd) {
scanf("%zu %zu", &p.first, &p.second);
--p.first;
--p.second;
es.emplace(p.first, p.second);
}
quick_find qf(N);
for (const auto& p: ab) {
size_t a, b;
std::tie(a, b) = p;
if (es.count({a, b})) continue;
qf.unite(a, b);
}
std::vector<int> res(N, 0);
for (size_t u: qf.components(0))
res[u] = -1;
for (size_t i = Q; i--;) {
size_t c, d;
std::tie(c, d) = cd[i];
if (qf.connected(c, d)) continue;
size_t u = c;
if (qf.connected(0, u)) u = d;
const auto& cmp = qf.components(u);
for (size_t v: cmp) res[v] = i+1;
qf.unite(c, d);
}
res.erase(res.begin());
for (auto ri: res) printf("%d\n", ri);
}
rsk0315