結果

問題 No.922 東北きりきざむたん
ユーザー FF256grhy
提出日時 2019-11-09 12:56:36
言語 C++14
(gcc 8.3.0)
結果
AC  
実行時間 343 ms
コード長 4,393 Byte
コンパイル時間 1,587 ms
使用メモリ 28,224 KB
最終ジャッジ日時 2019-11-09 12:56:44

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
0_sample_case1.txt AC 3 ms
6,872 KB
0_sample_case2.txt AC 3 ms
8,924 KB
0_sample_case3.txt AC 3 ms
8,916 KB
0_sample_case4.txt AC 3 ms
6,876 KB
1_small_case1.txt AC 3 ms
8,916 KB
1_small_case2.txt AC 4 ms
6,876 KB
1_small_case3.txt AC 4 ms
6,876 KB
1_small_case4.txt AC 4 ms
6,876 KB
1_small_case5.txt AC 4 ms
6,876 KB
2_random_case1.txt AC 90 ms
12,384 KB
2_random_case2.txt AC 69 ms
6,872 KB
2_random_case3.txt AC 87 ms
9,744 KB
2_random_case4.txt AC 39 ms
13,440 KB
2_random_case5.txt AC 28 ms
6,876 KB
2_random_case6.txt AC 143 ms
18,720 KB
2_random_case7.txt AC 26 ms
14,232 KB
3_0_hand_case1.txt AC 269 ms
19,976 KB
3_0_hand_case2.txt AC 284 ms
19,976 KB
3_0_hand_case3.txt AC 275 ms
19,972 KB
3_0_hand_case4.txt AC 287 ms
20,040 KB
3_0_hand_case5.txt AC 261 ms
20,044 KB
3_1_hand_case1.txt AC 343 ms
20,500 KB
3_1_hand_case2.txt AC 295 ms
20,504 KB
3_2_hand_case1.txt AC 319 ms
20,044 KB
3_2_hand_case2.txt AC 312 ms
20,044 KB
3_3_hand_case1.txt AC 302 ms
20,836 KB
3_3_hand_case2.txt AC 272 ms
20,832 KB
3_3_hand_case3.txt AC 261 ms
20,836 KB
4_corner_case1.txt AC 119 ms
16,872 KB
4_corner_case2.txt AC 281 ms
28,224 KB
テストケース一括ダウンロード

ソースコード

diff #
#include <bits/stdc++.h>
using namespace std;
typedef long long   signed int LL;
typedef long long unsigned int LU;
#define incID(i, l, r) for(LL i = (l)    ; i <  (r); ++i)
#define incII(i, l, r) for(LL i = (l)    ; i <= (r); ++i)
#define decID(i, l, r) for(LL i = (r) - 1; i >= (l); --i)
#define decII(i, l, r) for(LL i = (r)    ; i >= (l); --i)
#define inc(i, n)  incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define dec(i, n)  decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)
#define inID(v, l, r) ((l) <= (v) && (v) <  (r))
#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define  ALL(v)  v.begin(),  v.end()
#define RALL(v) v.rbegin(), v.rend()
template<typename T> bool setmin  (T & a, T b) { if(b <  a) { a = b; return true; } else { return false; } }
template<typename T> bool setmax  (T & a, T b) { if(b >  a) { a = b; return true; } else { return false; } }
template<typename T> bool setmineq(T & a, T b) { if(b <= a) { a = b; return true; } else { return false; } }
template<typename T> bool setmaxeq(T & a, T b) { if(b >= a) { a = b; return true; } else { return false; } }
LL mo(LL a, LL b) { assert(b > 0); a %= b; if(a < 0) { a += b; } return a; }
LL fl(LL a, LL b) { assert(b > 0); return (a > 0 ? a / b : (a - b + 1) / b); }
LL ce(LL a, LL b) { assert(b > 0); return (a < 0 ? a / b : (a + b - 1) / b); }
template<typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define bit(b, i) (((b) >> (i)) & 1)
#define BC __builtin_popcountll
#define SC static_cast
#define SI(v) SC<int>(v.size())
#define SL(v) SC<LL >(v.size())
#define RF(e, v) for(auto & e: v)
#define ef else if
#define UR assert(false)

