結果

問題 No.922 東北きりきざむたん
ユーザー kuhakukuhaku
提出日時 2019-11-09 16:42:12
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,328 bytes
コンパイル時間 1,558 ms
コンパイル使用メモリ 169,192 KB
実行使用メモリ 8,464 KB
最終ジャッジ日時 2023-10-13 07:19:52
合計ジャッジ時間 9,096 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,288 KB
testcase_01 AC 3 ms
5,380 KB
testcase_02 AC 4 ms
5,384 KB
testcase_03 AC 3 ms
5,156 KB
testcase_04 AC 3 ms
5,116 KB
testcase_05 AC 3 ms
5,228 KB
testcase_06 AC 3 ms
5,388 KB
testcase_07 AC 3 ms
5,136 KB
testcase_08 AC 4 ms
5,316 KB
testcase_09 AC 73 ms
7,556 KB
testcase_10 TLE -
testcase_11 AC 170 ms
7,232 KB
testcase_12 AC 15 ms
6,604 KB
testcase_13 AC 17 ms
5,960 KB
testcase_14 AC 91 ms
8,464 KB
testcase_15 AC 5 ms
6,408 KB
testcase_16 TLE -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define FOR(i, m, n) for(int (i) = (m); (i) < (n); (i)++)
#define FORN(i, m, n) for(int (i) = (m); (i) <= (n); (i)++)
#define FORR(i, m, n) for(int (i) = (m); (i) >= (n); (i)--)
#define rep(i, n) FOR(i, 0, n)
#define repn(i, n) FORN(i, 1, n)
#define repr(i, n) FORR(i, n, 0)
#define repnr(i, n) FORR(i, n, 1)
#define co(n) cout << (n) << endl
#define cosp(n) cout << (n) << ' '
#define setp(n) cout << fixed << setprecision(n);
#define all(s) (s).begin(), (s).end()
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
typedef long long ll;
typedef pair<int, int> P;

const ll INF = 1e9+1;
const ll LINF = 1e18+1;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const double PI = acos(-1);
const double EPS = 1e-9;
const int MAX_N = 1e5;

vector<vector<int> > tree(MAX_N+1);
int dist[MAX_N+1] = {};
int par[MAX_N+1] = {};
int depth[MAX_N+1] = {};
bool fin[MAX_N+1] = {};

void init(int n){
  repn(i, n) par[i] = i;
}

int root(int x){
	if(par[x] == x) return x;
	else return root(par[x]);
}

bool same_root(int a, int b){
	return root(a) == root(b);
}

void dfs_lca(int n, int p, int d){
	depth[n] = d;
	par[n] = p;
	for(int i : tree[n]){
		if(depth[i] == INF) dfs_lca(i, n, d+1);
	}
}

void build(int n){
	fill_n(depth, n+1, INF);
	repn(i, n) if(par[i] == i) dfs_lca(i, i, 0);
}

int lca(int a, int b){
	if(a == b) return a;
	if(depth[a] > depth[b]) return lca(par[a], b);
	if(depth[a] < depth[b]) return lca(a, par[b]);
	return lca(par[a], par[b]);
}

void dfs_dist(int n, int p, int d){
	for(int i : tree[n]){
		if(i != p){
			dist[i] += d+1;
			dfs_dist(i, n, d+1);
		}
	}
}

void dfs(int n, int &m){
	m = min(m, dist[n]);
	fin[n] = true;
	for(int i : tree[n]){
		if(!fin[i]) dfs(i, m);
	}
}

int main(void){
	int n, m, q;
	cin >> n >> m >> q;
	init(n);

	rep(i, m){
		int a, b;
		cin >> a >> b;
		tree[a].pb(b);
		tree[b].pb(a);
	}

	build(n);
	ll ans = 0;
	rep(i, q){
		int a, b;
		cin >> a >> b;
		if(same_root(a, b)) ans += depth[a]+depth[b]-2*depth[lca(a, b)];
		else{
			dfs_dist(a, 0, 0);
			dfs_dist(b, 0, 0);
		}
	}

	repn(i, n){
		int m = INF;
		if(!fin[i]) dfs(i, m);
		if(m != INF) ans += m;
	}

	co(ans);

	return 0;
}
0