結果
問題 | No.922 東北きりきざむたん |
ユーザー |
|
提出日時 | 2019-09-19 17:12:32 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,835 bytes |
コンパイル時間 | 2,175 ms |
コンパイル使用メモリ | 183,832 KB |
実行使用メモリ | 13,884 KB |
最終ジャッジ日時 | 2024-09-15 05:48:52 |
合計ジャッジ時間 | 7,863 ms |
ジャッジサーバーID (参考情報) |
judge6 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 12 TLE * 1 -- * 13 |
ソースコード
#include <bits/stdc++.h>using namespace std::literals::string_literals;using i64 = long long;using std::cout;using std::endl;using std::cin;template<typename T>std::vector<T> make_v(size_t a){return std::vector<T>(a);}template<typename T,typename... Ts>auto make_v(size_t a,Ts... ts){return std::vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));}int main() {int n, m, q; scanf("%d%d%d", &n, &m, &q);std::vector<std::vector<int>> g(n);for(int i = 0; i < m; i++) {int a, b; scanf("%d%d", &a, &b);g[a - 1].push_back(b - 1);g[b - 1].push_back(a - 1);}int cnt = 0;std::vector<int> comp(n, -1);for(int i = 0; i < n; i++) {if(comp[i] != -1) continue;auto kiri = [&](auto && kiri, int v, int par) -> void {comp[v] = cnt;for(auto e: g[v]) {if(e == par) continue;kiri(kiri, e, v);}};kiri(kiri, i, -1);cnt++;}auto calc = [g](int v, int u) -> i64 {std::queue<int> qu({v});std::vector<int> min_cost(g.size(), 1 << 30); min_cost[v] = 0;while(!qu.empty()) {auto v = qu.front(); qu.pop();for(auto e: g[v]) {if(min_cost[e] <= min_cost[v] + 1) continue;min_cost[e] = min_cost[v] + 1;qu.push(e);}}return min_cost[u];};i64 ans = 0;std::vector<i64> p(n);while(q--) {int a, b; scanf("%d%d", &a, &b); a--; b--;if(comp[a] != comp[b]) {p[a]++;p[b]++;} else {ans += calc(a, b);}}std::vector<i64> latte(cnt, 1LL << 60);for(int i = 0; i < n; i++) {i64 tmp = 0;auto dfs = [&](auto dfs, int v, int par, i64 d) -> void {tmp += p[v] * d;for(auto e: g[v]) {if(e == par) continue;dfs(dfs, e, v, d + 1);}};dfs(dfs, i, -1, 0);latte[comp[i]] = std::min(latte[comp[i]], tmp);}for(auto v: latte) ans += v;printf("%lld\n", ans);return 0;}