結果

問題 No.922 東北きりきざむたん
ユーザー ningenMeningenMe
提出日時 2021-04-23 02:12:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 179 ms / 2,000 ms
コード長 14,104 bytes
コンパイル時間 3,115 ms
コンパイル使用メモリ 238,448 KB
実行使用メモリ 63,304 KB
最終ジャッジ日時 2024-07-04 06:38:30
合計ジャッジ時間 7,442 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 70 ms
35,524 KB
testcase_10 AC 28 ms
12,504 KB
testcase_11 AC 50 ms
28,080 KB
testcase_12 AC 44 ms
39,724 KB
testcase_13 AC 16 ms
12,504 KB
testcase_14 AC 97 ms
53,960 KB
testcase_15 AC 40 ms
42,988 KB
testcase_16 AC 137 ms
58,144 KB
testcase_17 AC 145 ms
58,248 KB
testcase_18 AC 149 ms
58,276 KB
testcase_19 AC 140 ms
58,244 KB
testcase_20 AC 144 ms
58,156 KB
testcase_21 AC 167 ms
59,536 KB
testcase_22 AC 160 ms
59,520 KB
testcase_23 AC 179 ms
59,104 KB
testcase_24 AC 176 ms
59,236 KB
testcase_25 AC 132 ms
59,512 KB
testcase_26 AC 138 ms
59,540 KB
testcase_27 AC 138 ms
59,408 KB
testcase_28 AC 69 ms
51,276 KB
testcase_29 AC 164 ms
63,304 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using int128  = __int128_t;
using int64   = long long;
using int32   = int;
using uint128 = __uint128_t;
using uint64  = unsigned long long;
using uint32  = unsigned int;

#define ALL(obj) (obj).begin(),(obj).end()
template<class T> using priority_queue_reverse = priority_queue<T,vector<T>,greater<T>>;

constexpr int64 MOD = 1'000'000'000LL + 7; //'
constexpr int64 MOD2 = 998244353;
constexpr int64 HIGHINF = 1'000'000'000'000'000'000LL;
constexpr int64 LOWINF = 1'000'000'000'000'000LL; //'
constexpr long double PI = 3.1415926535897932384626433L;

template <class T> vector<T> multivector(size_t N,T init){return vector<T>(N,init);}
template <class... T> auto multivector(size_t N,T... t){return vector<decltype(multivector(t...))>(N,multivector(t...));}
template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; exit(0);}}
template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {o << "{"; for (auto &x : obj) o << " {" << x.first << " : " << x.second << "}" << ","; o << " }"; return o;}
template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const multiset<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const deque<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;}
template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "{" << obj.first << ", " << obj.second << "}"; return o;}
void print(void) {cout << endl;}
template <class Head> void print(Head&& head) {cout << head;print();}
template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) {cout << head << " ";print(forward<Tail>(tail)...);}
template <class T> void chmax(T& a, const T b){a=max(a,b);}
template <class T> void chmin(T& a, const T b){a=min(a,b);}
vector<string> split(const string &str, const char delemiter) {vector<string> res;stringstream ss(str);string buffer; while( getline(ss, buffer, delemiter) ) res.push_back(buffer); return res;}
inline constexpr int msb(int x) {return x?31-__builtin_clz(x):-1;}
inline constexpr int64 ceil_div(const int64 a,const int64 b) {return (a+(b-1))/b;}// return ceil(a/b)
void YN(bool flg) {cout << (flg ? "YES" : "NO") << endl;}
void Yn(bool flg) {cout << (flg ? "Yes" : "No") << endl;}
void yn(bool flg) {cout << (flg ? "yes" : "no") << endl;}

/*
 * @title UnionFindTree - Union Find 木
 * @docs md/graph/UnionFindTree.md
 */
class UnionFindTree {
	vector<int> parent,maxi,mini;
	inline int root(int n) {
		return (parent[n]<0?n:parent[n] = root(parent[n]));
	}
public:
	UnionFindTree(int N = 1) : parent(N,-1),maxi(N),mini(N){
		iota(maxi.begin(),maxi.end(),0);
		iota(mini.begin(),mini.end(),0);
	}
	inline bool connected(int n, int m) {
		return root(n) == root(m);
	}
	inline void merge(int n, int m) {
		n = root(n);
		m = root(m);
		if (n == m) return;
		if(parent[n]>parent[m]) swap(n, m);
		parent[n] += parent[m];
		parent[m] = n;
		maxi[n] = std::max(maxi[n],maxi[m]);
		mini[n] = std::min(mini[n],mini[m]);
	}
	inline int min(int n) {
		return mini[root(n)];
	}
	inline int max(int n) {
		return maxi[root(n)];
	}
	inline int size(int n){
		return (-parent[root(n)]);
	}
	inline int operator[](int n) {
		return root(n);
	}
	inline void print() {
		for(int i = 0; i < parent.size(); ++i) cout << root(i) << " ";
		cout << endl;
	}
};

