結果
| 問題 |
No.922 東北きりきざむたん
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-11-08 22:43:09 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 362 ms / 2,000 ms |
| コード長 | 7,089 bytes |
| コンパイル時間 | 2,930 ms |
| コンパイル使用メモリ | 190,260 KB |
| 実行使用メモリ | 35,896 KB |
| 最終ジャッジ日時 | 2024-09-15 01:55:02 |
| 合計ジャッジ時間 | 7,481 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 26 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
const lint mod = 1e9 + 7;
#define all(x) (x).begin(), (x).end()
#define bitcount(n) __builtin_popcountl((lint)(n))
#define fcout cout << fixed << setprecision(15)
#define highest(x) (63 - __builtin_clzl(x))
template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; }
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
template<class T = string, class U = char>int character_count(T text, U character){ int ans = 0; for(U i: text){ ans += (i == character); } return ans; }
lint power(lint base, lint exponent, lint module){ if(exponent % 2){ return power(base, exponent - 1, module) * base % module; }else if(exponent){ lint root_ans = power(base, exponent / 2, module); return root_ans * root_ans % module; }else{ return 1; }}
struct position{ int y, x; }; position mv[4] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}; // double euclidean(position first, position second){ return sqrt((second.x - first.x) * (second.x - first.x) + (second.y - first.y) * (second.y - first.y)); }
template<class T, class U> string to_string(pair<T, U> x){ return to_string(x.first) + "," + to_string(x.second); } string to_string(string x){ return x; }
template<class itr> void array_output(itr start, itr goal){ string ans; for(auto i = start; i != goal; i++) ans += to_string(*i) + " "; if(!ans.empty()) ans.pop_back(); cout << ans << endl; }
template<class itr> void cins(itr first, itr last){ for(auto i = first; i != last; i++){ cin >> (*i); } }
template<class T> T gcd(T a, T b){ if(a && b){ return gcd(min(a, b), max(a, b) % min(a, b)); }else{ return a; }} template<class T> T lcm(T a, T b){ return a / gcd(a, b) * b; }
struct combination{ vector<lint> fact, inv; combination(int sz) : fact(sz + 1), inv(sz + 1){ fact[0] = 1; for(int i = 1; i <= sz; i++){ fact[i] = fact[i - 1] * i % mod; } inv[sz] = power(fact[sz], mod - 2, mod); for(int i = sz - 1; i >= 0; i--){ inv[i] = inv[i + 1] * (i + 1) % mod; } } lint C(int p, int q) const{ if(q < 0 || p < q) return 0; return (fact[p] * inv[q] % mod * inv[p - q] % mod); } };
template<class itr> bool next_sequence(itr first, itr last, int max_bound){ itr now = last; while(now != first){ now--; (*now)++; if((*now) == max_bound){ (*now) = 0; }else{ return true; } } return false; }
template<class itr, class itr2> bool next_sequence2(itr first, itr last, itr2 first2, itr2 last2){ itr now = last; itr2 now2 = last2; while(now != first){ now--, now2--; (*now)++; if((*now) == (*now2)){ (*now) = 0; }else{ return true; } } return false; }
class LCA{
vector<vector<int>> roads;
bool is_built;
int size;
vector<int> depth;
vector<int> parent;
vector<vector<int>> ancestor;
vector<bool> is_came;
vector<int> group_number;
void make_rooted(int vertex, int back, int depth_now, int group_now){
depth[vertex] = depth_now;
is_came[vertex] = true;
group_number[vertex] = group_now;
for(int i: roads[vertex]){
if(i != back){
parent[i] = vertex;
make_rooted(i, vertex, depth_now + 1, group_now);
}
}
}
public:
int find_ancestor(int vertex, int height, int bit = 31){
if(vertex == -1){
return -1;
}else if(bit == -1){
return vertex;
}else if((height >> bit) & 1){
return find_ancestor(ancestor[bit][vertex], height, bit - 1);
}else{
return find_ancestor(vertex, height, bit - 1);
}
}
LCA(int n): size(n), roads(n), is_built(false) {}
void connect(int a, int b){
is_built = false;
roads[a].push_back(b);
roads[b].push_back(a);
}
void build(){
is_built = true;
parent = vector<int>(size);
depth = vector<int>(size);
group_number = vector<int>(size);
is_came = vector<bool>(size, false);
int group_count = 0;
for(int i = 0; i < size; i++){
if(!is_came[i]){
parent[i] = -1;
make_rooted(i, -1, 0, group_count);
group_count++;
}
}
ancestor.resize(32, vector<int>(size));
for(int i = 0; i < size; i++){
ancestor[0][i] = parent[i];
}
for(int i = 1; i < 32; i++){
for(int j = 0; j < size; j++){
if(ancestor[i - 1][j] == -1){
ancestor[i][j] = -1;
}else{
ancestor[i][j] = ancestor[i - 1][ancestor[i - 1][j]];
}
}
}
}
bool is_same_group(int a, int b){
return group_number[a] == group_number[b];
}
int dist(int a, int b){
if(!is_built){
cout << "ERROR : Not built!" << endl;
exit(1);
}
if(group_number[a] != group_number[b]){
return -1;
}
int above = min(depth[a], depth[b]);
int low = -1, high = above;
while(low + 1 < high){
int mid = (low + high) / 2;
if(find_ancestor(a, depth[a] - above + mid) == find_ancestor(b, depth[b] - above + mid)){
high = mid;
}else{
low = mid;
}
}
return high * 2 + max(depth[a], depth[b]) - above;
}
};
int N;
vector<vector<int>> roads;
vector<lint> cnt;
lint sum;
vector<bool> is_came;
lint get_ans(int now, int back, int dist){
lint ans = dist * cnt[now];
for(int i: roads[now]){
if(i == back){
continue;
}
ans += get_ans(i, now, dist + 1);
}
return ans;
}
vector<lint> size;
lint get_size(int now, int back){
lint ans = cnt[now];
for(int i: roads[now]){
if(i == back){
continue;
}
ans += get_size(i, now);
}
return size[now] = ans;
}
lint dfs(int now, int back, lint here_ans){
is_came[now] = true;
lint ans = here_ans;
for(int i: roads[now]){
if(i == back){
continue;
}
ans = min(ans, dfs(i, now, here_ans + sum - size[i] * 2));
}
return ans;
}
int main(){
int M, Q;
cin >> N >> M >> Q;
roads.resize(N);
LCA graph(N);
for(int i = 0; i < M; i++){
int a, b;
cin >> a >> b;
a--, b--;
roads[a].push_back(b);
roads[b].push_back(a);
graph.connect(a, b);
}
cnt.resize(N, 0);
lint ans = 0;
graph.build();
for(int i = 0; i < Q; i++){
int a, b;
cin >> a >> b;
a--, b--;
if(graph.is_same_group(a, b)){
ans += graph.dist(a, b);
}else{
cnt[a]++, cnt[b]++;
}
}
is_came.resize(N, false);
size.resize(N);
for(int i = 0; i < N; i++){
if(!is_came[i] && roads[i].size() <= 1){
sum = get_size(i, -1);
ans += dfs(i, -1, get_ans(i, -1, 0));
}
}
cout << ans << endl;
}