Submission #2272155


Source Code Expand

#include <bits/stdc++.h>
#define ADD(a, b) a = (a + ll(b)) % mod
#define MUL(a, b) a = (a * ll(b)) % mod
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define rep(i, a, b) for(int i = int(a); i < int(b); i++)
#define rer(i, a, b) for(int i = int(a) - 1; i >= int(b); i--)
#define all(a) (a).begin(), (a).end()
#define sz(v) (int)(v).size()
#define pb push_back
#define sec second
#define fst first
#define debug(fmt, ...) Debug(__LINE__, ":", fmt, ##__VA_ARGS__)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<int, pi> ppi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> mat;
void Debug() {cout << '\n'; }
template<class FIRST, class... REST>void Debug(FIRST arg, REST... rest){
	cout<<arg<<" ";Debug(rest...);}
template<class T>ostream& operator<<(ostream& out,const vector<T>& v) {
	out<<"[";if(!v.empty()){rep(i,0,sz(v)-1)out<<v[i]<<", ";out<<v.back();}out<<"]";return out;}
template<class S, class T>ostream& operator<<(ostream& out,const pair<S, T>& v){
	out<<"("<<v.first<<", "<<v.second<<")";return out;}
const int MAX_N = 400010;
const int MAX_V = 100010;
const double eps = 1e-6;
const ll mod = 1000000007;
const int inf = 1 << 29;
const ll linf = 1LL << 60;
const double PI = 3.14159265358979323846;
///////////////////////////////////////////////////////////////////////////////////////////////////

struct UF {
	vector<int> par, ran;
	void init(int n) {
		par.resize(n); ran.resize(n);
		for(int i = 0; i < n; i++) {
			par[i] = i;
			ran[i] = 0;
		}
	}
	UF(int mx = 0) { init(mx); }

	int find(int x) {
		if(par[x] == x) return x;
		else return par[x] = find(par[x]);
	}
	void unite(int x, int y) {
		x = find(x);
		y = find(y);
		if(x == y) return;
		if(ran[x] < ran[y]) {
			par[x] = y;
		}
		else {
			par[y] = x;
			if(ran[x] == ran[y]) ran[x]++;
		}
	}
	bool same(int x, int y) { return find(x) == find(y); }
};

//////////////

int N, M, E;
struct edge { int u, v, cost; };

bool comp(const edge& e1, const edge& e2) {
	return e1.cost < e2.cost;
}

edge es[400010];
vector<pi> G[MAX_N];
int depth[MAX_N];
int par[MAX_N];
int cost[MAX_N];

int kruskal() {
	sort(es, es + E, comp);
	UF uf(N);//init union_find
	int res = 0;
	for(int i = 0; i < E; i++) {
		edge e = es[i];
		if(!uf.same(e.u, e.v)) {
			uf.unite(e.u, e.v);
			G[e.u].pb(pi(e.v, e.cost));
			G[e.v].pb(pi(e.u, e.cost));
			res += e.cost;
		}
	}
	return res;
}

void add_edge(int s, int t, int cost) {
	es[E++] = edge{s, t, cost};
}

void loop(int v, int p, int k) {
	depth[v] = k;
	par[v] = p;
	rep(i, 0, sz(G[v])) {
		int n = G[v][i].fst;
		if(n == p) continue;
		cost[n] = G[v][i].sec;
		loop(n, v, k + 1);
	}
}

void solve() {
	E = 0;
	cin >> N >> M;
	rep(i, 0, M) {
		int a, b, c;
		cin >> a >> b >> c; a--; b--;
		add_edge(a, b, c);
	}
	ll S = kruskal();
	loop(0, -1, 0);
	int Q; cin >> Q;
	while(Q--) {
		int a, b; cin >> a >> b; a--; b--;
		if(depth[a] < depth[b]) swap(a, b);
		int mv = -1;
		while(depth[a] > depth[b]) {
			MAX(mv, cost[a]);
			a = par[a];
		}
		// debug(depth[a], depth[b], a, b);
		while(a != b) {
			MAX(mv, cost[a]);
			MAX(mv, cost[b]);
			a = par[a]; b = par[b];
		}
		cout << S - mv << "\n";
	}
}

