#include <bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define vll vector<ll>
#define vii vector<pair<int, int>>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define loop(_) for (int __ = 0; __ < (_); ++__)
#define pb push_back
#define f first
#define s second
#define sz(_) ((int)_.size())
#define all(_) _.begin(), _.end()
#define lb lower_bound
#define ub upper_bound
using ll = long long;
using ld = long double;
const int N = 3e5 + 7;
const ll mod = 1e9 + 7;
int n, m;
ll a[N];
int viz[N], col = 1;
ll ans;
map<pii, ll> cost;
vi adj[N];
vi adj2[N];
vii e, e2;
ll disto[N];
int fat[N];
void dfs(int x, int p = 0, int pp = 0)
{
viz[x] = col;
fat[x] = p;
for (auto u : adj[x])
{
if (u == p)
continue;
if (viz[u] != col)
{
dfs(u, x, p);
}
else
{
e2.pb({u, x});
if (p && pp && u == pp)
{
ans = max(ans, cost[{p, pp}] + cost[{x, p}] + cost[{x, pp}]);
}
adj2[x].pb(u);
}
}
}
void dfz(int x, int p)
{
map<int, bool> mp;
for (auto u : adj2[x])
mp[u] = 1;
for (auto u : adj[x])
{
if (fat[u] == x)
{
dfz(u, x);
for (auto v : adj2[u])
{
if (mp.count(v))
{
ans = max(ans, cost[{x, u}] + cost[{x, v}] + cost[{u, v}]);
}
disto[v] = -2e17;
}
}
}
if (x != p)
{
for (auto u : adj2[x])
{
disto[u] = cost[{x, u}];
}
}
}
void prep(vi &act, vii &e)
{
act.clear();
for (auto u : e)
{
act.pb(u.f);
act.pb(u.s);
}
for (auto u : act)
{
adj[u].clear();
adj2[u].clear();
disto[u] = -2e17;
fat[u] = 0;
}
for (auto u : e)
{
adj[u.f].pb(u.s);
adj[u.s].pb(u.f);
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
#ifndef ONLINE_JUDGE
#endif
cin >> n >> m;
for (int i = 0; i < N; ++i)
disto[i] = -2e17;
vi act;
for (int i = 0; i < m; ++i)
{
int u, v, w;
cin >> u >> v >> w;
a[u] += w;
a[v] += w;
ans = max({ans, a[u], a[v]});
e.pb({u, v});
cost[{u, v}] = cost[{v, u}] = w;
}
while (sz(e))
{
prep(act, e);
for (auto u : act)
{
if (viz[u] != col)
{
dfs(u, 0, 0);
dfz(u, 0);
}
}
e = e2;
e2.clear();
++col;
break;
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
16748 KB |
Output is correct |
2 |
Correct |
12 ms |
16876 KB |
Output is correct |
3 |
Correct |
12 ms |
16748 KB |
Output is correct |
4 |
Correct |
13 ms |
16748 KB |
Output is correct |
5 |
Correct |
13 ms |
16748 KB |
Output is correct |
6 |
Correct |
47 ms |
22204 KB |
Output is correct |
7 |
Correct |
12 ms |
16748 KB |
Output is correct |
8 |
Correct |
12 ms |
16748 KB |
Output is correct |
9 |
Correct |
12 ms |
16876 KB |
Output is correct |
10 |
Correct |
12 ms |
16748 KB |
Output is correct |
11 |
Correct |
13 ms |
17004 KB |
Output is correct |
12 |
Correct |
13 ms |
16876 KB |
Output is correct |
13 |
Correct |
12 ms |
16876 KB |
Output is correct |
14 |
Correct |
12 ms |
16748 KB |
Output is correct |
15 |
Correct |
12 ms |
16748 KB |
Output is correct |
16 |
Correct |
12 ms |
16748 KB |
Output is correct |
17 |
Correct |
14 ms |
16748 KB |
Output is correct |
18 |
Correct |
15 ms |
17132 KB |
Output is correct |
19 |
Correct |
16 ms |
17644 KB |
Output is correct |
20 |
Correct |
13 ms |
16888 KB |
Output is correct |
21 |
Correct |
12 ms |
16748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
16748 KB |
Output is correct |
2 |
Correct |
12 ms |
16876 KB |
Output is correct |
3 |
Correct |
12 ms |
16748 KB |
Output is correct |
4 |
Correct |
13 ms |
16748 KB |
Output is correct |
5 |
Correct |
13 ms |
16748 KB |
Output is correct |
6 |
Correct |
47 ms |
22204 KB |
Output is correct |
7 |
Correct |
12 ms |
16748 KB |
Output is correct |
8 |
Correct |
12 ms |
16748 KB |
Output is correct |
9 |
Correct |
12 ms |
16876 KB |
Output is correct |
10 |
Correct |
12 ms |
16748 KB |
Output is correct |
11 |
Correct |
13 ms |
17004 KB |
Output is correct |
12 |
Correct |
13 ms |
16876 KB |
Output is correct |
13 |
Correct |
12 ms |
16876 KB |
Output is correct |
14 |
Correct |
12 ms |
16748 KB |
Output is correct |
15 |
Correct |
12 ms |
16748 KB |
Output is correct |
16 |
Correct |
12 ms |
16748 KB |
Output is correct |
17 |
Correct |
14 ms |
16748 KB |
Output is correct |
18 |
Correct |
15 ms |
17132 KB |
Output is correct |
19 |
Correct |
16 ms |
17644 KB |
Output is correct |
20 |
Correct |
13 ms |
16888 KB |
Output is correct |
21 |
Correct |
12 ms |
16748 KB |
Output is correct |
22 |
Correct |
1981 ms |
152856 KB |
Output is correct |
23 |
Correct |
1115 ms |
150348 KB |
Output is correct |
24 |
Correct |
49 ms |
22372 KB |
Output is correct |
25 |
Correct |
26 ms |
18540 KB |
Output is correct |
26 |
Correct |
21 ms |
18540 KB |
Output is correct |
27 |
Correct |
16 ms |
17388 KB |
Output is correct |
28 |
Correct |
649 ms |
71068 KB |
Output is correct |
29 |
Correct |
278 ms |
38752 KB |
Output is correct |
30 |
Correct |
494 ms |
50268 KB |
Output is correct |
31 |
Correct |
13 ms |
17004 KB |
Output is correct |
32 |
Correct |
13 ms |
17004 KB |
Output is correct |
33 |
Correct |
960 ms |
127556 KB |
Output is correct |
34 |
Correct |
858 ms |
118736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
339 ms |
52096 KB |
Output is correct |
2 |
Correct |
869 ms |
77916 KB |
Output is correct |
3 |
Correct |
221 ms |
37220 KB |
Output is correct |
4 |
Correct |
429 ms |
55772 KB |
Output is correct |
5 |
Correct |
1288 ms |
103888 KB |
Output is correct |
6 |
Correct |
255 ms |
50268 KB |
Output is correct |
7 |
Correct |
286 ms |
90976 KB |
Output is correct |
8 |
Correct |
349 ms |
81104 KB |
Output is correct |
9 |
Correct |
17 ms |
21612 KB |
Output is correct |
10 |
Correct |
250 ms |
50136 KB |
Output is correct |
11 |
Correct |
422 ms |
82640 KB |
Output is correct |
12 |
Correct |
470 ms |
83668 KB |
Output is correct |
13 |
Correct |
12 ms |
16748 KB |
Output is correct |
14 |
Correct |
282 ms |
44128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
339 ms |
52096 KB |
Output is correct |
2 |
Correct |
869 ms |
77916 KB |
Output is correct |
3 |
Correct |
221 ms |
37220 KB |
Output is correct |
4 |
Correct |
429 ms |
55772 KB |
Output is correct |
5 |
Correct |
1288 ms |
103888 KB |
Output is correct |
6 |
Correct |
255 ms |
50268 KB |
Output is correct |
7 |
Correct |
286 ms |
90976 KB |
Output is correct |
8 |
Correct |
349 ms |
81104 KB |
Output is correct |
9 |
Correct |
17 ms |
21612 KB |
Output is correct |
10 |
Correct |
250 ms |
50136 KB |
Output is correct |
11 |
Correct |
422 ms |
82640 KB |
Output is correct |
12 |
Correct |
470 ms |
83668 KB |
Output is correct |
13 |
Correct |
12 ms |
16748 KB |
Output is correct |
14 |
Correct |
282 ms |
44128 KB |
Output is correct |
15 |
Correct |
12 ms |
16748 KB |
Output is correct |
16 |
Correct |
12 ms |
16876 KB |
Output is correct |
17 |
Correct |
12 ms |
16748 KB |
Output is correct |
18 |
Correct |
13 ms |
16748 KB |
Output is correct |
19 |
Correct |
13 ms |
16748 KB |
Output is correct |
20 |
Correct |
47 ms |
22204 KB |
Output is correct |
21 |
Correct |
12 ms |
16748 KB |
Output is correct |
22 |
Correct |
12 ms |
16748 KB |
Output is correct |
23 |
Correct |
12 ms |
16876 KB |
Output is correct |
24 |
Correct |
12 ms |
16748 KB |
Output is correct |
25 |
Correct |
13 ms |
17004 KB |
Output is correct |
26 |
Correct |
13 ms |
16876 KB |
Output is correct |
27 |
Correct |
12 ms |
16876 KB |
Output is correct |
28 |
Correct |
12 ms |
16748 KB |
Output is correct |
29 |
Correct |
12 ms |
16748 KB |
Output is correct |
30 |
Correct |
12 ms |
16748 KB |
Output is correct |
31 |
Correct |
14 ms |
16748 KB |
Output is correct |
32 |
Correct |
15 ms |
17132 KB |
Output is correct |
33 |
Correct |
16 ms |
17644 KB |
Output is correct |
34 |
Correct |
13 ms |
16888 KB |
Output is correct |
35 |
Correct |
12 ms |
16748 KB |
Output is correct |
36 |
Correct |
1981 ms |
152856 KB |
Output is correct |
37 |
Correct |
1115 ms |
150348 KB |
Output is correct |
38 |
Correct |
49 ms |
22372 KB |
Output is correct |
39 |
Correct |
26 ms |
18540 KB |
Output is correct |
40 |
Correct |
21 ms |
18540 KB |
Output is correct |
41 |
Correct |
16 ms |
17388 KB |
Output is correct |
42 |
Correct |
649 ms |
71068 KB |
Output is correct |
43 |
Correct |
278 ms |
38752 KB |
Output is correct |
44 |
Correct |
494 ms |
50268 KB |
Output is correct |
45 |
Correct |
13 ms |
17004 KB |
Output is correct |
46 |
Correct |
13 ms |
17004 KB |
Output is correct |
47 |
Correct |
960 ms |
127556 KB |
Output is correct |
48 |
Correct |
858 ms |
118736 KB |
Output is correct |
49 |
Correct |
701 ms |
60116 KB |
Output is correct |
50 |
Correct |
682 ms |
60980 KB |
Output is correct |
51 |
Correct |
758 ms |
69688 KB |
Output is correct |
52 |
Correct |
1173 ms |
77168 KB |
Output is correct |
53 |
Correct |
181 ms |
35812 KB |
Output is correct |
54 |
Correct |
1102 ms |
100680 KB |
Output is correct |
55 |
Correct |
830 ms |
143696 KB |
Output is correct |
56 |
Correct |
513 ms |
101344 KB |
Output is correct |
57 |
Correct |
595 ms |
76376 KB |
Output is correct |
58 |
Correct |
540 ms |
49624 KB |
Output is correct |
59 |
Correct |
1085 ms |
101284 KB |
Output is correct |
60 |
Correct |
1198 ms |
83412 KB |
Output is correct |
61 |
Correct |
1122 ms |
82244 KB |
Output is correct |