結果
問題 | No.922 東北きりきざむたん |
ユーザー | chocorusk |
提出日時 | 2019-11-08 21:53:18 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 191 ms / 2,000 ms |
コード長 | 2,612 bytes |
コンパイル時間 | 1,168 ms |
コンパイル使用メモリ | 114,420 KB |
実行使用メモリ | 25,420 KB |
最終ジャッジ日時 | 2023-10-13 03:48:20 |
合計ジャッジ時間 | 5,271 ms |
ジャッジサーバーID (参考情報) |
judge14 / judge13 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 5 ms
14,508 KB |
testcase_01 | AC | 5 ms
14,868 KB |
testcase_02 | AC | 5 ms
14,664 KB |
testcase_03 | AC | 5 ms
14,652 KB |
testcase_04 | AC | 5 ms
14,704 KB |
testcase_05 | AC | 5 ms
14,632 KB |
testcase_06 | AC | 5 ms
14,700 KB |
testcase_07 | AC | 5 ms
14,840 KB |
testcase_08 | AC | 5 ms
14,584 KB |
testcase_09 | AC | 53 ms
18,428 KB |
testcase_10 | AC | 52 ms
16,104 KB |
testcase_11 | AC | 57 ms
17,388 KB |
testcase_12 | AC | 21 ms
18,660 KB |
testcase_13 | AC | 20 ms
15,888 KB |
testcase_14 | AC | 83 ms
20,404 KB |
testcase_15 | AC | 15 ms
18,844 KB |
testcase_16 | AC | 156 ms
20,260 KB |
testcase_17 | AC | 156 ms
20,308 KB |
testcase_18 | AC | 156 ms
20,300 KB |
testcase_19 | AC | 155 ms
20,384 KB |
testcase_20 | AC | 151 ms
20,264 KB |
testcase_21 | AC | 188 ms
20,628 KB |
testcase_22 | AC | 191 ms
20,548 KB |
testcase_23 | AC | 164 ms
19,780 KB |
testcase_24 | AC | 172 ms
19,948 KB |
testcase_25 | AC | 147 ms
20,796 KB |
testcase_26 | AC | 151 ms
20,584 KB |
testcase_27 | AC | 147 ms
20,852 KB |
testcase_28 | AC | 68 ms
20,172 KB |
testcase_29 | AC | 154 ms
25,420 KB |
ソースコード
#include <cstdio> #include <cstring> #include <iostream> #include <string> #include <cmath> #include <bitset> #include <vector> #include <map> #include <set> #include <queue> #include <deque> #include <algorithm> #include <complex> #include <unordered_map> #include <unordered_set> #include <random> #include <cassert> #include <fstream> #include <utility> #include <functional> #include <time.h> #include <stack> #include <array> #define popcount __builtin_popcount using namespace std; typedef long long int ll; typedef pair<ll, ll> P; vector<int> g[100010]; vector<int> w[100010]; int par[17][100010]; int comp[100010]; int c; int n; int r[100010]; int d[100010]; void dfs0(int x, int p){ comp[x]=c; w[c].push_back(x); for(auto y:g[x]){ if(p==y) continue; par[0][y]=x; d[y]=d[x]+1; dfs0(y, x); } } void build(){ for(int i=1; i<17; i++){ for(int j=0; j<n; j++){ par[i][j]=par[i-1][par[i-1][j]]; } } } int lca(int a, int b){ if(d[a]>d[b]) swap(a, b); int dd=d[b]-d[a], i=0; int a1=a, b1=b; while(dd){ if(dd&1) b1=par[i][b1]; dd>>=1; i++; } if(a1==b1) return a1; for(int j=17-1; j>=0; j--){ if(par[j][a1]!=par[j][b1]){ a1=par[j][a1], b1=par[j][b1]; } } return par[0][a1]; } int dist(int a, int b){ return d[a]+d[b]-2*d[lca(a, b)]; } int cnt[100010]; void dfs1(int x, int p){ for(auto y:g[x]){ if(y==p) continue; dfs1(y, x); cnt[x]+=cnt[y]; } } int cent[100010]; int main() { int m, q; cin>>n>>m>>q; for(int i=0; i<m; i++){ int u, v; cin>>u>>v; u--; v--; g[u].push_back(v); g[v].push_back(u); } int a[100010], b[100010]; for(int i=0; i<q; i++){ cin>>a[i]>>b[i]; a[i]--; b[i]--; } fill(comp, comp+n, -1); for(int i=0; i<n; i++){ if(comp[i]==-1){ r[c]=i; par[0][i]=i; dfs0(i, -1); c++; } } build(); ll ans=0; for(int i=0; i<q; i++){ if(comp[a[i]]==comp[b[i]]){ ans+=dist(a[i], b[i]); }else{ cnt[a[i]]++; cnt[b[i]]++; } } for(int i=0; i<c; i++){ dfs1(r[i], -1); int tot=cnt[r[i]]; for(auto x:w[i]){ bool dame=0; for(auto y:g[x]){ if(x!=r[i] && par[0][x]==y){ if(tot-cnt[x]>tot/2){ dame=1; break; } }else{ if(cnt[y]>tot/2){ dame=1; break; } } } if(!dame){ cent[i]=x; break; } } } for(int i=0; i<q; i++){ if(comp[a[i]]!=comp[b[i]]){ ans+=dist(cent[comp[a[i]]], a[i])+dist(cent[comp[b[i]]], b[i]); } } cout<<ans<<endl; return 0; }