#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)
template<typename T>
void amin(T &a, T b) {
a = min(a,b);
}
template<typename T>
void amax(T &a, T b) {
a = max(a,b);
}
#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif
/*
refs:
some solutions
*/
const int MOD = 1e9 + 7;
const int N = 500 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;
struct DSU {
vector<int> par, rankk, siz;
DSU() {
}
DSU(int n) {
init(n);
}
void init(int n) {
par = vector<int>(n + 1);
rankk = vector<int>(n + 1);
siz = vector<int>(n + 1);
rep(i, n + 1) create(i);
}
void create(int u) {
par[u] = u;
rankk[u] = 0;
siz[u] = 1;
}
int find(int u) {
if (u == par[u]) return u;
else return par[u] = find(par[u]);
}
bool same(int u, int v) {
return find(u) == find(v);
}
void merge(int u, int v) {
u = find(u), v = find(v);
if (u == v) return;
if (rankk[u] == rankk[v]) rankk[u]++;
if (rankk[u] < rankk[v]) swap(u, v);
par[v] = u;
siz[u] += siz[v];
}
};
vector<pll> adj[N];
vector<ll> active;
ll mn_on_path;
void dfs1(ll u, ll p, ll des, ll mn){
if(u == des){
mn_on_path = mn;
}
for(auto [v,id] : adj[u]){
if(v == p) conts;
dfs1(v,u,des,min(mn,id));
}
}
void solve(int test_case)
{
ll n,m; cin >> n >> m;
vector<array<ll,3>> edges(m+5);
rep1(i,m){
ll u,v,w; cin >> u >> v >> w;
edges[i] = {w,u,v};
}
sort(edges.begin()+1,edges.begin()+m+1);
vector<ll> edges_front[m+5], edges_back[m+5];
rep1(i,m){
mn_on_path = inf2;
auto [w,u,v] = edges[i];
dfs1(u,-1,v,inf2);
if(mn_on_path != inf2){
auto [w,x,y] = edges[mn_on_path];
active.erase(find(all(active),mn_on_path));
pll px = {x,mn_on_path}, py = {y,mn_on_path};
adj[x].erase(find(all(adj[x]),py));
adj[y].erase(find(all(adj[y]),px));
}
active.pb(i);
adj[u].pb({v,i}), adj[v].pb({u,i});
edges_front[i] = active;
}
rep1(i,n){
adj[i].clear();
}
active.clear();
rev(i,m,1){
mn_on_path = inf2;
auto [w,u,v] = edges[i];
dfs1(u,-1,v,inf2);
if(mn_on_path != inf2){
mn_on_path = -mn_on_path;
auto [w,x,y] = edges[mn_on_path];
active.erase(find(all(active),mn_on_path));
pll px = {x,-mn_on_path}, py = {y,-mn_on_path};
adj[x].erase(find(all(adj[x]),py));
adj[y].erase(find(all(adj[y]),px));
}
active.pb(i);
adj[u].pb({v,-i}), adj[v].pb({u,-i});
edges_back[i] = active;
}
ll q; cin >> q;
while(q--){
ll x; cin >> x;
array<ll,3> ar = {x,-1,-1};
ll pos = lower_bound(edges.begin()+1,edges.begin()+m+1,ar)-edges.begin();
vector<array<ll,3>> curr_edges;
trav(id,edges_front[pos-1]){
auto [w,u,v] = edges[id];
w = abs(w-x);
curr_edges.pb({w,u,v});
}
trav(id,edges_back[pos]){
auto [w,u,v] = edges[id];
w = abs(w-x);
curr_edges.pb({w,u,v});
}
sort(all(curr_edges));
DSU dsu(n+5);
ll ans = 0;
for(auto [w,u,v] : curr_edges){
if(dsu.same(u,v)) conts;
dsu.merge(u,v);
ans += w;
}
cout << ans << endl;
}
}
int main()
{
fastio;
int t = 1;
// cin >> t;
rep1(i, t) {
solve(i);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
472 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
464 KB |
Output is correct |
18 |
Correct |
0 ms |
352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
472 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
464 KB |
Output is correct |
18 |
Correct |
0 ms |
352 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1437 ms |
789652 KB |
Output is correct |
21 |
Correct |
930 ms |
748988 KB |
Output is correct |
22 |
Correct |
1262 ms |
780632 KB |
Output is correct |
23 |
Correct |
1404 ms |
787676 KB |
Output is correct |
24 |
Correct |
1275 ms |
789052 KB |
Output is correct |
25 |
Correct |
1224 ms |
789368 KB |
Output is correct |
26 |
Correct |
1322 ms |
789688 KB |
Output is correct |
27 |
Correct |
1317 ms |
789892 KB |
Output is correct |
28 |
Correct |
1292 ms |
789700 KB |
Output is correct |
29 |
Correct |
939 ms |
776064 KB |
Output is correct |
30 |
Correct |
1286 ms |
789660 KB |
Output is correct |
31 |
Correct |
1356 ms |
789780 KB |
Output is correct |
32 |
Correct |
1283 ms |
789604 KB |
Output is correct |
33 |
Correct |
1288 ms |
789608 KB |
Output is correct |
34 |
Correct |
709 ms |
663648 KB |
Output is correct |
35 |
Correct |
1245 ms |
789572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Execution timed out |
5079 ms |
789316 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
472 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
464 KB |
Output is correct |
18 |
Correct |
0 ms |
352 KB |
Output is correct |
19 |
Correct |
0 ms |
468 KB |
Output is correct |
20 |
Execution timed out |
5035 ms |
10704 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
472 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
464 KB |
Output is correct |
18 |
Correct |
0 ms |
352 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1437 ms |
789652 KB |
Output is correct |
21 |
Correct |
930 ms |
748988 KB |
Output is correct |
22 |
Correct |
1262 ms |
780632 KB |
Output is correct |
23 |
Correct |
1404 ms |
787676 KB |
Output is correct |
24 |
Correct |
1275 ms |
789052 KB |
Output is correct |
25 |
Correct |
1224 ms |
789368 KB |
Output is correct |
26 |
Correct |
1322 ms |
789688 KB |
Output is correct |
27 |
Correct |
1317 ms |
789892 KB |
Output is correct |
28 |
Correct |
1292 ms |
789700 KB |
Output is correct |
29 |
Correct |
939 ms |
776064 KB |
Output is correct |
30 |
Correct |
1286 ms |
789660 KB |
Output is correct |
31 |
Correct |
1356 ms |
789780 KB |
Output is correct |
32 |
Correct |
1283 ms |
789604 KB |
Output is correct |
33 |
Correct |
1288 ms |
789608 KB |
Output is correct |
34 |
Correct |
709 ms |
663648 KB |
Output is correct |
35 |
Correct |
1245 ms |
789572 KB |
Output is correct |
36 |
Correct |
3136 ms |
790404 KB |
Output is correct |
37 |
Correct |
2354 ms |
750356 KB |
Output is correct |
38 |
Correct |
2903 ms |
781204 KB |
Output is correct |
39 |
Correct |
3071 ms |
788300 KB |
Output is correct |
40 |
Correct |
3060 ms |
789688 KB |
Output is correct |
41 |
Correct |
2998 ms |
790092 KB |
Output is correct |
42 |
Correct |
3067 ms |
790060 KB |
Output is correct |
43 |
Correct |
3065 ms |
790148 KB |
Output is correct |
44 |
Correct |
2530 ms |
790228 KB |
Output is correct |
45 |
Correct |
1879 ms |
776572 KB |
Output is correct |
46 |
Correct |
3055 ms |
790196 KB |
Output is correct |
47 |
Correct |
3057 ms |
790332 KB |
Output is correct |
48 |
Correct |
3182 ms |
790448 KB |
Output is correct |
49 |
Correct |
3021 ms |
790256 KB |
Output is correct |
50 |
Correct |
1070 ms |
664112 KB |
Output is correct |
51 |
Correct |
2606 ms |
790044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
472 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
464 KB |
Output is correct |
18 |
Correct |
0 ms |
352 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1437 ms |
789652 KB |
Output is correct |
21 |
Correct |
930 ms |
748988 KB |
Output is correct |
22 |
Correct |
1262 ms |
780632 KB |
Output is correct |
23 |
Correct |
1404 ms |
787676 KB |
Output is correct |
24 |
Correct |
1275 ms |
789052 KB |
Output is correct |
25 |
Correct |
1224 ms |
789368 KB |
Output is correct |
26 |
Correct |
1322 ms |
789688 KB |
Output is correct |
27 |
Correct |
1317 ms |
789892 KB |
Output is correct |
28 |
Correct |
1292 ms |
789700 KB |
Output is correct |
29 |
Correct |
939 ms |
776064 KB |
Output is correct |
30 |
Correct |
1286 ms |
789660 KB |
Output is correct |
31 |
Correct |
1356 ms |
789780 KB |
Output is correct |
32 |
Correct |
1283 ms |
789604 KB |
Output is correct |
33 |
Correct |
1288 ms |
789608 KB |
Output is correct |
34 |
Correct |
709 ms |
663648 KB |
Output is correct |
35 |
Correct |
1245 ms |
789572 KB |
Output is correct |
36 |
Correct |
1 ms |
344 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Execution timed out |
5079 ms |
789316 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |