Submission #1016207

#TimeUsernameProblemLanguageResultExecution timeMemory
1016207phongUnique Cities (JOI19_ho_t5)C++17
100 / 100
520 ms108032 KiB
#include<bits/stdc++.h> #define ll long long const int nmax = 2e5 + 5, N = 4e5; const ll oo = 1e9; const int lg = 18, M = 4e3; const int base = 2e5, mod = 1e9 + 7; #define pii pair<int, int> #define fi first #define se second #define endl "\n" #define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' ';cout << endl using namespace std; int n, m; vector<int> adj[nmax]; int h[nmax], a[nmax], dep[nmax][2], H[nmax]; void dfs_1(int u, int p){ for(auto v : adj[u]){ if(v == p) continue; H[v] = H[u] + 1; dfs_1(v, u); } } void ckmax(int &a, int b){ a = max(a, b); } vector<int> gg[nmax]; int sz[nmax], up[nmax][lg + 1], mx[nmax]; int nchain = 1, Time = 0, head[nmax], ind[nmax], pos[nmax]; void dfs_4(int u, int p, int id){ sz[u] = 1; for(auto v : adj[u]){ if(v == p) continue; dep[v][id] = dep[u][id] + 1; dfs_4(v, u, id); sz[u] += sz[v]; } } int two[nmax]; void dfs_2(int u, int p){ if(!head[nchain])head[nchain] = u; ind[u] = nchain; pos[u] = ++Time; two[Time] = u; int idx = -1; for(auto v : adj[u]){ if(v == p) continue; up[v][0] = u; for(int j = 1; j <= lg; ++j) up[v][j] = up[up[v][j - 1]][j - 1]; if(idx == -1 || sz[v] > sz[idx]){ idx = v; } } if(idx != -1){ int v = idx; dfs_2(v, u); h[u] = max(h[u], h[v] + 1); } for(auto v : adj[u]){ if(v == p || v == idx) continue; ++nchain; dfs_2(v, u); h[u] = max(h[u], h[v] + 1); } int ma = 0; for(auto v : adj[u]){ if(v == p) continue; mx[v] = max(mx[v], ma); ma = max(ma, h[v] + 1); } reverse(adj[u].begin(), adj[u].end()); ma = 0; for(auto v : adj[u]){ if(v == p) continue; mx[v] = max(mx[v], ma); ma = max(ma, h[v] + 1); } sort(adj[u].begin(), adj[u].end(),[](int a, int b){ return mx[a] > mx[b]; }); } int tree[nmax << 2], lz[nmax << 2]; void build(int id, int l, int r){ tree[id] = lz[id] = 0; if(l == r){ return; } int mid = r + l >> 1; build(id << 1, l, mid); build(id << 1 | 1, mid + 1, r); } void fix(int id, int val){ tree[id] += val; lz[id] += val; } void down(int id){ fix(id << 1,lz[id]); fix(id << 1 | 1, lz[id]); lz[id] = 0; } void Update(int id, int l, int r, int u, int v, int val){ if(l > v || r < u) return; if(l >= u && r <= v){ return fix(id, val); } down(id); int mid = r + l >> 1; Update(id << 1, l, mid, u, v, val); Update(id << 1 | 1, mid + 1, r, u, v, val); tree[id] = max(tree[id << 1], tree[id << 1 | 1]); } #define Update(u, v, ma) Update(1, 1, n, u, v, ma) int Get(int id, int l, int r, int u){ if(l == r) return tree[id]; down(id); int mid = r + l >> 1; if(u <= mid) return Get(id << 1, l, mid, u); return Get(id << 1 | 1, mid + 1, r, u); } #define Get(u) Get(1, 1, n, u) int siz = 0; vector<pii> active(nmax); int get(int x){ if(x < 0) return 0; int l = 1, r = siz, kq = siz; while(l <= r){ int mid = r + l >> 1; if(active[mid].fi > x){ kq = mid - 1; r = mid - 1; } else l = mid + 1; } return kq; } struct node{ int id, x, y, z; }; int first_scc[nmax], ans[nmax][2]; void update(int u, int a,int id, int val){ if(dep[u][id] < dep[a][id] || dep[a][id] == -1) return; // cerr << dep[u][id] << "#" << dep[a][id] << ' '; int uchain = ind[u], achain = ind[a]; // cout << u << ' ' << a << ' ' << val << endl; while(1){ // cerr << u << ' ' << a << endl; if(achain == uchain){ Update(pos[a], pos[u], val); // cout << tree[1] << endl; return; } Update(pos[head[uchain]], pos[u], val); u = up[head[uchain]][0]; uchain = ind[u]; } // cout << endl; } /* 5 4 1 2 2 3 3 4 3 5 1 2 1 2 4 */ bool check(int u){ if(u == 0 || Get(pos[u]) < 0) return 1; return 0; } int fly(int u, int k){ for(int j = 0; j <= lg; ++j){ if(k >> j & 1){ u = up[u][j]; } } return u; } void dfs_3(int u, int p, int id){ bool ok = 1; int idx = -1; for(auto v : adj[u]){ if(v == p) continue; if(ok){ int x = min(dep[u][id], mx[v]); int y = u; update(up[u][0], fly(u, x), id, -1); ok = 0; } else{ // if() update(fly(u, mx[v] + 1), fly(u, idx), id, 1); // if(v == 1) cout << F.get(2) << endl; } idx = min(mx[v], dep[u][id]); vector<node> tmp; int new_siz = get(max(-1, dep[u][id] - mx[v] - 1)); int x = a[u]; tmp.push_back({2, siz}); siz = new_siz; // if(v == 1) // cout << v << ' ' << tree[1] << endl; if(check(first_scc[x])){ tmp.push_back({1, x, first_scc[x]}); tmp.push_back({3, ++siz, active[siz].fi, active[siz].se}); active[siz] = {dep[u][id], x}; first_scc[x] = u; } // cout << v<<"#" << ' ' << siz<< endl; // for(int i = 1; i <= siz; ++i){ // cout << active[i].fi << ' ' << active[i].se << ' ' << first_scc[active[i].se]<<endl; // } // cout << "#" << endl; // if(v == 1) exit(0); ans[v][id] = get(dep[v][id] - h[v] - 1); // if(v == 4) exit(0); dfs_3(v, u, id); for(auto [id, x, y, z] : tmp){ if(id == 1){ first_scc[x] = y; } else if(id == 2){ siz = x; } else{ active[x] = {y, z}; } } } if(idx != -1){ update(up[u][0], fly(u, idx), id, 1); } } main(){ ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); // freopen("code.inp", "r", stdin); // freopen("code.out", "w", stdout); cin >> n >> m; for(int i = 1; i < n; ++i){ int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } for(int i = 1; i <= n; ++i) cin >> a[i]; int s, t, ma; ma = -1; dfs_1(1, -1); for(int i = 1; i <= n; ++i){ if(ma < H[i]){ ma = max(ma, H[i]); s = i; } } ma = -1; dep[0][0] = dep[0][1] = -1; dfs_1(s, -1); for(int i = 1; i <= n; ++i){ if(ma < H[i]){ ma = max(ma, H[i]); t = i; } } active[0] = {-1, 0}; dfs_4(s, -1, 0); dfs_2(s, -1); dfs_3(s, -1, 0); for(int i = 1; i <= n; ++i){ h[i] = 0, mx[i] = 0; for(int j = 0; j <= lg; ++j) up[i][j] = 0; head[i] = 0; } build(1, 1, n); nchain = 1;Time = 0; dfs_4(t, -1, 1); dfs_2(t, -1); dfs_3(t, -1, 1); for(int i = 1; i <= n; ++i) h[i] = 0, mx[i] = 0; for(int i = 1; i <= n; ++i){ if(dep[i][0] >= dep[i][1]) cout << ans[i][0]; else cout << ans[i][1]; cout << endl; } } /* */