// ---- ----

class Forest {
private:
	int n, B;
	vector<vector<int>> g, p;
	vector<int> h, f;
	void dfs(int v, int pp, int hh) {
		if(f[v] != 0) { return; }
		f[v] = 1;
		p[v][0] = pp;
		h[v] = hh;
		RF(e, g[v]) { dfs(e, v, hh + 1); }
	}
public:
	Forest(int nn = 0) { init(nn); }
	void init(int nn) {
		n = nn;
		B = 0;
		while((1 << B) < n) { B++; }
		g = vector<vector<int>>(n);
		p = vector<vector<int>>(n, vector<int>(B + 1));
		h = vector<int>(n);
		f = vector<int>(n);
	}
	void add(int a, int b) {
		assert(inID(a, 0, n));
		assert(inID(b, 0, n));
		g[a].PB(b);
		g[b].PB(a);
	}
	void calc(const vector<int> & root = { }) {
		RF(e, root) { dfs(e, e, 0); }
		inc(i, n)   { dfs(i, i, 0); }
		inc(k, B) {
		inc(i, n) {
			p[i][k + 1] = p[p[i][k]][k];
		}
		}
	}
	bool same(int a, int b) {
		assert(inID(a, 0, n));
		assert(inID(b, 0, n));
		return (p[a][B] == p[b][B]);
	}
	int lca(int a, int b) {
		if(! same(a, b)) { return -1; }
		int d = abs(h[a] - h[b]);
		if(h[a] > h[b]) { a = up(a, d); }
		if(h[a] < h[b]) { b = up(b, d); }
		assert(h[a] == h[b]);
		if(a == b) { return a; }
		dec(k, B) {
			if(p[a][k] != p[b][k]) {
				a = p[a][k];
				b = p[b][k];
			}
		}
		assert(a != b && p[a][0] == p[b][0]);
		return p[a][0];
	}
	int dist(int a, int b) {
		if(! same(a, b)) { return -1; }
		return h[a] + h[b] - 2 * h[lca(a, b)];
	}
	int up(int a, int x) {
		assert(inID(a, 0, n));
		if(x > h[a]) { return -1; }
		inc(k, B) { if(bit(x, k) == 1) { a = p[a][k]; } }
		return a;
	}
	const vector<vector<int>> & get_g() { return g; }
	const vector<vector<int>> & get_p() { return p; }
	const vector<int>         & get_h() { return h; }
};

int main() {
	int n, m, q;
	cin >> n >> m >> q;
	
	Forest forest(n);
	inc(i, m) {
		int a, b;
		cin >> a >> b;
		a--; b--;
		forest.add(a, b);
	}
	forest.calc();
	
	vector<LL> c(n), s(n), f(n);
	LL ans = 0;
	inc(i, q) {
		int a, b;
		cin >> a >> b;
		a--; b--;
		if(forest.same(a, b)) { ans += forest.dist(a, b); } else { c[a]++; c[b]++; }
	}
	
	function<void(int, int)> dfs1 = [&](int v, int p) {
		if(f[v] == 1) { return; }
		f[v] = 1;
		RF(e, forest.get_g()[v]) {
			if(e == p) { continue; }
			dfs1(e, v);
			c[v] += c[e];
			s[v] += s[e] + c[e];
		}
	};
	function<LL(int, int, int)> dfs2 = [&](int v, int p, int r) -> LL {
		if(f[v] == 2) { return 0; }
		f[v] = 2;
		if(v != r) { s[v] = s[p] + c[r] - 2 * c[v]; }
		LL mi = s[v];
		RF(e, forest.get_g()[v]) {
			if(e == p) { continue; }
			setmin(mi, dfs2(e, v, r));
		}
		return mi;
	};
	inc(i, n) { dfs1(i, i); }
	inc(i, n) { ans += dfs2(i, i, i); }
	
	cout << ans << endl;
	
	return 0;
}
0