/*
 * @title Graph
 * @docs md/graph/Graph.md
 */
template<class T> class Graph{
private:
    const size_t N,H,W;
public:
    vector<vector<pair<size_t,T>>> edges;
    Graph(const size_t N):H(-1),W(-1),N(N), edges(N) {}
    Graph(const size_t H, const size_t W):H(H),W(W),N(H*W), edges(H*W) {}
    inline void make_edge(size_t from, size_t to, T w) {
        edges[from].emplace_back(to,w);
    }
    //{from_y,from_x} -> {to_y,to_x} 
    inline void make_edge(pair<size_t,size_t> from, pair<size_t,size_t> to, T w) {
        make_edge(from.first*W+from.second,to.first*W+to.second,w);
    }
    inline void make_bidirectional_edge(size_t from, size_t to, T w) {
        make_edge(from,to,w);
        make_edge(to,from,w);
    }
    inline void make_bidirectional_edge(pair<size_t,size_t> from, pair<size_t,size_t> to, T w) {
        make_edge(from.first*W+from.second,to.first*W+to.second,w);
        make_edge(to.first*W+to.second,from.first*W+from.second,w);
    }
    inline size_t size(){return N;}
    inline size_t idx(pair<size_t,size_t> yx){return yx.first*W+yx.second;}
};

/*
 * @title Tree - 木
 * @docs md/graph/Tree.md
 */
