Submission #877240

#TimeUsernameProblemLanguageResultExecution timeMemory
877240wiiRigged Roads (NOI19_riggedroads)C++17
100 / 100
366 ms110520 KiB
#include <bits/stdc++.h> using namespace std; typedef double db; typedef long long ll; typedef long double ld; #define int ll typedef pair<int, int> pii; #define lx (id << 1) #define rx (lx | 1) #define gcd __gcd #define pb push_back #define all(x) (x).begin(), (x).end() #define bit(i, mask) ((mask) >> (i) & 1) #define reset(x, val) memset(x, val, sizeof(x)) #define foru(i,a,b) for(int i = (a); i <= (b); ++i) #define ford(i,a,b) for(int i = (a); i >= (b); --i) #define FastIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); template<typename T> bool maximize(T &res, const T &val) { if (res < val) { res = val; return true; } return false; } template<typename T> bool minimize(T &res, const T &val) { if (val < res) { res = val; return true; } return false; } const ll Linf = 0x3f3f3f3f3f3f3f3f; const int Inf = 0x3f3f3f3f; const ll Mod = 1e9 + 7; const ll Mod2 = ll(1e9) + 9; const int Lim = 1e6 + 5; const int inv6 = 166666668; // #define Sieve #ifdef Sieve vector<int> pr; vector<int> lpf; void Linear_sieve(int n = Lim) { pr.assign(1, 2); lpf.assign(n + 1, 2); for (int x = 3; x <= n; x += 2) { if (lpf[x] == 2) pr.push_back(lpf[x] = x); for (int i = 1; i < pr.size() && pr[i] <= lpf[x] && pr[i] * x <= n; ++i) lpf[pr[i] * x] = pr[i]; } } #endif /// ====*====*====*====*====*====*====*====*====*====*====*====*====*====*====*====*==== const int base = 3; const int N = 3e5 + 5; const int K = log2(N) + 1; const int dx[] = {+1, -1, 0, 0}; const int dy[] = {0, 0, +1, -1}; const int block_size = sqrt(2e9) + 2; int n, m; int a[N]; vector<pair<int, int>> edges; vector<pii> adj[N]; int up[N][K + 1], dep[N], par[N]; int tin[N], tout[N], timer; struct Dsu { int par[N]; Dsu() { memset(par, 0, sizeof par); } int root(int u) { return par[u] == 0 ? u : par[u] = root(par[u]); } int join(int u, int v) { if ((u = root(u)) == (v = root(v))) return false; if (dep[u] > dep[v]) swap(u, v); par[v] = u; return true; } int ok(int u, int v) { return root(u) != root(v); } }; void dfs(int u, int p) { tin[u] = ++timer; dep[u] = dep[p] + 1; up[u][0] = p; foru(k, 1, K) up[u][k] = up[up[u][k - 1]][k - 1]; for (auto [v, id]: adj[u]) if (v != p) { par[v] = id; dfs(v, u); } tout[u] = timer; } int isanc(int u, int v) { return tin[u] <= tin[v] && tout[v] <= tout[u]; } int lca(int u, int v) { if (isanc(u, v)) return u; if (isanc(v, u)) return v; ford(k, K, 0) if (up[u][k]) if (!isanc(up[u][k], v)) u = up[u][k]; return up[u][0]; } int dist(int u, int v) { return dep[u] + dep[v] - 2*dep[lca(u, v)]; } int ans[N], top; void solve() { cin >> n >> m; foru(i, 1, m) { int u, v; cin >> u >> v; edges.emplace_back(u, v); } foru(i, 1, n - 1) { int x; cin >> x; --x; int u = edges[x].first, v = edges[x].second; adj[u].emplace_back(v, x); adj[v].emplace_back(u, x); a[x] = true; } dfs(1, 0); Dsu St; for (int i = 0; i < m; ++i) if (ans[i] == 0) { int u = edges[i].first; int v = edges[i].second; int w = lca(u, v); static vector<int> f; if (!f.empty()) f.clear(); for (int x = St.root(u); dep[x] > dep[w]; x = St.root(u)) { int id = par[x]; int p = up[x][0]; f.push_back(id); St.join(x, p); } for (int x = St.root(v); dep[x] > dep[w]; x = St.root(v)) { int id = par[x]; int p = up[x][0]; f.push_back(id); St.join(x, p); } sort(all(f)); for (int &id: f) { ans[id] = ++top; } if (ans[i] == 0) ans[i] = ++top; } for (int i = 0; i < m; ++i) cout << ans[i] << " \n"[i == m]; } signed main() { FastIO; #define task "" if (fopen(task".inp", "r")) { freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); } #define task "test" if (fopen(task".inp", "r")) { freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); } #ifdef Sieve Linear_sieve(); #endif int ntest = 1; // cin >> ntest; while (ntest--) { //cout << "Case " << q << ": " << "\n"; solve(); cout << "\n"; } return 0; } /** /\_/\ * (= ._.) * / >TL \>AC **/

Compilation message (stderr)

riggedroads.cpp:195: warning: "task" redefined
  195 |     #define task "test"
      | 
riggedroads.cpp:189: note: this is the location of the previous definition
  189 |     #define task ""
      | 
riggedroads.cpp: In function 'int main()':
riggedroads.cpp:191:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  191 |   freopen(task".inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
riggedroads.cpp:192:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  192 |   freopen(task".out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
riggedroads.cpp:197:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  197 |   freopen(task".inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
riggedroads.cpp:198:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  198 |   freopen(task".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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...