結果
| 問題 |
No.922 東北きりきざむたん
|
| ユーザー |
|
| 提出日時 | 2020-12-28 16:08:40 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 123 ms / 2,000 ms |
| コード長 | 6,804 bytes |
| コンパイル時間 | 2,407 ms |
| コンパイル使用メモリ | 213,528 KB |
| 最終ジャッジ日時 | 2025-01-17 07:54:12 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 26 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using VI = vector<int>;
using VL = vector<ll>;
using VS = vector<string>;
template<class T> using PQ = priority_queue<T, vector<T>, greater<T>>;
#define FOR(i,a,n) for(int i=(a);i<(n);++i)
#define eFOR(i,a,n) for(int i=(a);i<=(n);++i)
#define rFOR(i,a,n) for(int i=(n)-1;i>=(a);--i)
#define erFOR(i,a,n) for(int i=(n);i>=(a);--i)
#define SORT(a) sort(a.begin(),a.end())
#define rSORT(a) sort(a.rbegin(),a.rend())
#define fSORT(a,f) sort(a.begin(),a.end(),f)
#define all(a) a.begin(),a.end()
#define out(y,x) ((y)<0||h<=(y)||(x)<0||w<=(x))
#define tp(a,i) get<i>(a)
#ifdef _DEBUG
#define line cout << "-----------------------------\n"
#define stop system("pause")
#define debug(x) print(x)
#endif
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
constexpr ld eps = 1e-10;
constexpr ld pi = 3.1415926535897932;
template<class T>inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; }return false; }
template<class T>inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; }return false; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }
template<class T>inline istream& operator>>(istream& is, vector<T>& v) { for (auto& a : v)is >> a; return is; }
template<class T, class U>inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template<class T>inline vector<T> vec(size_t a) { return vector<T>(a); }
template<class T>inline vector<T> defvec(T def, size_t a) { return vector<T>(a, def); }
template<class T, class... Ts>inline auto vec(size_t a, Ts... ts) { return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...)); }
template<class T, class... Ts>inline auto defvec(T def, size_t a, Ts... ts) { return vector<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...)); }
template<class T>inline void print(const T& a) { cout << a << "\n"; }
template<class T, class... Ts>inline void print(const T& a, const Ts&... ts) { cout << a << " "; print(ts...); }
template<class T>inline void print(const vector<T>& v) { for (int i = 0; i < v.size(); ++i)cout << v[i] << (i == v.size() - 1 ? "\n" : " "); }
template<class T>inline void print(const vector<vector<T>>& v) { for (auto& a : v)print(a); }
inline string reversed(const string& s) { string t = s; reverse(all(t)); return t; }
template<class T>inline T sum(const vector<T>& a, int l, int r) { return a[r] - (l == 0 ? 0 : a[l - 1]); }
template<class T>inline void END(T s) { print(s); exit(0); }
void END() { exit(0); }
VI dep, kind, cnt;
vector<VI> nxt, g;
int k = 0;
void dfs1(int cur, int par = -1) {
kind[cur] = k;
for (const int& to : g[cur]) {
if (to != par) {
dep[to] = dep[cur] + 1;
nxt[0][to] = cur;
dfs1(to, cur);
}
}
}
int lca(int a, int b) {
if (dep[a] > dep[b])swap(a, b);
int d = dep[b] - dep[a];
FOR(j, 0, 18)if (d & (1 << j))b = nxt[j][b];
if (a == b)return a;
rFOR(j, 0, 18)if (nxt[j][a] != nxt[j][b])
a = nxt[j][a], b = nxt[j][b];
return nxt[0][a];
}
int dis(int a, int b) {
return dep[a] + dep[b] - dep[lca(a, b)] * 2;
}
int dfs2(int cur, int par = -1) {
for (const int& to : g[cur]) {
if (to != par)cnt[cur] += dfs2(to, cur);
}
return cnt[cur];
}
vector<bool> yet;
vector<VI> ver;
void dfs3(ll& res, ll val, int cur, int par = -1) {
yet[cur] = false;
for (const int& to : g[cur]) {
if (to != par) {
ll backup = val;
chmin(res, val += ver[kind[cur]].size() - cnt[to] * 2);
dfs3(res, val, to, cur);
val -= ver[kind[cur]].size() - cnt[to] * 2;
}
}
}
#include <algorithm>
#include <cassert>
#include <vector>
namespace atcoder {
struct dsu {
public:
dsu() : _n(0) {}
dsu(int n) : _n(n), parent_or_size(n, -1) {}
int merge(int a, int b) {
assert(0 <= a && a < _n);
assert(0 <= b && b < _n);
int x = leader(a), y = leader(b);
if (x == y) return x;
if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
parent_or_size[x] += parent_or_size[y];
parent_or_size[y] = x;
return x;
}
bool same(int a, int b) {
assert(0 <= a && a < _n);
assert(0 <= b && b < _n);
return leader(a) == leader(b);
}
int leader(int a) {
assert(0 <= a && a < _n);
if (parent_or_size[a] < 0) return a;
return parent_or_size[a] = leader(parent_or_size[a]);
}
int size(int a) {
assert(0 <= a && a < _n);
return -parent_or_size[leader(a)];
}
std::vector<std::vector<int>> groups() {
std::vector<int> leader_buf(_n), group_size(_n);
for (int i = 0; i < _n; i++) {
leader_buf[i] = leader(i);
group_size[leader_buf[i]]++;
}
std::vector<std::vector<int>> result(_n);
for (int i = 0; i < _n; i++) {
result[i].reserve(group_size[i]);
}
for (int i = 0; i < _n; i++) {
result[leader_buf[i]].push_back(i);
}
result.erase(
std::remove_if(result.begin(), result.end(),
[&](const std::vector<int>& v) { return v.empty(); }),
result.end());
return result;
}
private:
int _n;
std::vector<int> parent_or_size;
};
} // namespace atcoder
int main() {
init();
//input-begin
int n, m, q; cin >> n >> m >> q;
atcoder::dsu uf(n);
g.resize(n);
FOR(i, 0, m) {
int u, v; cin >> u >> v;
uf.merge(--u, --v);
g[u].push_back(v);
g[v].push_back(u);
}
//input-end
//lca-init-begin
dep.resize(n, -1), kind.resize(n);
nxt = defvec<int>(-1, 18, n);
FOR(i, 0, n)if (dep[i] == -1) {
dep[uf.leader(i)] = 0;
dfs1(uf.leader(i));
++k;
}
FOR(j, 0, 17)FOR(i, 0, n)if (nxt[j][i] != -1)
nxt[j + 1][i] = nxt[j][nxt[j][i]];
//lca-init-end
//kueri
cnt.resize(n), ver.resize(k);
ll ans = 0;
while (q--) {
int a, b; cin >> a >> b;
if (uf.same(--a, --b))ans += dis(a, b);
else {
ver[kind[a]].push_back(a);
ver[kind[b]].push_back(b);
++cnt[a], ++cnt[b];
}
}
yet.resize(n, true);
FOR(i, 0, n) {
if (!yet[i])continue;
int rt = uf.leader(i), idx = kind[rt];
ll val = 0;
for (const int& v : ver[idx]) {
val += dis(rt, v);
}
ll res = val;
dfs2(rt);
dfs3(res, val, rt);
ans += res;
}
print(ans);
return 0;
}