# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
202271 | Rakhmand | Mergers (JOI19_mergers) | C++14 | 10 ms | 5116 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 = 1e5 + 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], used[MXN], city[MXN];
bool check[MXN];
vector<int> g[MXN];
vector<int> state[MXN];
queue<int> q;
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++){
int to = g[x][i];
if(to == p) continue;
dfs(to, 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 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++){
cin >> city[i];
state[city[i]].pb(i);
}
int ans = 0;
for(int st = 1; st <= k; st++){
if(check[st] == true){
continue;
}
q.push(st);
check[st] = 1;
while(!q.empty()){
int comp = q.front();
q.pop();
int batya;
for(int i = 0; i < state[comp].size(); i++){
int x = state[comp][i];
if(i == 0) batya = x;
else batya = lca(batya, x);
}
bool was = 1;
for(int i = 0; i < state[comp].size(); i++){
int x = state[comp][i];
if(used[x] != 0) was = 0;
while(x != batya){
if(used[x] == 0){
if(check[city[x]] == 0) {
q.push(city[x]);
check[city[x]] = 1;
}
used[x] = batya;
x = dp[x][0];
}else{
x = used[x];
was = 0;
}
}
}
ans += (was == 0 ? 0 : 1);
}
}
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... |