Submission #409663

#TimeUsernameProblemLanguageResultExecution timeMemory
409663kwongwengSwapping Cities (APIO20_swap)C++17
7 / 100
130 ms13180 KiB
#include "swap.h" #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int, int> ii; typedef vector<ii> vii; #define FOR(i, a, b) for(int i = a; i < b; i++) #define ROF(i, a, b) for(int i = a; i >= b; i--) #define ms memset #define pb push_back #define F first #define S second vector<pair<int, ii> > edges; int n, m; vi p(100001), sz(100001, 1), s(100001), e(100001), line(100001, -1), sub(100001); vi d(100001), g[100001]; int get(int a){ if (p[a] == a) return a; return get(p[a]); } void Union(int a, int b, int w){ int c = get(a); int d = get(b); if (c == d){ if (line[c] == -1) line[c] = w; return; } if (sz[c] < sz[d]){ swap(c, d); swap(a, b); } p[d] = c; sz[c] += sz[d]; sub[d] = w; if (line[c] == -1 && line[d] == -1){ if (s[c] == a && b == s[d]){ s[c] = e[d]; return; } if (s[c] == a && b == e[d]){ s[c] = s[d]; return; } if (e[c] == a && b == s[d]){ e[c] = e[d]; return; } if (e[c] == a && b == e[d]){ e[c] = s[d]; return; } line[c] = w; return; } if (line[c] == -1) line[c] = line[d]; } void init(int N, int M, vi U, vi V, vi W) { n = N; m = M; FOR(i, 0, m){ edges.pb({W[i], {U[i], V[i]}}); } FOR(i, 0, n){ p[i] = i; s[i] = i; e[i] = i; } sort(edges.begin(), edges.end()); FOR(i, 0, m){ Union(edges[i].S.F, edges[i].S.S, edges[i].F); } int head; FOR(i, 0, n){ if (p[i] != i){ g[p[i]].pb(i); continue; } head = i; } vi bfs; bfs.pb(head); FOR(i, 0, bfs.size()){ int u = bfs[i]; for (int v : g[u]){ d[v] = d[u] + 1; bfs.pb(v); } } } int getMinimumFuelCapacity(int x, int y) { int u = x; int v = y; int maxi = 0; if (d[u] < d[v]) swap(u, v); while (d[u] > d[v]){ maxi = max(maxi, sub[u]); u = p[u]; } while (u != v){ maxi = max(maxi, max(sub[u], sub[v])); u = p[u]; v = p[v]; } if (p[u] == u && line[u] == -1) return -1; if (line[u] == -1){ maxi = max(maxi, sub[u]); u = p[u]; } return max(maxi, line[u]); return -1; }

Compilation message (stderr)

swap.cpp: In function 'void init(int, int, vi, vi, vi)':
swap.cpp:8:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    8 | #define FOR(i, a, b) for(int i = a; i < b; i++)
......
   77 |     FOR(i, 0, bfs.size()){
      |         ~~~~~~~~~~~~~~~~               
swap.cpp:77:5: note: in expansion of macro 'FOR'
   77 |     FOR(i, 0, bfs.size()){
      |     ^~~
#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...