Compilation message (stderr)

joi2019_ho_t5.cpp: In function 'void build(int, int, int)':
joi2019_ho_t5.cpp:93:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   93 |     int mid = r + l >> 1;
      |               ~~^~~
joi2019_ho_t5.cpp: In function 'void Update(int, int, int, int, int, int)':
joi2019_ho_t5.cpp:114:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  114 |     int mid = r + l >> 1;
      |               ~~^~~
joi2019_ho_t5.cpp: In function 'int Get(int, int, int, int)':
joi2019_ho_t5.cpp:124:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  124 |     int mid = r + l >> 1;
      |               ~~^~~
joi2019_ho_t5.cpp: In function 'int get(int)':
joi2019_ho_t5.cpp:136:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  136 |         int mid = r + l >> 1;
      |                   ~~^~~
joi2019_ho_t5.cpp: In function 'void dfs_3(int, int, int)':
joi2019_ho_t5.cpp:195:17: warning: unused variable 'y' [-Wunused-variable]
  195 |             int y = u;
      |                 ^
joi2019_ho_t5.cpp: At global scope:
joi2019_ho_t5.cpp:247:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  247 | main(){
      | ^~~~
joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:293:10: warning: 't' may be used uninitialized in this function [-Wmaybe-uninitialized]
  293 |     dfs_3(t, -1, 1);
      |     ~~~~~^~~~~~~~~~
joi2019_ho_t5.cpp:283:10: warning: 's' may be used uninitialized in this function [-Wmaybe-uninitialized]
  283 |     dfs_3(s, -1, 0);
      |     ~~~~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...