Submission #863995

#TimeUsernameProblemLanguageResultExecution timeMemory
863995nnhzzzEvacuation plan (IZhO18_plan)C++14
100 / 100
551 ms60256 KiB
// cre: Nguyen Ngoc Hung - Train VOI 2024 #include<bits/stdc++.h> using namespace std; #define __nnhzzz__ signed main() #define BIT(i,j) ((i>>j)&1LL) #define MASK(i) (1LL<<i) #define ALL(x) (x).begin(),(x).end() #define SZ(x) (int)(x).size() #define gcd __gcd #define fi first #define se second #define ll long long #define ull unsigned long long #define ld long double #define vi vector<int> #define vvi vector<vi> #define vvvi vector<vvi> #define pii pair<int,int> #define vpii vector<pii> #define REP(i,a,b) for(int i = (a); i<=(b); ++i) #define REPD(i,a,b) for(int i = (a); i>=(b); --i) #define REPDIS(i,a,b,c) for(int i = (a); i<=(b); i+=c) #define endl "\n" #define int ll #define MP make_pair //-------------------------------------------------------------// const int oo = 1e9,LOG = 20,MAXN = 2e5+7,N = 1e2+3,inf = 1e15; const int MOD = 1e9+7,MOD1 = 1e9+207,MOD2 = 1e9+407,MOD3 = 998244353; //-------------------------------------------------------------// template<typename T> bool mini(T &a, const T &b){if(a>b){a=b;return true;}return false;} template<typename T> bool maxi(T &a, const T &b){if(a<b){a=b;return true;}return false;} /* ---------------------------------------------------------------- END OF TEMPLATE ---------------------------------------------------------------- Nguyen Ngoc Hung - nnhzzz Training for VOI24 gold medal ---------------------------------------------------------------- */ int res[MAXN]; vpii adj[MAXN]; vi spec; int n,m,k,q,now = 0; struct DSU { vi par; vector<set<int>> se; int n; int find(int u){ return u==par[u]?u:par[u]=find(par[u]); } int same(int u, int v){ return find(u)==find(v); } bool unite(int u, int v){ u = find(u); v = find(v); if(u==v) return false; if(SZ(se[u])<SZ(se[v])) swap(u,v); vi tmp; for(const auto &i:se[v]){ if(SZ(se[u])==0) break; auto it = se[u].find(i); if(it==se[u].end()){ se[u].insert(i); continue; } se[u].erase(it); tmp.push_back(i); res[i] = now; } for(const auto &i:tmp) se[v].erase(i); par[v] = u; se[v].clear(); return true; } DSU(int _n):n(_n){ par.assign(n+3,0); iota(ALL(par),0); se.assign(n+3,set<int>()); } }; void solve(){ cin >> n >> m; REP(i,1,m){ int u,v,w; cin >> u >> v >> w; adj[u].emplace_back(v,w); adj[v].emplace_back(u,w); } cin >> k; REP(i,1,k){ int u; cin >> u; spec.push_back(u); } priority_queue<pii,vpii,greater<pii>> heap; vi dis(n+3,inf); for(const auto &u:spec){ dis[u] = 0; heap.emplace(0,u); } while(SZ(heap)!=0){ int w1 = heap.top().fi,u = heap.top().se; heap.pop(); if(dis[u]!=w1) continue; REP(i,0,SZ(adj[u])-1){ int w = adj[u][i].se,v = adj[u][i].fi; if(mini(dis[v],dis[u]+w)){ heap.emplace(dis[v],v); } } } vector<pair<int,pii>> e; REP(u,1,n){ REP(i,0,SZ(adj[u])-1){ int v = adj[u][i].fi; if(u>v) continue; e.push_back(MP(min(dis[u],dis[v]),MP(u,v))); } } sort(ALL(e)); reverse(ALL(e)); DSU dsu(n); cin >> q; REP(i,1,q){ int s,t; cin >> s >> t; dsu.se[s].insert(i); dsu.se[t].insert(i); } REP(i,0,SZ(e)-1){ int w = e[i].fi,u = e[i].se.fi,v = e[i].se.se; if(dsu.same(u,v)==false){ now = w; dsu.unite(u,v); } } REP(i,1,q) cout << res[i] << endl; } __nnhzzz__{ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define name "test" if(fopen(name".inp","r")){ freopen(name".inp","r",stdin); freopen(name".out","w",stdout); } #define name1 "nnhzzz" if(fopen(name1".inp","r")){ freopen(name1".inp","r",stdin); freopen(name1".out","w",stdout); } int test = 1; while(test--){ solve(); } cerr << "\nTime elapsed: " << 1000*clock()/CLOCKS_PER_SEC << "ms\n"; return 0; }

Compilation message (stderr)

plan.cpp: In function 'int main()':
plan.cpp:152:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  152 |         freopen(name".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
plan.cpp:153:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  153 |         freopen(name".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
plan.cpp:157:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  157 |         freopen(name1".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
plan.cpp:158:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  158 |         freopen(name1".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#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...