Submission #82628

#TimeUsernameProblemLanguageResultExecution timeMemory
82628GoodMin-max tree (BOI18_minmaxtree)C++11
100 / 100
366 ms67244 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #define ff first #define ss second #define Maxn 70009 #define ll long long #define pb push_back #define Inf 1000000009 #define ppb() pop_back() #define pii pair <int , int> #define mid(x, y) (x + y) / 2 #define all(x) x.begin(),x.end() #define llInf 1000000000000000009 #define tr(i, c) for(__typeof(c).begin() i = (c).begin() ; i != (c).end() ; i++) using namespace std; using namespace __gnu_pbds; typedef tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update> order; char c[Maxn]; bool vis[Maxn]; int n, q; int T[Maxn]; int lvl[Maxn]; int mat[Maxn][20]; int l[Maxn], r[Maxn]; int E[Maxn][2], P[Maxn][2]; int x[Maxn], y[Maxn], z[Maxn]; vector <int> M[Maxn]; vector <int> adj[Maxn]; vector <pair <int, pair <int, pii> > > A[2]; map <int, int> mp; void dfs (int nd, int p, int d) { mat[nd][0] = p; lvl[nd] = lvl[p] + 1; if (d == 1 and nd != 1) { if (E[nd][0]) M[nd].pb (E[nd][0]); if (E[nd][1]) M[nd].pb (E[nd][1]); } if (d == 2 and nd != 1) { if (!l[nd]) printf ("%d %d %d\n", p, nd, T[E[nd][1]]); else printf ("%d %d %d\n", p, nd, T[l[nd]]); } for (auto i: adj[nd]) if (i != p) dfs (i, nd, d); } bool dfs1 (int nd) { if (vis[nd]) return 0; vis[nd] = 1; for (auto i: M[nd]) if (!r[i] or dfs1 (r[i])) { l[nd] = i; r[i] = nd; return 1; } return 0; } void build () { for (int i = 1; i <= 18; i++) for (int j = 1; j <= n; j++) if (mat[j][i - 1] != -1) mat[j][i] = mat[mat[j][i - 1]][i - 1]; } int LCA (int a, int b) { if (lvl[a] < lvl[b]) swap (a, b); for (int i = 18; i >= 0; i--) if (lvl[mat[a][i]] >= lvl[b]) a = mat[a][i]; if (a == b) return a; for (int i = 18; i >= 0; i--) if (mat[a][i] != -1 and mat[a][i] != mat[b][i]) a = mat[a][i], b = mat[b][i]; return mat[a][0]; } int dsu (int x, bool d) { if (P[x][d] == x) return x; return P[x][d] = dsu (P[x][d], d); } void solve (int x, int y, int z, bool d) { x = dsu (P[x][d], d); if (lvl[x] <= lvl[y]) return; E[x][d] = z; if (mat[x][0] != -1) P[x][d] = dsu (mat[x][0], d); x = P[x][d]; solve (x, y, z, d); } int main () { //freopen ("file.in", "r", stdin); //freopen ("file.out", "w", stdout); //srand ((unsigned) time ( NULL )); //int randomNumber = rand() % 10 + 1; scanf ("%d", &n); for (int i = 1; i < n; i++) { int u, v; scanf ("%d%d", &u, &v); adj[u].pb (v); adj[v].pb (u); P[i][0] = P[i][1] = i; } P[n][0] = P[n][1] = n; memset (mat, -1, sizeof (mat)); dfs (1, -1, 0); build (); scanf ("%d", &q); for (int i = 1; i <= q; i++) { scanf (" %c%d%d%d", &c[i], &x[i], &y[i], &z[i]); mp[z[i]] = 1; } int cnt = 0; for (auto i: mp) mp[i.ff] = ++cnt; for (int i = 1; i <= q; i++) { int l = LCA (x[i], y[i]); if (c[i] == 'M') A[0].pb ({mp[z[i]], {l, {x[i], y[i]}}}); else A[1].pb ({mp[z[i]], {l, {x[i], y[i]}}}); T[mp[z[i]]] = z[i]; } sort (all (A[0])); sort (all (A[1])); reverse (all (A[1])); for (auto i: A[0]) { solve (i.ss.ss.ff, i.ss.ff, i.ff, 0); solve (i.ss.ss.ss, i.ss.ff, i.ff, 0); } for (auto i: A[1]) { solve (i.ss.ss.ff, i.ss.ff, i.ff, 1); solve (i.ss.ss.ss, i.ss.ff, i.ff, 1); } dfs (1, -1, 1); bool ok = 1; while (ok) { ok = 0; memset (vis, 0, sizeof (vis)); for (int i = 2; i <= n; i++) if (!l[i] and dfs1 (i)) ok = 1; } dfs (1, -1, 2); return 0; }

Compilation message (stderr)

minmaxtree.cpp: In function 'int main()':
minmaxtree.cpp:131:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf ("%d", &n);
  ~~~~~~^~~~~~~~~~
minmaxtree.cpp:134:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf ("%d%d", &u, &v);
   ~~~~~~^~~~~~~~~~~~~~~~
minmaxtree.cpp:147:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf ("%d", &q);
  ~~~~~~^~~~~~~~~~
minmaxtree.cpp:150:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf (" %c%d%d%d", &c[i], &x[i], &y[i], &z[i]);
   ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...