結果

問題 No.922 東北きりきざむたん
ユーザー FF256grhyFF256grhy
提出日時 2019-11-08 23:01:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 256 ms / 2,000 ms
コード長 4,384 bytes
コンパイル時間 1,748 ms
コンパイル使用メモリ 175,208 KB
実行使用メモリ 29,568 KB
最終ジャッジ日時 2023-10-13 04:38:07
合計ジャッジ時間 6,711 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
11,496 KB
testcase_01 AC 3 ms
11,496 KB
testcase_02 AC 3 ms
11,556 KB
testcase_03 AC 3 ms
11,480 KB
testcase_04 AC 4 ms
11,700 KB
testcase_05 AC 4 ms
11,536 KB
testcase_06 AC 4 ms
11,588 KB
testcase_07 AC 4 ms
11,592 KB
testcase_08 AC 4 ms
11,760 KB
testcase_09 AC 69 ms
18,072 KB
testcase_10 AC 50 ms
13,364 KB
testcase_11 AC 64 ms
16,796 KB
testcase_12 AC 27 ms
19,032 KB
testcase_13 AC 21 ms
13,308 KB
testcase_14 AC 108 ms
22,108 KB
testcase_15 AC 18 ms
19,844 KB
testcase_16 AC 215 ms
23,720 KB
testcase_17 AC 209 ms
23,732 KB
testcase_18 AC 210 ms
23,840 KB
testcase_19 AC 207 ms
24,236 KB
testcase_20 AC 200 ms
23,584 KB
testcase_21 AC 246 ms
25,632 KB
testcase_22 AC 256 ms
25,344 KB
testcase_23 AC 251 ms
24,300 KB
testcase_24 AC 251 ms
24,296 KB
testcase_25 AC 200 ms
24,460 KB
testcase_26 AC 202 ms
24,216 KB
testcase_27 AC 198 ms
24,084 KB
testcase_28 AC 75 ms
20,644 KB
testcase_29 AC 212 ms
29,568 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 UnionFind {
private:
	int n, s;
	vector<int> t;
	vector<vector<int>> v;
public:
	UnionFind(int arg_n = 0) { init(arg_n); }
	void init(int arg_n) {
		n = s = arg_n;
		t.clear();
		v.clear();
		inc(i, n) { t.PB(i); v.EB(1, i); }
	}
	int get_n() { return n; }
	int size() { return s; }
	int id(int x) { return t.at(x); }
	const vector<vector<int>> & get_v() { return v; }
	bool unite(int x, int y) {
		x = id(x);
		y = id(y);
		if(x == y) { return false; }
		if(v[x].size() < v[y].size()) { swap(x, y); }
		for(auto & e: v[y]) { v[x].PB(e); t[e] = x; }
		v[y].clear();
		s--;
		return true;
	}
	bool same(int x, int y) { return (id(x) == id(y)); }
	const vector<int> & operator[](int x) { return v[id(x)]; }
	friend ostream & operator<<(ostream & os, const UnionFind & uf) {
		inc(i, uf.n) { os << i << ": "; for(auto & e: uf.v[i]) { os << e << " "; } os << "\n"; }
		return os;
	}
};

// ---- ----

const int M = 100000;
LL n, m, q, c[M], s[M];
vector<int> g[M];


// ----

const int N = 100000;
const int B = 17;
int h[N], p[B][N];
void dfs(int v, int pp, int hh) {
	h[v] = hh;
	p[0][v] = pp;
	for(auto && e: g[v]) {
		if(e == pp) { continue; }
		dfs(e, v, h[v] + 1);
	}
}
void pre(const vector<int> & root) {
	RF(e, root) { dfs(e, -1, 0); }
	inc(k, B - 1) {
	inc(i, n) {
		p[k + 1][i] = p[k][p[k][i]];
	}
	}
}
int lca(int a, int b) {
	if(! (h[a] <= h[b])) { swap(a, b); }
	int d = h[b] - h[a];
	inc(k, B) { if(bit(d, k)) { b = p[k][b]; } }
	if(a == b) { return a; }
	
	dec(k, B) {
		int pa = p[k][a];
		int pb = p[k][b];
		if(pa != pb) { a = pa; b = pb; }
	}
	assert(p[0][a] == p[0][b]);
	
	return p[0][a];
}

int dist(int a, int b) {
	int m = lca(a, b);
	return h[a] + h[b] - 2 * h[m];
}

// ----

void dfs1(int v, int p) {
	RF(e, g[v]) {
		if(e == p) { continue; }
		dfs1(e, v);
		c[v] += c[e];
		s[v] += s[e] + c[e];
	}
}

void dfs2(int v, int p, int r, LL & mi) {
	if(p != -1) { s[v] = s[p] + c[r] - 2 * c[v]; }
	setmin(mi, s[v]);
	RF(e, g[v]) {
		if(e == p) { continue; }
		dfs2(e, v, r, mi);
	}
}

int main() {
	cin >> n >> m >> q;
	UnionFind uf(n);
	inc(i, m) {
		int a, b;
		cin >> a >> b;
		a--; b--;
		g[a].PB(b);
		g[b].PB(a);
		uf.unite(a, b);
	}
	
	vector<int> root;
	RF(e, uf.get_v()) {
		if(SI(e) == 0) { continue; }
		root.PB(e[0]);
	}
	
	pre(root);
	
	LL ans = 0;
	inc(i, q) {
		int a, b;
		cin >> a >> b;
		a--; b--;
		if(uf.same(a, b)) {
			ans += dist(a, b);
		} else {
			c[a]++;
			c[b]++;
		}
	}
	
	RF(e, root) { dfs1(e, -1); }
	RF(e, root) {
		LL x = 1e18;
		dfs2(e, -1, e, x);
		ans += x;
	}
	
	cout << ans << endl;
	
	return 0;
}
0