QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#799758#8253. Very Important EdgeYarema#WA 180ms18184kbC++202.5kb2024-12-05 17:44:152024-12-05 17:44:22

Judging History

This is the latest submission verdict.

  • [2024-12-05 17:44:22]
  • Judged
  • Verdict: WA
  • Time: 180ms
  • Memory: 18184kb
  • [2024-12-05 17:44:15]
  • Submitted

answer

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef double db;

struct DSU
{
	int n;
	VI p, sz;
	
	DSU(int _n = 0) 
	{
		n = _n;
		p.resize(n);
		iota(ALL(p), 0);
		sz.assign(n, 1);
	}
	int find(int v)
	{
		if (v == p[v])
			return v;
		return p[v] = find(p[v]);
	}
	bool unite(int u, int v)
	{
		u = find(u);
		v = find(v);
		if (u == v) 
			return false;
		if (sz[u] > sz[v])
			swap(u, v);
		p[u] = v;
		sz[v] += sz[u];
		return true;
	}
};


const int N = 100'447;
const int LOG = 18;
const int INF = 1e9 + 7;
vector<PII> g[N];
int tin[N], tout[N];
int h[N];
int T = 0;
int up[N][LOG];
int mn[N][LOG];

void dfs(int v, int p, int d, int w)
{
	h[v] = d;
	tin[v] = T++;
	up[v][0] = p;
	mn[v][0] = w;
	FOR (i, 1, LOG)
	{
		up[v][i] = up[up[v][i - 1]][i - 1];
		mn[v][i] = min(mn[up[v][i - 1]][i - 1], mn[v][i - 1]);
	}
	for (auto [to, c] : g[v])
	{
		if (to != p)
			dfs(to, v, d + 1, c);
	}
	tout[v] = T;
}

bool is_parent(int p, int v)
{
	return tin[p] <= tin[v] && tout[v] <= tout[p];
}
int lca(int u, int v)
{
	if (is_parent(u, v))
		return u;
	RFOR (i, LOG, 0)
	{
		if (!is_parent(up[u][i], v))
			u = up[u][i];
	}
	return up[u][0];
}
int query(int u, int d)
{
	int res = INF;
	FOR (i, 0, LOG)
	{
		if ((1 << i) & d)
		{
			res = min(res, mn[u][i]);
			u = up[u][i];
		}
	}
	return res;
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);

	FOR (i, 0, N) FOR (j, 0, LOG) mn[i][j] = INF;

	int n, m;
	cin >> n >> m;
	
	vector<pair<int, PII>> edges;
	FOR (i, 0, m)
	{
		int a, b, c;
		cin >> a >> b >> c;
		a--, b--;
		edges.PB({c, {a, b}});
	}
	sort(ALL(edges));
	LL ans = 0;
	DSU d(n);
	for (auto [w, e] : edges)
	{
		if (d.find(e.F) != d.find(e.S))
		{
			d.unite(e.F, e.S);
			ans += w;
			g[e.F].PB({e.S, w});
			g[e.S].PB({e.F, w});
		}
	}
	dfs(0, 0, 0, 0);
	LL res = ans;
	for (auto [w, e] : edges)
	{
		int l = lca(e.F, e.S);
		int minEdge = min(query(e.F, h[e.F] - h[l]), query(e.S, h[e.S] - h[l]));
		res = max(res, ans + w - minEdge);
	}
	cout << res << '\n';
		
	
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 180ms
memory: 18184kb

input:

1000 499500
26 715 732723
850 918 507404
276 643 372190
67 702 972051
397 777 337003
178 185 863117
61 151 943008
83 581 731622
99 501 3260
301 588 948638
17 908 147104
193 480 94512
347 415 416562
519 912 627431
70 959 86892
333 573 757685
129 197 181261
224 636 259176
335 426 567962
193 339 70097
...

output:

2112512

result:

wrong answer 1st lines differ - expected: '1121154', found: '2112512'