Submission #443749

#TimeUsernameProblemLanguageResultExecution timeMemory
443749Killer2501Mergers (JOI19_mergers)C++14
100 / 100
1338 ms213000 KiB
#include <bits/stdc++.h> #define ll long long #define pb push_back #define task "slingshot" #define pll pair<ll, ll> #define pii pair<pll, ll> #define fi first #define se second #define ull unsigned long long using namespace std; const ll mod = 2111992; const ll N = 5e5+5; const int base = 300; ll n, m, t, ans, k, a[N], b[N], c[N], tong, cnt, q, d[N], P[N][21], h[N], lab[N]; mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count()); struct point { ll x, y; }p[N]; vector<ll> dx, dy; vector<ll> adj[N], kq, gr[N]; string s[N]; ll pw(ll k, ll n) { ll total = 1; for(; n; n >>= 1) { if(n & 1)total = total * k % mod; k = k * k % mod; } return total; } ll A(ll u, ll v) { if(u > v)return 0; return b[v] * c[v-u] % mod; } ll findp(ll u) { return lab[u] < 0 ? u : lab[u] = findp(lab[u]); } void hop(ll u, ll v) { u = findp(u); v = findp(v); if(u == v)return; if(lab[u] > lab[v])swap(u, v); lab[u] += lab[v]; lab[v] = u; } void add(ll id) { for(; id <= n;id += id & -id)++d[id]; } ll get(ll id) { ll total = 0; for(; id; id -= id & -id)total += d[id]; return total; } ll lwr(ll x) { return lower_bound(kq.begin(), kq.end(), x) - kq.begin() + 1; } void dfs(ll u, ll p) { a[u] = ++m; for(int i = 1; i <= 20; i ++)P[u][i] = P[P[u][i-1]][i-1]; for(ll v : adj[u]) { if(v == p)continue; h[v] = h[u] + 1; P[v][0] = u; dfs(v, u); } } bool cmp(const ll& x, const ll& y) { return a[x] < a[y]; } ll lca(ll u, ll v) { if(h[u] < h[v])swap(u, v); ll log = log2(h[u]); for(int i = log; i >= 0; i --)if(h[u] >= h[v] + (1<<i))u = P[u][i]; if(u == v)return u; for(int i = log; i >= 0; i --) { if(P[u][i] && P[u][i] != P[v][i]) { u = P[u][i]; v = P[v][i]; } } return P[u][0]; } void cal(ll u, ll p) { for(ll v : adj[u]) { if(v == p)continue; cal(v, u); c[u] += c[v]; } if(c[u])hop(u, P[u][0]); } void sol() { cin >> n >> k; for(int i = 1; i < n; i ++) { ll x, y; cin >> x >> y; p[i].x = x; p[i].y = y; adj[x].pb(y); adj[y].pb(x); } for(int i = 1; i <= n; i ++) { cin >> b[i]; gr[b[i]].pb(i); } dfs(1, 0); for(int i = 1; i <= k; i ++) { sort(gr[i].begin(), gr[i].end(), cmp); for(int j = 1; j < gr[i].size(); j ++) { ll x = gr[i][j], y = gr[i][j-1], z = lca(x, y); ++c[x]; ++c[y]; c[z] -= 2; } } fill_n(lab, n+2, -1); cal(1, 0); for(int i = 1; i < n; i ++) { ll u = findp(p[i].x), v = findp(p[i].y); if(u != v) { ++d[u]; ++d[v]; } } for(int i = 1; i <= n; i ++)ans += (d[i] == 1); cout << (ans + 1) / 2; } int main() { if(fopen(task".in", "r")){ freopen(task".in", "r", stdin); freopen(task".out", "w", stdout); } ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int ntest = 1; //cin >> ntest; while(ntest -- > 0) sol(); }

Compilation message (stderr)

mergers.cpp: In function 'void sol()':
mergers.cpp:131:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  131 |         for(int j = 1; j < gr[i].size(); j ++)
      |                        ~~^~~~~~~~~~~~~~
mergers.cpp: In function 'int main()':
mergers.cpp:156:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  156 |        freopen(task".in", "r", stdin);
      |        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:157:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  157 |        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...