#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
struct Edge{
int v, w, i;
Edge(){}
Edge(int _v, int _w, int _i): v(_v), w(_w), i(_i) {}
};
vector<Edge> adj[500500];
int on[2002000], W[2002000], dist[500500];
vector<Edge> E;
vector<int> P;
bool dfs0(int s, int e, int pa = -1){
if (s==e) return 1;
for (auto &v:adj[s]) if (v.v!=pa && on[v.i]){
P.push_back(v.i);
if (dfs0(v.v, e, s)) return 1;
P.pop_back();
}
return 0;
}
void dfs(int s, int pa = -1, int w = 0){
if (pa==-1) dist[s] = 0;
else dist[s] = dist[pa] + w;
for (auto &v:adj[s]) if (v.v!=pa && on[v.i]) dfs(v.v, s, v.w);
}
int cur = 0, ans = 1e9, m;
void calc(int n){
dfs(0);
dfs(max_element(dist, dist+n)-dist);
ans = min(ans, cur*2 - *max_element(dist, dist+n));
//printf("cur: %d, ans = %d\n", cur, cur*2 - *max_element(dist, dist+n));
//for (int i=1;i<=m;i++) printf("%d ", on[i]);
//printf("\n");
}
int main(){
int n;
scanf("%d %d", &n, &m);
for (int i=1;i<=m;i++){
int x, y, z;
scanf("%d %d %d", &x, &y, &z);
adj[x].emplace_back(y, z, i);
adj[y].emplace_back(x, z, i);
W[i] = z;
if (z==1) on[i] = 1, cur++;
else E.emplace_back(x, y, i);
}
calc(n);
if (n>500) {printf("%d\n", ans); return 0;}
for (auto &e:E){
P.clear();
assert(dfs0(e.v, e.w));
//printf("%d %d: ", e.v, e.w);
//for (auto &x:P) printf("%d ", x);
//printf("\n");
if ((int)P.size()<=W[e.i]) continue;
on[e.i] = 1, cur += W[e.i];
for (auto &x:P){
on[x] = 0, cur -= W[x];
calc(n);
on[x] = 1, cur += W[x];
}
on[e.i] = 0, cur -= W[e.i];
}
printf("%d\n", ans);
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:45:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:48:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
48 | scanf("%d %d %d", &x, &y, &z);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11984 KB |
Output is correct |
2 |
Correct |
7 ms |
12040 KB |
Output is correct |
3 |
Correct |
6 ms |
11980 KB |
Output is correct |
4 |
Correct |
6 ms |
12016 KB |
Output is correct |
5 |
Correct |
7 ms |
11980 KB |
Output is correct |
6 |
Correct |
6 ms |
11980 KB |
Output is correct |
7 |
Correct |
7 ms |
11980 KB |
Output is correct |
8 |
Correct |
6 ms |
11980 KB |
Output is correct |
9 |
Correct |
6 ms |
11980 KB |
Output is correct |
10 |
Correct |
6 ms |
11980 KB |
Output is correct |
11 |
Correct |
6 ms |
11980 KB |
Output is correct |
12 |
Correct |
9 ms |
11980 KB |
Output is correct |
13 |
Correct |
7 ms |
12024 KB |
Output is correct |
14 |
Correct |
6 ms |
11980 KB |
Output is correct |
15 |
Correct |
8 ms |
11980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11984 KB |
Output is correct |
2 |
Correct |
7 ms |
12040 KB |
Output is correct |
3 |
Correct |
6 ms |
11980 KB |
Output is correct |
4 |
Correct |
6 ms |
12016 KB |
Output is correct |
5 |
Correct |
7 ms |
11980 KB |
Output is correct |
6 |
Correct |
6 ms |
11980 KB |
Output is correct |
7 |
Correct |
7 ms |
11980 KB |
Output is correct |
8 |
Correct |
6 ms |
11980 KB |
Output is correct |
9 |
Correct |
6 ms |
11980 KB |
Output is correct |
10 |
Correct |
6 ms |
11980 KB |
Output is correct |
11 |
Correct |
6 ms |
11980 KB |
Output is correct |
12 |
Correct |
9 ms |
11980 KB |
Output is correct |
13 |
Correct |
7 ms |
12024 KB |
Output is correct |
14 |
Correct |
6 ms |
11980 KB |
Output is correct |
15 |
Correct |
8 ms |
11980 KB |
Output is correct |
16 |
Correct |
6 ms |
11980 KB |
Output is correct |
17 |
Correct |
6 ms |
12052 KB |
Output is correct |
18 |
Incorrect |
6 ms |
12068 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
12748 KB |
Output is correct |
2 |
Correct |
10 ms |
12748 KB |
Output is correct |
3 |
Correct |
10 ms |
12708 KB |
Output is correct |
4 |
Correct |
11 ms |
12620 KB |
Output is correct |
5 |
Correct |
10 ms |
12620 KB |
Output is correct |
6 |
Correct |
11 ms |
12592 KB |
Output is correct |
7 |
Correct |
11 ms |
12748 KB |
Output is correct |
8 |
Correct |
10 ms |
12748 KB |
Output is correct |
9 |
Correct |
10 ms |
12728 KB |
Output is correct |
10 |
Correct |
10 ms |
12700 KB |
Output is correct |
11 |
Correct |
10 ms |
12620 KB |
Output is correct |
12 |
Correct |
14 ms |
12636 KB |
Output is correct |
13 |
Correct |
11 ms |
12748 KB |
Output is correct |
14 |
Correct |
11 ms |
12720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11984 KB |
Output is correct |
2 |
Correct |
7 ms |
12040 KB |
Output is correct |
3 |
Correct |
6 ms |
11980 KB |
Output is correct |
4 |
Correct |
6 ms |
12016 KB |
Output is correct |
5 |
Correct |
7 ms |
11980 KB |
Output is correct |
6 |
Correct |
6 ms |
11980 KB |
Output is correct |
7 |
Correct |
7 ms |
11980 KB |
Output is correct |
8 |
Correct |
6 ms |
11980 KB |
Output is correct |
9 |
Correct |
6 ms |
11980 KB |
Output is correct |
10 |
Correct |
6 ms |
11980 KB |
Output is correct |
11 |
Correct |
6 ms |
11980 KB |
Output is correct |
12 |
Correct |
9 ms |
11980 KB |
Output is correct |
13 |
Correct |
7 ms |
12024 KB |
Output is correct |
14 |
Correct |
6 ms |
11980 KB |
Output is correct |
15 |
Correct |
8 ms |
11980 KB |
Output is correct |
16 |
Correct |
6 ms |
11980 KB |
Output is correct |
17 |
Correct |
6 ms |
12052 KB |
Output is correct |
18 |
Incorrect |
6 ms |
12068 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11984 KB |
Output is correct |
2 |
Correct |
7 ms |
12040 KB |
Output is correct |
3 |
Correct |
6 ms |
11980 KB |
Output is correct |
4 |
Correct |
6 ms |
12016 KB |
Output is correct |
5 |
Correct |
7 ms |
11980 KB |
Output is correct |
6 |
Correct |
6 ms |
11980 KB |
Output is correct |
7 |
Correct |
7 ms |
11980 KB |
Output is correct |
8 |
Correct |
6 ms |
11980 KB |
Output is correct |
9 |
Correct |
6 ms |
11980 KB |
Output is correct |
10 |
Correct |
6 ms |
11980 KB |
Output is correct |
11 |
Correct |
6 ms |
11980 KB |
Output is correct |
12 |
Correct |
9 ms |
11980 KB |
Output is correct |
13 |
Correct |
7 ms |
12024 KB |
Output is correct |
14 |
Correct |
6 ms |
11980 KB |
Output is correct |
15 |
Correct |
8 ms |
11980 KB |
Output is correct |
16 |
Correct |
6 ms |
11980 KB |
Output is correct |
17 |
Correct |
6 ms |
12052 KB |
Output is correct |
18 |
Incorrect |
6 ms |
12068 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11984 KB |
Output is correct |
2 |
Correct |
7 ms |
12040 KB |
Output is correct |
3 |
Correct |
6 ms |
11980 KB |
Output is correct |
4 |
Correct |
6 ms |
12016 KB |
Output is correct |
5 |
Correct |
7 ms |
11980 KB |
Output is correct |
6 |
Correct |
6 ms |
11980 KB |
Output is correct |
7 |
Correct |
7 ms |
11980 KB |
Output is correct |
8 |
Correct |
6 ms |
11980 KB |
Output is correct |
9 |
Correct |
6 ms |
11980 KB |
Output is correct |
10 |
Correct |
6 ms |
11980 KB |
Output is correct |
11 |
Correct |
6 ms |
11980 KB |
Output is correct |
12 |
Correct |
9 ms |
11980 KB |
Output is correct |
13 |
Correct |
7 ms |
12024 KB |
Output is correct |
14 |
Correct |
6 ms |
11980 KB |
Output is correct |
15 |
Correct |
8 ms |
11980 KB |
Output is correct |
16 |
Correct |
6 ms |
11980 KB |
Output is correct |
17 |
Correct |
6 ms |
12052 KB |
Output is correct |
18 |
Incorrect |
6 ms |
12068 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11984 KB |
Output is correct |
2 |
Correct |
7 ms |
12040 KB |
Output is correct |
3 |
Correct |
6 ms |
11980 KB |
Output is correct |
4 |
Correct |
6 ms |
12016 KB |
Output is correct |
5 |
Correct |
7 ms |
11980 KB |
Output is correct |
6 |
Correct |
6 ms |
11980 KB |
Output is correct |
7 |
Correct |
7 ms |
11980 KB |
Output is correct |
8 |
Correct |
6 ms |
11980 KB |
Output is correct |
9 |
Correct |
6 ms |
11980 KB |
Output is correct |
10 |
Correct |
6 ms |
11980 KB |
Output is correct |
11 |
Correct |
6 ms |
11980 KB |
Output is correct |
12 |
Correct |
9 ms |
11980 KB |
Output is correct |
13 |
Correct |
7 ms |
12024 KB |
Output is correct |
14 |
Correct |
6 ms |
11980 KB |
Output is correct |
15 |
Correct |
8 ms |
11980 KB |
Output is correct |
16 |
Correct |
6 ms |
11980 KB |
Output is correct |
17 |
Correct |
6 ms |
12052 KB |
Output is correct |
18 |
Incorrect |
6 ms |
12068 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11984 KB |
Output is correct |
2 |
Correct |
7 ms |
12040 KB |
Output is correct |
3 |
Correct |
6 ms |
11980 KB |
Output is correct |
4 |
Correct |
6 ms |
12016 KB |
Output is correct |
5 |
Correct |
7 ms |
11980 KB |
Output is correct |
6 |
Correct |
6 ms |
11980 KB |
Output is correct |
7 |
Correct |
7 ms |
11980 KB |
Output is correct |
8 |
Correct |
6 ms |
11980 KB |
Output is correct |
9 |
Correct |
6 ms |
11980 KB |
Output is correct |
10 |
Correct |
6 ms |
11980 KB |
Output is correct |
11 |
Correct |
6 ms |
11980 KB |
Output is correct |
12 |
Correct |
9 ms |
11980 KB |
Output is correct |
13 |
Correct |
7 ms |
12024 KB |
Output is correct |
14 |
Correct |
6 ms |
11980 KB |
Output is correct |
15 |
Correct |
8 ms |
11980 KB |
Output is correct |
16 |
Correct |
6 ms |
11980 KB |
Output is correct |
17 |
Correct |
6 ms |
12052 KB |
Output is correct |
18 |
Incorrect |
6 ms |
12068 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |