#include <bits/stdc++.h>
using namespace std;
typedef pair<long long,long long> ii;
static vector<ii> adj[500005];
static vector<ii> tree[500005];
map<ii, long long> thonk;
class UFDS {
typedef vector<int> vi;
public:
vi p, rak, setSize;
int numSets;
void create(int n){
setSize.assign(n, 1);
numSets = n;
rak.assign(n, 0);
p.assign(n, 0);
for(int i =0;i < n;i++){
p[i] = i;
}
}
int findSet(int i){
return (p[i] == i) ? i : (p[i] = findSet(p[i]));
}
bool isSameSet(int i, int j){
return findSet(i) == findSet(j);
}
bool unionSet(int i, int j){
if(isSameSet(i,j))
return false;
numSets--;
int x = findSet(i);
int y = findSet(j);
if(rak[x] > rak[y]) {
p[y] = x; setSize[x] += setSize[y];
}
else {
p[x] = y; setSize[y] += setSize[x];
if(rak[x] == rak[y]) rak[y]++;
}
return true;
}
} uf;
struct edge{
long long u;
long long v;
long long w;
};
bool comp(edge a, edge b){
return a.w > b.w;
}
int main(){
//freopen("i.txt","r",stdin);
ios_base::sync_with_stdio(false);
cin.tie(0);
long long n, m; cin >> n >> m;
edge edges[m];
uf.create(n);
for(long long i = 0;i < m;i++){
long long u, v; long long w;
cin >> u >> v >> w; u--; v--;
assert(u != v);
adj[u].push_back(ii(v,w));
adj[v].push_back(ii(u,w));
edges[i] = {u,v,w};
thonk[ii(u,v)] = w;
thonk[ii(v,u)] = w;
}
long long ans = 0;
for(long long i = 0;i < n;i++){
long long acc = 0;
for(ii v : adj[i]) acc += v.second;
ans = max(ans, acc);
}
sort(edges,edges+m,comp);
for(edge e : edges){
if(uf.unionSet(e.u, e.v)){
tree[e.u].push_back(ii(e.v,e.w));
tree[e.v].push_back(ii(e.u,e.w));
}
}
for(long long i = 0;i < n;i++){
if(tree[i].size() == 2){
ii u = tree[i][0];
ii v = tree[i][1];
if(thonk.find(ii(u.first,v.first)) != thonk.end()){
ans = max(ans, u.second + v.second + thonk[ii(u.first,v.first)]);
}
}
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
23800 KB |
Output is correct |
2 |
Correct |
19 ms |
23800 KB |
Output is correct |
3 |
Correct |
19 ms |
23800 KB |
Output is correct |
4 |
Correct |
20 ms |
23800 KB |
Output is correct |
5 |
Correct |
19 ms |
23800 KB |
Output is correct |
6 |
Correct |
42 ms |
27640 KB |
Output is correct |
7 |
Correct |
20 ms |
23800 KB |
Output is correct |
8 |
Correct |
21 ms |
23796 KB |
Output is correct |
9 |
Correct |
19 ms |
23800 KB |
Output is correct |
10 |
Incorrect |
20 ms |
23800 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
23800 KB |
Output is correct |
2 |
Correct |
19 ms |
23800 KB |
Output is correct |
3 |
Correct |
19 ms |
23800 KB |
Output is correct |
4 |
Correct |
20 ms |
23800 KB |
Output is correct |
5 |
Correct |
19 ms |
23800 KB |
Output is correct |
6 |
Correct |
42 ms |
27640 KB |
Output is correct |
7 |
Correct |
20 ms |
23800 KB |
Output is correct |
8 |
Correct |
21 ms |
23796 KB |
Output is correct |
9 |
Correct |
19 ms |
23800 KB |
Output is correct |
10 |
Incorrect |
20 ms |
23800 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
440 ms |
68444 KB |
Output is correct |
2 |
Correct |
1113 ms |
99680 KB |
Output is correct |
3 |
Correct |
299 ms |
49144 KB |
Output is correct |
4 |
Correct |
674 ms |
75136 KB |
Output is correct |
5 |
Correct |
1416 ms |
94968 KB |
Output is correct |
6 |
Correct |
160 ms |
46456 KB |
Output is correct |
7 |
Correct |
334 ms |
88208 KB |
Output is correct |
8 |
Correct |
401 ms |
96992 KB |
Output is correct |
9 |
Correct |
27 ms |
27640 KB |
Output is correct |
10 |
Correct |
153 ms |
46456 KB |
Output is correct |
11 |
Correct |
397 ms |
92896 KB |
Output is correct |
12 |
Correct |
310 ms |
69112 KB |
Output is correct |
13 |
Correct |
21 ms |
23800 KB |
Output is correct |
14 |
Correct |
291 ms |
43000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
440 ms |
68444 KB |
Output is correct |
2 |
Correct |
1113 ms |
99680 KB |
Output is correct |
3 |
Correct |
299 ms |
49144 KB |
Output is correct |
4 |
Correct |
674 ms |
75136 KB |
Output is correct |
5 |
Correct |
1416 ms |
94968 KB |
Output is correct |
6 |
Correct |
160 ms |
46456 KB |
Output is correct |
7 |
Correct |
334 ms |
88208 KB |
Output is correct |
8 |
Correct |
401 ms |
96992 KB |
Output is correct |
9 |
Correct |
27 ms |
27640 KB |
Output is correct |
10 |
Correct |
153 ms |
46456 KB |
Output is correct |
11 |
Correct |
397 ms |
92896 KB |
Output is correct |
12 |
Correct |
310 ms |
69112 KB |
Output is correct |
13 |
Correct |
21 ms |
23800 KB |
Output is correct |
14 |
Correct |
291 ms |
43000 KB |
Output is correct |
15 |
Correct |
798 ms |
66888 KB |
Output is correct |
16 |
Correct |
675 ms |
61892 KB |
Output is correct |
17 |
Correct |
914 ms |
72044 KB |
Output is correct |
18 |
Correct |
1111 ms |
78768 KB |
Output is correct |
19 |
Correct |
277 ms |
49640 KB |
Output is correct |
20 |
Correct |
1143 ms |
83192 KB |
Output is correct |
21 |
Correct |
1398 ms |
102952 KB |
Output is correct |
22 |
Correct |
752 ms |
76512 KB |
Output is correct |
23 |
Correct |
795 ms |
87500 KB |
Output is correct |
24 |
Correct |
523 ms |
56532 KB |
Output is correct |
25 |
Correct |
1147 ms |
84344 KB |
Output is correct |
26 |
Correct |
1214 ms |
82092 KB |
Output is correct |
27 |
Correct |
1082 ms |
78984 KB |
Output is correct |
28 |
Correct |
20 ms |
23800 KB |
Output is correct |
29 |
Correct |
19 ms |
23800 KB |
Output is correct |
30 |
Correct |
19 ms |
23800 KB |
Output is correct |
31 |
Correct |
20 ms |
23800 KB |
Output is correct |
32 |
Correct |
19 ms |
23800 KB |
Output is correct |
33 |
Correct |
42 ms |
27640 KB |
Output is correct |
34 |
Correct |
20 ms |
23800 KB |
Output is correct |
35 |
Correct |
21 ms |
23796 KB |
Output is correct |
36 |
Correct |
19 ms |
23800 KB |
Output is correct |
37 |
Incorrect |
20 ms |
23800 KB |
Output isn't correct |
38 |
Halted |
0 ms |
0 KB |
- |