# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
202282 | Rakhmand | Mergers (JOI19_mergers) | C++14 | 1731 ms | 130956 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//
// ROIGold.cpp
// Main calisma
//
// Created by Rakhman on 05/02/2019.
// Copyright © 2019 Rakhman. All rights reserved.
//
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <queue>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <iterator>
#define ios ios_base::sync_with_stdio(0), cout.tie(0), cin.tie(0);
#define S second
#define F first
#define pb push_back
#define nl '\n'
#define NL cout << '\n';
#define EX exit(0)
#define all(s) s.begin(), s.end()
#define FOR(i, start, finish, k) for(int i = start; i <= finish; i += k)
const int MXN = 5e5 + 10;
const long long MNN = 1e6 + 200;
const long long MOD = 1e9 + 7;
const long long INF = 1e16;
const int OO = 1e9 + 10;
typedef long long llong;
typedef unsigned long long ullong;
using namespace std;
void istxt(bool yes){
if(yes == 1){
freopen("balancing.in", "r", stdin);
freopen("balancing.out", "w", stdout);
}else{
freopen("/Users/rakhmanabdirashov/Desktop/folder/Programming/Road2Master/Road2Master/input.txt", "r", stdin);
}
}
int n, k, dp[MXN][20], lvl[MXN], cnt[MXN], p[MXN], lol[MXN];
vector<int> g[MXN];
vector<int> state[MXN];
void dfs(int x, int p){
dp[x][0] = p;
for(int i = 1; i < 20; i++) dp[x][i] = dp[dp[x][i - 1]][i - 1];
lvl[x] = lvl[p] + 1;
for(int i = 0; i < g[x].size(); i++) if(g[x][i] != p) dfs(g[x][i], x);
}
int lca(int u, int v){
if(lvl[u] < lvl[v]) swap(u, v);
int dist = lvl[u] - lvl[v];
for(int i = 0; i < 20; i++)
if((1 << i) & dist)
u = dp[u][i];
if(u == v) return u;
for(int i = 19; i >= 0; i--){
if(dp[u][i] != dp[v][i]){
u = dp[u][i];
v = dp[v][i];
}
}
return dp[v][0];
}
int find(int x) { return p[x] = (x == p[x] ? x : find(p[x])); }
void attach(int u, int v) {u = find(u), v = find(v), p[u] = v; }
void dfs2(int v, int p){
for(int i = 0; i < g[v].size(); i++){
int to = g[v][i];
if(to == p) continue;
dfs2(to, v);
cnt[v] += cnt[to];
}
if(v != 1 && cnt[v]) attach(p, v);
}
int main () {
ios;
//istxt(0);
cin >> n >> k;
for(int i = 1; i < n; i++){
int u, v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
dfs(1, 0);
for(int i = 1; i <= n; i++){
p[i] = i;
int x;
cin >> x;
state[x].pb(i);
}
for(int st = 1; st <= k; st++){
for(int j = 1; j < state[st].size(); j++){
int u = state[st][j - 1], v = state[st][j];
int batya = lca(u, v);
cnt[u]++, cnt[v]++, cnt[batya]--, cnt[batya]--;
}
}
dfs2(1, 1);
for(int i = 2; i <= n; i++){
int batya = dp[i][0];
if(find(batya) == find(i)) continue;
lol[find(batya)]++;
lol[find(i)]++;
}
int ans = 0;
for(int i = 1; i <= n; i++){
if(lol[i] == 1) ans++;
}
if(ans == 1) ans = 0;
ans++;
cout << ans / 2;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |