Submission #805727

# Submission time Handle Problem Language Result Execution time Memory
805727 2023-08-03T21:59:56 Z YesPy Cheap flights (LMIO18_pigus_skrydziai) C++17
100 / 100
938 ms 155684 KB
#include <bits/stdc++.h>

#define ff first
#define ss second
#define tcT template<class T
#define fastio ios::sync_with_stdio(false);cin.tie(nullptr);
#define ln '\n'
#define nwln cout<<ln;

using namespace std;

tcT> using vr = vector<T>;
using pi = pair<int, int>;
using vi = vr<int>;
using vpi = vr<pi>;
using ll = long long;
using oo = bool;

#define maxs(i, j) (i = max(i, j))
#define fri(i,a,b) for(int i=(a); i<(b); ++i)
#define each(x, a) for(auto& x: a)

#define sz(x) int((x).size())
#define phb push_back
#define eb emplace_back

const int MX = (int) 3e5+3;
ll N, M, ans;
oo used[MX];
set<int> orz[MX];
vpi adj[MX];

inline void dfs(int u) {
	used[u] = true;
	ll res = 0, xam = 0, U = 0, idx = 0;

	map<int, int> cur;

	each(x, adj[u]) {
		cur[x.ff] = idx++;
		res += x.ss;
		if(xam < x.ss) U = x.ff, xam = x.ss;
	}

	maxs(ans, res);
	// idk if this "if" is necessary
	if(!used[U]) {
		each(x, adj[U]) {
			if(!orz[u].count(x.ff)) continue;
			
			if(cur[x.ff] >= sz(adj[u])) {
				cout<<"why?"<<ln;
				exit(0);
			}

			maxs(ans, xam + x.ss + adj[u][cur[x.ff]].ss);
		}
	}

	each(x, adj[u]) {
		if(!used[x.ff]) dfs(x.ff);
	}
}

int main()
{
	fastio
	cin>>N>>M;

	while(M--) {
		int u, v, w;
		cin>>u>>v>>w;
		adj[u].eb(v, w);
		adj[v].eb(u, w);
		orz[u].insert(v);
		orz[v].insert(u);
	}

	fri(i,1,N+1) {
		if(used[i]) continue;
		dfs(i);
	}

	cout<<ans<<ln;

	return 0;
}

/*
Wow, beautiful trick
Two possible types: star or triangle
Stars are easy, bottleneck are the triangles

What I do is, if I'm on node v then between all its edges I choose the
greatest one and try to match the rest... it can be proved it's correct

Suppose the answer is not correct and let the max-weight triangle be
p, q -> a
q, r -> b
r, p -> c

Then, we're assuming there exists
p, P -> greater than c, a but less than b
q, Q -> greater than a, b but less than c
r, R -> greater than b, c but less than a

Contradiction! We're done :D
*/
# Verdict Execution time Memory Grader output
1 Correct 10 ms 21452 KB Output is correct
2 Correct 9 ms 21452 KB Output is correct
3 Correct 9 ms 21332 KB Output is correct
4 Correct 9 ms 21384 KB Output is correct
5 Correct 9 ms 21460 KB Output is correct
6 Correct 23 ms 25556 KB Output is correct
7 Correct 9 ms 21460 KB Output is correct
8 Correct 9 ms 21460 KB Output is correct
9 Correct 9 ms 21460 KB Output is correct
10 Correct 9 ms 21460 KB Output is correct
11 Correct 11 ms 21504 KB Output is correct
12 Correct 10 ms 21460 KB Output is correct
13 Correct 9 ms 21460 KB Output is correct
14 Correct 10 ms 21332 KB Output is correct
15 Correct 9 ms 21332 KB Output is correct
16 Correct 9 ms 21460 KB Output is correct
17 Correct 9 ms 21460 KB Output is correct
18 Correct 10 ms 21588 KB Output is correct
19 Correct 12 ms 21860 KB Output is correct
20 Correct 11 ms 21460 KB Output is correct
21 Correct 10 ms 21376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 21452 KB Output is correct
2 Correct 9 ms 21452 KB Output is correct
3 Correct 9 ms 21332 KB Output is correct
4 Correct 9 ms 21384 KB Output is correct
5 Correct 9 ms 21460 KB Output is correct
6 Correct 23 ms 25556 KB Output is correct
7 Correct 9 ms 21460 KB Output is correct
8 Correct 9 ms 21460 KB Output is correct
9 Correct 9 ms 21460 KB Output is correct
10 Correct 9 ms 21460 KB Output is correct
11 Correct 11 ms 21504 KB Output is correct
12 Correct 10 ms 21460 KB Output is correct
13 Correct 9 ms 21460 KB Output is correct
14 Correct 10 ms 21332 KB Output is correct
15 Correct 9 ms 21332 KB Output is correct
16 Correct 9 ms 21460 KB Output is correct
17 Correct 9 ms 21460 KB Output is correct
18 Correct 10 ms 21588 KB Output is correct
19 Correct 12 ms 21860 KB Output is correct
20 Correct 11 ms 21460 KB Output is correct
21 Correct 10 ms 21376 KB Output is correct
22 Correct 938 ms 135032 KB Output is correct
23 Correct 421 ms 132228 KB Output is correct
24 Correct 25 ms 26064 KB Output is correct
25 Correct 13 ms 22996 KB Output is correct
26 Correct 13 ms 22896 KB Output is correct
27 Correct 11 ms 21972 KB Output is correct
28 Correct 252 ms 66636 KB Output is correct
29 Correct 117 ms 39244 KB Output is correct
30 Correct 161 ms 49192 KB Output is correct
31 Correct 10 ms 21592 KB Output is correct
32 Correct 10 ms 21596 KB Output is correct
33 Correct 335 ms 109016 KB Output is correct
34 Correct 292 ms 100164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 137 ms 45172 KB Output is correct
2 Correct 301 ms 59304 KB Output is correct
3 Correct 81 ms 34040 KB Output is correct
4 Correct 190 ms 49064 KB Output is correct
5 Correct 428 ms 96980 KB Output is correct
6 Correct 90 ms 48088 KB Output is correct
7 Correct 129 ms 87360 KB Output is correct
8 Correct 129 ms 63692 KB Output is correct
9 Correct 13 ms 21844 KB Output is correct
10 Correct 87 ms 48244 KB Output is correct
11 Correct 191 ms 73988 KB Output is correct
12 Correct 169 ms 75084 KB Output is correct
13 Correct 10 ms 21460 KB Output is correct
14 Correct 101 ms 43652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 137 ms 45172 KB Output is correct
2 Correct 301 ms 59304 KB Output is correct
3 Correct 81 ms 34040 KB Output is correct
4 Correct 190 ms 49064 KB Output is correct
5 Correct 428 ms 96980 KB Output is correct
6 Correct 90 ms 48088 KB Output is correct
7 Correct 129 ms 87360 KB Output is correct
8 Correct 129 ms 63692 KB Output is correct
9 Correct 13 ms 21844 KB Output is correct
10 Correct 87 ms 48244 KB Output is correct
11 Correct 191 ms 73988 KB Output is correct
12 Correct 169 ms 75084 KB Output is correct
13 Correct 10 ms 21460 KB Output is correct
14 Correct 101 ms 43652 KB Output is correct
15 Correct 10 ms 21452 KB Output is correct
16 Correct 9 ms 21452 KB Output is correct
17 Correct 9 ms 21332 KB Output is correct
18 Correct 9 ms 21384 KB Output is correct
19 Correct 9 ms 21460 KB Output is correct
20 Correct 23 ms 25556 KB Output is correct
21 Correct 9 ms 21460 KB Output is correct
22 Correct 9 ms 21460 KB Output is correct
23 Correct 9 ms 21460 KB Output is correct
24 Correct 9 ms 21460 KB Output is correct
25 Correct 11 ms 21504 KB Output is correct
26 Correct 10 ms 21460 KB Output is correct
27 Correct 9 ms 21460 KB Output is correct
28 Correct 10 ms 21332 KB Output is correct
29 Correct 9 ms 21332 KB Output is correct
30 Correct 9 ms 21460 KB Output is correct
31 Correct 9 ms 21460 KB Output is correct
32 Correct 10 ms 21588 KB Output is correct
33 Correct 12 ms 21860 KB Output is correct
34 Correct 11 ms 21460 KB Output is correct
35 Correct 10 ms 21376 KB Output is correct
36 Correct 938 ms 135032 KB Output is correct
37 Correct 421 ms 132228 KB Output is correct
38 Correct 25 ms 26064 KB Output is correct
39 Correct 13 ms 22996 KB Output is correct
40 Correct 13 ms 22896 KB Output is correct
41 Correct 11 ms 21972 KB Output is correct
42 Correct 252 ms 66636 KB Output is correct
43 Correct 117 ms 39244 KB Output is correct
44 Correct 161 ms 49192 KB Output is correct
45 Correct 10 ms 21592 KB Output is correct
46 Correct 10 ms 21596 KB Output is correct
47 Correct 335 ms 109016 KB Output is correct
48 Correct 292 ms 100164 KB Output is correct
49 Correct 335 ms 58364 KB Output is correct
50 Correct 249 ms 57784 KB Output is correct
51 Correct 371 ms 64372 KB Output is correct
52 Correct 431 ms 70884 KB Output is correct
53 Correct 143 ms 41228 KB Output is correct
54 Correct 474 ms 91468 KB Output is correct
55 Correct 388 ms 155684 KB Output is correct
56 Correct 243 ms 110540 KB Output is correct
57 Correct 421 ms 78292 KB Output is correct
58 Correct 277 ms 48864 KB Output is correct
59 Correct 428 ms 94512 KB Output is correct
60 Correct 488 ms 73248 KB Output is correct
61 Correct 406 ms 72052 KB Output is correct