template<class Operator> class TreeBuilder;
template<class Operator> class Tree {
	using TypeEdge = typename Operator::TypeEdge;
	size_t num;
	size_t ord;
	Graph<TypeEdge>& g;
	friend TreeBuilder<Operator>;
	/**
	 * constructor
	 * O(N) 
	 */
	Tree(Graph<TypeEdge>& graph):
		g(graph),
		num(graph.size()),
		depth(graph.size(),-1),
		order(graph.size()),
		edge_dist(graph.size()){
	}
	//for make_depth
	void dfs(int curr, int prev){
		for(const auto& e:g.edges[curr]){
			const int& next = e.first;
			if(next==prev) continue;
			depth[next] = depth[curr] + 1;
			edge_dist[next]  = Operator::func_edge_merge(edge_dist[curr],e.second);
			dfs(next,curr);
			order[ord++] = next;
		}
	}
	/**
	 * 根付き木を作る
	 * O(N) you can use anytime
	 */
	void make_root(const int root) {
		depth[root] = 0;
		edge_dist[root] = Operator::unit_edge;
		ord = 0;
		dfs(root,-1);
		order[ord++] = root;
		reverse_copy(order.begin(),order.end(),back_inserter(reorder));
	}
	/**
	 * 根付き木を作る
	 * O(N) you can use anytime
	 */
	void make_root() {
        ord = 0;
        for(int i=0;i<num;++i) {
            if(depth[i]!=-1) continue;
            depth[i] = 0;
            edge_dist[i] = Operator::unit_edge;
            dfs(i,-1);
            order[ord++] = i;
        }
		reverse_copy(order.begin(),order.end(),back_inserter(reorder));
	}
	/**
	 * 子を作る
	 * O(N) after make_root
	 */
	void make_child(const int root = 0) {
		child.resize(num);
		for (size_t i = 0; i < num; ++i) for (auto& e : g.edges[i]) if (depth[i] < depth[e.first]) child[i].push_back(e);
	}
	/**
	 * 部分木のサイズを作る
	 * O(N) after make_child
	 */
	void make_subtree_size() {
		subtree_size.resize(num,1);
		for (size_t i:order) for (auto e : child[i]) subtree_size[i] += subtree_size[e.first];
	}
	/**
	 * 親を作る
	 * O(N) after make_root
	 */
	void make_parent() {
		parent.resize(num,make_pair(num,Operator::unit_edge));
		for (size_t i = 0; i < num; ++i) for (auto& e : g.edges[i]) if (depth[i] > depth[e.first]) parent[i] = e;
	}
	void make_ancestor() {
		ancestor.resize(num);
		for (size_t i = 0; i < num; ++i) ancestor[i][0] = (parent[i].first!=num?parent[i]:make_pair(i,Operator::unit_lca_edge));
		for (size_t j = 1; j < Operator::bit; ++j) {
			for (size_t i = 0; i < num; ++i) {
				size_t k = ancestor[i][j - 1].first;
				ancestor[i][j] = Operator::func_lca_edge_merge(ancestor[k][j - 1],ancestor[i][j - 1]);
			}
		}
	}
	pair<size_t,TypeEdge> lca_impl(size_t l, size_t r) {
		if (depth[l] < depth[r]) swap(l, r);
		int diff = depth[l] - depth[r];
		auto ancl = make_pair(l,Operator::unit_lca_edge);
		auto ancr = make_pair(r,Operator::unit_lca_edge);
		for (int j = 0; j < Operator::bit; ++j) {
			if (diff & (1 << j)) ancl = Operator::func_lca_edge_merge(ancestor[ancl.first][j],ancl);
		}
		if(ancl.first==ancr.first) return ancl;
		for (int j = Operator::bit - 1; 0 <= j; --j) {
			if(ancestor[ancl.first][j].first!=ancestor[ancr.first][j].first) {
				ancl = Operator::func_lca_edge_merge(ancestor[ancl.first][j],ancl);
				ancr = Operator::func_lca_edge_merge(ancestor[ancr.first][j],ancr);
			}
		}
		ancl = Operator::func_lca_edge_merge(ancestor[ancl.first][0],ancl);
		ancr = Operator::func_lca_edge_merge(ancestor[ancr.first][0],ancr);
		return Operator::func_lca_edge_merge(ancl,ancr);
	}
	pair<TypeEdge,vector<size_t>> diameter_impl() {
		Tree tree = Tree::builder(g).build();
		size_t root = 0;
		{
			tree.make_root(0);
		}
		root = max_element(tree.edge_dist.begin(),tree.edge_dist.end()) - tree.edge_dist.begin();
		{
			tree.make_root(root);
		}
		size_t leaf = max_element(tree.edge_dist.begin(),tree.edge_dist.end()) - tree.edge_dist.begin();
		TypeEdge sz = tree.edge_dist[leaf];
		vector<size_t> st;
		{
			tree.make_parent();
			while(leaf != root) {
				st.push_back(leaf);
				leaf = tree.parent[leaf].first;
			}
			st.push_back(root);
		}
		return make_pair(sz,st);
	}
	template<class TypeReroot> vector<TypeReroot> rerooting_impl(vector<TypeReroot> rerootdp,vector<TypeReroot> rerootparent) {
		for(size_t pa:order) for(auto& e:child[pa]) rerootdp[pa] = Operator::func_reroot_dp(rerootdp[pa],rerootdp[e.first]);
		for(size_t pa:reorder) {
			if(depth[pa]) rerootdp[pa] = Operator::func_reroot_dp(rerootdp[pa],rerootparent[pa]);
			size_t m = child[pa].size();
			for(int j = 0; j < m && depth[pa]; ++j){
				size_t ch = child[pa][j].first;
				rerootparent[ch] = Operator::func_reroot_dp(rerootparent[ch],rerootparent[pa]);
			}
			if(m <= 1) continue;
			vector<TypeReroot> l(m),r(m);
			for(int j = 0; j < m; ++j) {
				size_t ch = child[pa][j].first;
				l[j] = rerootdp[ch];
				r[j] = rerootdp[ch];
			}
			for(int j = 1; j+1 < m; ++j) l[j] = Operator::func_reroot_merge(l[j],l[j-1]);
			for(int j = m-2; 0 <=j; --j) r[j] = Operator::func_reroot_merge(r[j],r[j+1]);
			size_t chl = child[pa].front().first;
			size_t chr = child[pa].back().first;
			rerootparent[chl] = Operator::func_reroot_dp(rerootparent[chl],r[1]);
			rerootparent[chr] = Operator::func_reroot_dp(rerootparent[chr],l[m-2]);
			for(int j = 1; j+1 < m; ++j) {
				size_t ch = child[pa][j].first;
				rerootparent[ch] = Operator::func_reroot_dp(rerootparent[ch],l[j-1]);
				rerootparent[ch] = Operator::func_reroot_dp(rerootparent[ch],r[j+1]);
			}
		}
		return rerootdp;
	}
public:
	vector<size_t> depth;
	vector<size_t> order;
	vector<size_t> reorder;
	vector<size_t> subtree_size;
	vector<pair<size_t,TypeEdge>> parent;
	vector<vector<pair<size_t,TypeEdge>>> child;
	vector<TypeEdge> edge_dist;
	vector<array<pair<size_t,TypeEdge>,Operator::bit>> ancestor;
 
	/**
	 * O(N) builder
	 */
	static TreeBuilder<Operator> builder(Graph<TypeEdge>& graph) { return TreeBuilder<Operator>(graph);}
	/**
	 * O(logN) after make_ancestor
	 * return {lca,lca_dist} l and r must be connected 
	 */
	pair<size_t,TypeEdge> lca(size_t l, size_t r) {return lca_impl(l,r);}
	/**
	 * O(N) anytime
	 * return {diameter size,diameter set} 
	 */
	pair<TypeEdge,vector<size_t>> diameter(void){return diameter_impl();}
	/**
	 * O(N) after make_child
	 */
	template<class TypeReroot> vector<TypeReroot> rerooting(const vector<TypeReroot>& rerootdp,const vector<TypeReroot>& rerootparent) {return rerooting_impl(rerootdp,rerootparent);}
};
 
template<class Operator> class TreeBuilder {
	bool is_root_made =false;
	bool is_child_made =false;
	bool is_parent_made=false;
public:
	using TypeEdge = typename Operator::TypeEdge;
	TreeBuilder(Graph<TypeEdge>& g):tree(g){}
	TreeBuilder& root(const int rt) { is_root_made=true; tree.make_root(rt); return *this;}
	TreeBuilder& root() { is_root_made=true; tree.make_root(); return *this;}
	TreeBuilder& child() { assert(is_root_made); is_child_made=true;  tree.make_child();  return *this;}
	TreeBuilder& parent() { assert(is_root_made); is_parent_made=true; tree.make_parent(); return *this;}
	TreeBuilder& subtree_size() { assert(is_child_made); tree.make_subtree_size(); return *this;}
	TreeBuilder& ancestor() { assert(is_parent_made); tree.make_ancestor(); return *this;}
	Tree<Operator>&& build() {return move(tree);}
private:
	Tree<Operator> tree;
}; 
template<class T> struct TreeOperator{
	using TypeEdge = T;
	inline static constexpr size_t bit = 20;
	inline static constexpr TypeEdge unit_edge = 0;
	inline static constexpr TypeEdge unit_lca_edge = 0;
	inline static constexpr TypeEdge func_edge_merge(const TypeEdge& parent,const TypeEdge& w){return parent+w;}
	inline static constexpr pair<size_t,TypeEdge> func_lca_edge_merge(const pair<size_t,TypeEdge>& l,const pair<size_t,TypeEdge>& r){return make_pair(l.first,l.second+r.second);}
	template<class TypeReroot> inline static constexpr TypeReroot func_reroot_dp(const TypeReroot& l,const TypeReroot& r) {return {l.first+r.first+r.second,l.second+r.second};}
	template<class TypeReroot> inline static constexpr TypeReroot func_reroot_merge(const TypeReroot& l,const TypeReroot& r) {return {l.first+r.first,l.second+r.second};}
};
//auto tree = Tree<TreeOperator<int>>::builder(g).build();

/**
 * @url 
 * @est
 */ 
int main() {
    cin.tie(0);ios::sync_with_stdio(false);
    int N,M,Q; cin >> N >> M >> Q;
    UnionFindTree uf(N);
    Graph<int64> g(N);
    for(int i=0;i<M;++i) {
        int u,v; cin >> u >> v;
        u--,v--;
		uf.merge(u,v);
        g.make_bidirectional_edge(u,v,1);
    }
    auto tree = Tree<TreeOperator<int64>>::builder(g).root().parent().ancestor().child().build();
	int64 ans = 0;
	vector<pair<int64,int64>> cnt(N,{0,0}),par(N,{0,0});
	for(int i = 0; i < Q; ++i){
        int a,b; cin >> a >> b;
        a--,b--;
        if(uf.connected(a,b)){
			ans += tree.lca(a,b).second;
        }
        else{
            cnt[a].second++;
            cnt[b].second++;
        }
    }
	for(int i = 0; i < N; ++i) if(tree.depth[i]) par[i] = cnt[tree.parent[i].first];
	auto dp = tree.rerooting<pair<int64,int64>>(cnt,par);
	const int64 inf = 1e15;
	vector<int64> sum(N,inf);
	for(int i = 0,j; i < N; ++i) j = uf[i],sum[j] = min(sum[j],dp[i].first);
    for(int i = 0; i < N; ++i) if(sum[i] != inf) ans += sum[i];
	cout << ans << endl;	
    return 0;
}
0