結果
| 問題 |
No.922 東北きりきざむたん
|
| コンテスト | |
| ユーザー |
kotamanegi
|
| 提出日時 | 2019-11-08 21:59:01 |
| 言語 | C++17(clang) (17.0.6 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 218 ms / 2,000 ms |
| コード長 | 3,521 bytes |
| コンパイル時間 | 1,417 ms |
| コンパイル使用メモリ | 151,144 KB |
| 実行使用メモリ | 45,056 KB |
| 最終ジャッジ日時 | 2024-11-30 15:36:52 |
| 合計ジャッジ時間 | 6,579 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 26 |
ソースコード
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include<map>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include<fstream>
#include <unordered_map>
#include <cstdlib>
#include <complex>
#include <cctype>
#include <bitset>
using namespace std;
typedef string::const_iterator State;
#define Ma_PI 3.141592653589793
#define eps 1e-5
#define LONG_INF 2000000000000000000LL
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL
#define MOD 998244353LL
#define seg_size 262144
#define REP(a,b) for(long long a = 0;a < b;++a)
vector<int> vertexs[200000];
int union_tree[200000];
int union_find(int now) {
if (union_tree[now] == now) return now;
return union_tree[now] = union_find(union_tree[now]);
}
int union_merge(int a, int b) {
a = union_find(a);
b = union_find(b);
union_tree[a] = b;
return 0;
}
int backing[64][200000];
int depth[200000];
int dfs(int now, int back) {
backing[0][now] = back;
REP(q, vertexs[now].size()) {
int go = vertexs[now][q];
if (go == back) continue;
depth[go] = depth[now] + 1;
dfs(go, now);
}
return 0;
}
long long cnt[200000];
long long calc(int a,int b) {
if (depth[a] > depth[b]) swap(a, b);
long long ans = 0;
int hoge = depth[b] - depth[a];
ans = hoge;
for (int i = 0;hoge != 0; ++i) {
if (hoge % 2 == 1) {
b = backing[i][b];
}
hoge /= 2;
}
while (a != b) {
for (int i = 0;; ++i) {
if (backing[i][a] == backing[i][b]) {
int next_go = max(i - 1, 0);
ans += (1 << (next_go + 1));
a = backing[next_go][a];
b = backing[next_go][b];
break;
}
}
}
return ans;
}
long long calcu(long long now, long long back) {
long long ans = 0;
long long hoge = 0;
REP(q, vertexs[now].size()) {
long long go = vertexs[now][q];
if (go == back) continue;
ans += calcu(go, now);
hoge += cnt[go];
}
ans += hoge;
cnt[now] += hoge;
return ans;
}
long long final_calc(long long now_ans,long long now,long long now_back,long long based) {
long long final_ans = now_ans;
for (int i = 0; i < vertexs[now].size(); ++i) {
int go = vertexs[now][i];
if (go == now_back) continue;
long long next_hoge = now_ans;
next_hoge -= cnt[go];
next_hoge += cnt[based] - cnt[go];
final_ans = min(final_ans, final_calc(next_hoge, go, now, based));
}
return final_ans;
}
int main(){
int n, m, query;
cin >> n >> m >> query;
REP(i, n) {
union_tree[i] = i;
}
REP(i, m) {
int a, b;
cin >> a >> b;
a--; b--;
vertexs[a].push_back(b);
vertexs[b].push_back(a);
union_merge(a, b);
}
REP(i, 200000) {
backing[0][i] = -2;
}
REP(i, n) {
if (backing[0][i] == -2) {
dfs(i, i);
}
}
REP(i, 63) {
REP(q, n) {
backing[i + 1][q] = backing[i][backing[i][q]];
}
}
long long ans = 0;
REP(i, query) {
int a, b;
cin >> a >> b;
a--; b--;
if (union_find(a) == union_find(b)) {
ans += calc(a, b);
}
else {
cnt[a]++;
cnt[b]++;
}
}
//cout << ans << endl;
set<int> gone;
REP(i, n) {
if (gone.find(union_find(i)) != gone.end()) continue;
gone.insert(union_find(i));
int now = i;
long long hoge = calcu(i, -1);
hoge = final_calc(hoge, i, -1, i);
ans += hoge;
}
cout << ans << endl;
}
kotamanegi