int main() {
#ifndef LOCAL
	ios::sync_with_stdio(false);
    cin.tie(0);
#endif
    cout << fixed;
	cout.precision(20);
	srand((unsigned int)time(NULL));
#ifdef LOCAL
	//freopen("in.txt", "wt", stdout); //for tester
    freopen("in.txt", "rt", stdin);
#endif	
	solve();
#ifdef LOCAL
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
	return 0;
}

Submission Info

Submission Time
Task A - Graph
User omochana2
Language C++14 (GCC 5.4.1)
Score 0
Code Size 3783 Byte
Status WA
Exec Time 180 ms
Memory 18176 KB

Judge Result

Set Name Sample subtask1 subtask2 All
Score / Max Score 0 / 0 0 / 200 0 / 300 0 / 200
Status
AC × 2
AC × 2
WA × 10
AC × 3
WA × 18
AC × 5
WA × 26
Set Name Test Cases
Sample sample_1.txt, sample_2.txt
subtask1 sample_2.txt, subtask_1_1.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_2.txt, subtask_1_3.txt, subtask_1_4.txt, subtask_1_5.txt, subtask_1_6.txt, subtask_1_7.txt, subtask_1_8.txt, subtask_1_9.txt
subtask2 sample_1.txt, sample_2.txt, subtask_1_1.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_2.txt, subtask_1_3.txt, subtask_1_4.txt, subtask_1_5.txt, subtask_1_6.txt, subtask_1_7.txt, subtask_1_8.txt, subtask_1_9.txt, subtask_2_1.txt, subtask_2_2.txt, subtask_2_3.txt, subtask_2_4.txt, subtask_2_5.txt, subtask_2_6.txt, subtask_2_7.txt, subtask_2_8.txt
All sample_1.txt, sample_2.txt, sample_1.txt, sample_2.txt, subtask_1_1.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_2.txt, subtask_1_3.txt, subtask_1_4.txt, subtask_1_5.txt, subtask_1_6.txt, subtask_1_7.txt, subtask_1_8.txt, subtask_1_9.txt, subtask_2_1.txt, subtask_2_2.txt, subtask_2_3.txt, subtask_2_4.txt, subtask_2_5.txt, subtask_2_6.txt, subtask_2_7.txt, subtask_2_8.txt, subtask_3_1.txt, subtask_3_2.txt, subtask_3_3.txt, subtask_3_4.txt, subtask_3_5.txt, subtask_3_6.txt, subtask_3_7.txt, subtask_3_8.txt
Case Name Status Exec Time Memory
sample_1.txt AC 4 ms 12544 KB
sample_2.txt AC 4 ms 12544 KB
subtask_1_1.txt WA 5 ms 12544 KB
subtask_1_10.txt WA 74 ms 16896 KB
subtask_1_11.txt AC 4 ms 12544 KB
subtask_1_2.txt WA 18 ms 14848 KB
subtask_1_3.txt WA 144 ms 17152 KB
subtask_1_4.txt WA 6 ms 12800 KB
subtask_1_5.txt WA 6 ms 12800 KB
subtask_1_6.txt WA 39 ms 14848 KB
subtask_1_7.txt WA 143 ms 17152 KB
subtask_1_8.txt WA 6 ms 12800 KB
subtask_1_9.txt WA 8 ms 12800 KB
subtask_2_1.txt WA 145 ms 17280 KB
subtask_2_2.txt WA 145 ms 17280 KB
subtask_2_3.txt WA 145 ms 17280 KB
subtask_2_4.txt WA 145 ms 17280 KB
subtask_2_5.txt WA 7 ms 12800 KB
subtask_2_6.txt WA 13 ms 12800 KB
subtask_2_7.txt WA 40 ms 14848 KB
subtask_2_8.txt WA 144 ms 17280 KB
subtask_3_1.txt WA 178 ms 18176 KB
subtask_3_2.txt WA 178 ms 18176 KB
subtask_3_3.txt WA 35 ms 13952 KB
subtask_3_4.txt WA 41 ms 13952 KB
subtask_3_5.txt WA 108 ms 17920 KB
subtask_3_6.txt WA 145 ms 17920 KB
subtask_3_7.txt WA 180 ms 18048 KB
subtask_3_8.txt WA 178 ms 18048 KB