제출 #378627

#제출 시각아이디문제언어결과실행 시간메모리
378627i_am_noobEvacuation plan (IZhO18_plan)C++17
100 / 100
985 ms60644 KiB
#include<bits/stdc++.h> using namespace std; #define int long long #define rep(n) rep1(i,n) #define rep1(i,n) rep2(i,0,n) #define rep2(i,a,b) for(int i=a; i<b; ++i) #define rep3(i,a,b) for(int i=a; i>=b; --i) #define sz(a) ((int)a.size()) #define all(a) a.begin(),a.end() #define pow2(x) (1ll<<(x)) #define pii pair<int,int> #define pb push_back #ifdef i_am_noob #define bug(...) cerr << "#" << __LINE__ << ' ' << #__VA_ARGS__ << "- ", _do(__VA_ARGS__) template<typename T> void _do(T && x){cerr << x << endl;} template<typename T, typename ...S> void _do(T && x, S&&... y){cerr << x << ' ',_do(y...);} #else #define bug(...) 826 #endif const int maxn=100005; const int mod=1000000007; int n,m,k,dis[maxn],q,ans[maxn]; vector<int> vec; vector<pii> adj[maxn]; priority_queue<pii,vector<pii>,greater<pii>> pq; bool vis[maxn]; int par[maxn]; unordered_set<int> st[maxn]; int Find(int x){return x==par[x]?x:par[x]=Find(par[x]);} void Union(int x, int y, int t){ x=Find(x),y=Find(y); if(x==y) return; if(sz(st[x])<sz(st[y])) swap(x,y); for(auto z: st[y]){ if(st[x].count(z)){ ans[z]=t; st[x].erase(z); } else st[x].insert(z); } par[y]=x; } signed main(){ ios_base::sync_with_stdio(0),cin.tie(0); #ifdef i_am_noob freopen("input1.txt","r",stdin); freopen("output1.txt","w",stdout); freopen("output2.txt","w",stderr); #endif cin >> n >> m; rep(m){ int u,v,w; cin >> u >> v >> w; u--,v--; adj[u].pb({v,w}),adj[v].pb({u,w}); } cin >> k; rep(k){ int x; cin >> x; x--; pq.push({0,x}); } while(!pq.empty()){ auto [d,u]=pq.top(); pq.pop(); if(vis[u]) continue; vis[u]=1; dis[u]=d; vec.pb(u); for(auto& [v,w]: adj[u]) pq.push({d+w,v}); } reverse(all(vec)); cin >> q; rep(q){ int u,v; cin >> u >> v; u--,v--; st[u].insert(i),st[v].insert(i); } rep(n) par[i]=i; memset(vis,0,sizeof vis); for(auto u: vec){ vis[u]=1; for(auto& [v,w]: adj[u]) if(vis[v]) Union(u,v,dis[u]); } rep(q) cout << ans[i] << "\n"; }
#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...