Submission #554841

#TimeUsernameProblemLanguageResultExecution timeMemory
554841Sam_a17Evacuation plan (IZhO18_plan)C++17
0 / 100
272 ms52508 KiB
#define _CRT_SECURE_NO_WARNINGS #include <bits/stdc++.h> // #include "temp.cpp" #include <cstdio> using namespace std; #ifndef ONLINE_JUDGE #define dbg(x) cerr << #x <<" "; print(x); cerr << endl; #else #define dbg(x) #endif #define sz(x) (int((x).size())) #define len(x) (int)x.length() #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define clr(x) x.clear() #define uniq(x) x.resize(unique(all(x)) - x.begin()); #define blt __builtin_popcount #define pb push_back #define pf push_front #define popf pop_front #define popb pop_back void print(long long t) {cerr << t;} void print(int t) {cerr << t;} void print(string t) {cerr << t;} void print(char t) {cerr << t;} void print(double t) {cerr << t;} void print(long double t) {cerr << t;} void print(unsigned long long t) {cerr << t;} template <class T> void print(set <T> v); template <class T> void print(vector <T> v); template <class T> void print(multiset <T> v); template <class T, class V> void print(map <T, V> v); template <class T, class V> void print(pair <T, V> p); template <class T, class V> void print(T v[],V n) {cerr << "["; for(int i = 0; i < n; i++) {print(v[i]); cerr << " "; } cerr << "]";} template <class T, class V> void print(pair <T, V> p) {cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}";} template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";} template <class T> void print(deque<T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";} template <class T> void print(set <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";} template <class T> void print(multiset <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";} template <class T, class V> void print(map <T, V> v) {cerr << "[ "; for (auto i : v) {print(i); cerr << " ";} cerr << "]";} template <class T, class V> void print(unordered_map<T, V> v) {cerr << "[ "; for (auto i : v) {print(i); cerr << " ";} cerr << "]";} #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; #define nl '\n' // for grid problems int dx[8] = {-1,0,1,0,1,-1,1,-1}; int dy[8] = {0,1,0,-1,1,1,-1,-1}; // lowest / (1 << 17) >= 1e5 / (1 << 18) >= 2e5 / (1 << 21) >= 1e6 void fastIO() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); } // file in/out void setIO(string str = "") { fastIO(); if (str != "" && str != "input") { freopen((str + ".in").c_str(), "r", stdin); freopen((str + ".out").c_str(), "w", stdout); } if(str == "input") { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); } } // Indexed Set template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; const int N = 1e5 + 10, inf = 1e8, LOG = sizeof(int) * 8 - 2; int n, m, k, dist[N], vis[N], p[N], sz[N], depths[N]; int up[N][LOG], mini[N][LOG]; vector <pair<int, int>> adj[N], g[N]; vector <int> stations; set <int> st[N]; void djikstra() { for(int i = 1; i <= n; i++) { dist[i] = inf; } priority_queue<pair<int, int>> q; for(int i = 0; i < k; i++) { dist[stations[i]] = 0; q.push({0, stations[i]}); } while(!q.empty()) { auto a = q.top(); q.pop(); if(vis[a.second]) continue; vis[a.second] = true; for(auto u: adj[a.second]) { if(dist[a.second] + u.second < dist[u.first]) { dist[u.first] = dist[a.second] + u.second; q.push({-dist[u.first], u.first}); } } } } int find(int a) { if(a != p[a]) { p[a] = find(p[a]); } return p[a]; } int merge(int x, int y) { x = find(x), y = find(y); if(x == y) { return 0; } if(sz[x] > sz[y]) { swap(x, y); } p[y] = x, sz[x] += sz[y]; return 1; } void dfs(int node, int parent) { for(auto u: g[node]) { if(u.first == parent) continue; up[u.first][0] = node; mini[u.first][0] = u.second; for(int i = 1; i < LOG; i++) { up[u.first][i] = up[up[u.first][i - 1]][i - 1]; mini[u.first][i] = min(mini[u.first][i - 1], mini[up[u.first][i - 1]][i - 1]); } depths[u.first] = depths[node] + 1; dfs(u.first, node); } } int lca(int a, int b) { if(a == b) { return 0; } if(depths[a] > depths[b]) { swap(a, b); } int delta = depths[b] - depths[a]; int answ = inf; for(int i = 0; i < LOG; i++) { if(delta & (1 << i)) { answ = min(answ, mini[b][i]); b = up[b][i]; } } if(a == b) { if(answ == inf) answ = 0; return answ; } for(int i = LOG - 1; i >= 0; i--) { if(up[a][i] != up[b][i]) { answ = min(answ, up[b][i]); a = up[a][i], b = up[b][i]; } } if(answ == inf) answ = 0; return answ; } void solve_() { cin >> n >> m; for(int i = 1; i <= m; i++) { int a, b, c; cin >> a >> b >> c; adj[a].push_back({b, c}); adj[b].push_back({a, c}); } cin >> k; for(int i = 1; i <= k; i++) { int a; cin >> a; stations.push_back(a); } djikstra(); vector < pair<int, pair<int, int> > > edge; for(int i = 1; i <= n; i++) { p[i] = i, sz[i] = 1; } for(int i = 1; i <= n; i++) { for (auto j: adj[i]) { edge.push_back({min(dist[i], dist[j.first]), {i, j.first}}); } } sort(rall(edge)); for(auto u: edge) { int a = u.second.first, b = u.second.second; int c = u.first; if(merge(a, b)) { g[a].push_back({b, c}); g[b].push_back({a, c}); } } dfs(1, 0); int q; cin >> q; while(q--) { int a, b; cin >> a >> b; printf("%d\n", lca(a, b)); } } int main() { setIO(""); auto solve = [&](int test_case)-> void { while(test_case--) { solve_(); } }; int test_cases = 1; solve(test_cases); return 0; }

Compilation message (stderr)

plan.cpp: In function 'void setIO(std::string)':
plan.cpp:67:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |     freopen((str + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
plan.cpp:68:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   68 |     freopen((str + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
plan.cpp:72:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |     freopen("input.txt", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
plan.cpp:73:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |     freopen("output.txt", "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...