제출 #1199665

#제출 시각아이디문제언어결과실행 시간메모리
1199665mychecksedadReconstruction Project (JOI22_reconstruction)C++20
70 / 100
5094 ms15328 KiB
/* Author : Mychecksdead */ #include<bits/stdc++.h> using namespace std; #define ll long long int #define MOD (1000000000+7) #define MOD1 (998244353) #define pb push_back #define all(x) x.begin(), x.end() #define en cout << '\n' #define ff first #define ss second #define pii pair<int,int> #define vi vector<int> const int N = 1e6+100, M = 1e5+10, K = 52, MX = 30; int n, m, q, dep[N], L[N], R[N]; pii g[501][501], par[N]; vector<array<int, 3>> E; bitset<501> vis; int ptr[501]; void dfs(int v, int p){ vis[v] = 1; dep[v] = dep[p] + 1; for(int i = 0; i < ptr[v]; ++i){ auto [u, idx] = g[v][i]; if(!vis[u]){ par[u] = {v, idx}; dfs(u, v); } } } int get(int u, int v){ int min_w = -1; while(u != v){ if(par[u].ff == u && par[v].ff == v) return -1; if(dep[u] > dep[v]){ min_w = min_w == -1 ? par[u].ss : (E[par[u].ss][0] < E[min_w][0] ? par[u].ss : min_w); u = par[u].ff; }else{ min_w = min_w == -1 ? par[v].ss : (E[par[v].ss][0] < E[min_w][0] ? par[v].ss : min_w); v = par[v].ff; } } return min_w; } void solve(){ cin >> n >> m; for(int i = 1; i <= m; ++i){ int u, v, w; cin >> u >> v >> w; E.pb({w, u, v}); } sort(all(E)); cerr << "edge: \n"; for(auto [w, u, v]: E){ cerr << w << ' ' << u << ' ' << v << '\n'; } cerr << '\n'; vector<int> C; // current edges for(int j = 0; j < m; ++j){ auto [w, u, v] = E[j]; // for(int x: C) cout << x << endl; // cout << endl; for(int i = 1; i <= n; ++i){ ptr[i] = 0; } for(auto idx: C){ auto [W, U, V] = E[idx]; g[U][ptr[U]++] = {V, idx}; g[V][ptr[V]++] = {U, idx}; } vis = 0; for(int i = 1; i <= n; ++i){ if(!vis[i]){ dep[i] = 0; par[i] = {i, 0}; dfs(i, i); } } int minimal = get(u, v); if(minimal == -1){ L[j] = 0; C.pb(j); }else{ for(int i = 0; i < C.size(); ++i){ if(C[i] == minimal){ C.erase(C.begin() + i); break; } } C.pb(j); R[minimal] = (E[j][0] + E[minimal][0]) / 2; L[j] = (E[j][0] + E[minimal][0] + 2) / 2; } } for(int idx: C) R[idx] = MOD; // for(int j = 0; j < m; ++j) cout << L[j] << ' ' << R[j] << '\n'; cin >> q; for(int i = 1; i <= q; ++i){ ll x; cin >> x; ll sum = 0; for(int j = 0; j < m; ++j){ if(L[j] <= x && x <= R[j]){ sum += abs(E[j][0] - x); } } cout << sum << '\n'; } } int main(){ cin.tie(0); ios::sync_with_stdio(0); int tt = 1, aa; // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); while(tt--){ solve(); } cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...