제출 #1365379

#제출 시각아이디문제언어결과실행 시간메모리
1365379weedywelonWind Turbines (EGOI25_windturbines)C++20
13 / 100
215 ms56984 KiB
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <deque>
#include <map>
#include <chrono>
#include <random>
#include <bitset>
#include <tuple>
#define SZ(x) int(x.size())
#define FR(i,a,b) for(int i=(a);i<(b);++i)
#define FOR(i,n) FR(i,0,n)
#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define A first
#define B second
#define mp(a,b) make_pair(a,b)
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef unsigned __int128 U128;
typedef __int128 I128;
typedef std::pair<int,int> PII;
typedef std::pair<LL,LL> PLL;
using namespace std;

const LL MAXN=1e5+5, MAXLG=21;
vector<pair<LL,PLL> > ed;
vector<PLL> adj[MAXN];
LL far[MAXN][MAXLG], d[MAXN];
LL mx[MAXN][MAXLG];

struct dsu{
	vector<LL> p,s;
	LL cost;
	
	void init(LL n){
		p.resize(n);
		s.resize(n);
		cost=0;
		
		FOR(i,n){
			p[i]=i;
			s[i]=1;
		}
	}
	
	LL f(LL x){
		if (p[x]==x) return x;
		p[x]=f(p[x]);
		return p[x];
	}
	
	void un(LL a, LL b, LL c){
		LL a1=a, b1=b;
		a=f(a);
		b=f(b);
		if (a==b) return;
		
		if (s[a]<s[b]) swap(a,b);
		s[a]+=s[b];
		p[b]=a;
		cost+=c;
		
		ed.push_back(mp(c, mp(a1,b1)));
	}
};

void dfs(LL u, LL par){
	for (PLL pp:adj[u]){
		LL v=pp.A, c=pp.B;
		if (v==par) continue;
		
		d[v]=d[u]+1;
		far[v][0]=u;
		mx[v][0]=c;
		dfs(v,u);
	}
}

LL lca(LL x, LL y){
	if (d[x]<d[y]) swap(x,y);
	for (int i=MAXLG-1; i>=0; i--){
		if (d[x]-(1LL<<i)>=d[y]) x=far[x][i];
	}
	if (x==y) return x;
	for (int i=MAXLG-1; i>=0; i--){
		if (far[x][i]!=far[y][i]){
			x=far[x][i];
			y=far[y][i];
		}
	}
	return far[x][0];
}

signed main(){
	FAST;
	LL n,m,q; cin>>n>>m>>q;
	vector<pair<LL,PLL> > e;
	while (m--){
		LL u,v,c; cin>>u>>v>>c;
		e.push_back(mp(c,mp(u,v)));
	}
	sort(e.begin(),e.end());
	
	dsu tree;
	tree.init(n);
	for (auto x:e){
		LL u=x.B.A, v=x.B.B, c=x.A;
		tree.un(u,v,c);
	}
	
	LL base=tree.cost;
	for (auto x:ed){
		LL u=x.B.A, v=x.B.B, c=x.A;
		adj[u].push_back(mp(v,c));
		adj[v].push_back(mp(u,c));
	}
	dfs(0,0);
	
	FR(j,1,MAXLG) FOR(i,n){
		LL p=far[i][j-1];
		far[i][j]=far[p][j-1];
		mx[i][j]=max(mx[p][j-1], mx[i][j-1]);
	}
	
	while (q--){
		LL l,r; cin>>l>>r;
		LL u=lca(l,r);
		//cout<<u<<"\n";
		
		LL ans=0;
		for (int i=MAXLG-1; i>=0; i--){
			if (d[far[l][i]]>=d[u]){
				ans=max(ans, mx[l][i]);
				l=far[l][i];
			}
		}
		for (int i=MAXLG-1; i>=0; i--){
			if (d[far[r][i]]>=d[u]){
				ans=max(ans, mx[r][i]);
				r=far[r][i];
			}
		}
		cout<<base-ans<<"\n";
	}
	return 0;
}
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…