이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define pb push_back
#define ii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define INF 1000000000//00000000
#define modulo 1000000007
#define mod 998244353
//#define int long long int
using namespace std;
struct Tree{
vector<vector<int> >& adj;
vector<vector<int> > anc;
vector<int> depth;
int n;
int root;
Tree(vector<vector<int> >& a, int N = 1e6, int r = 1): adj(a){
n = N + 5;
root = r;
anc = vector<vector<int> > (25, vector<int>(n, 0));
depth = vector<int>(n, 0);
init(root, -1);
for(int d = 1; d < 25; d++){
for(int x = 0; x < n; x++){
anc[d][x] = anc[d - 1][anc[d - 1][x]];
}
}
}
void init(int x, int pre){
for(int i = 0; i < adj[x].size(); i++){
int y = adj[x][i];
if(y == pre)continue;
anc[0][y] = x;
depth[y] = depth[x] + 1;
init(y, x);
}
}
int LCA(int x, int y){
if(depth[x] < depth[y])swap(x,y);
while(depth[x] != depth[y]){
int diff = depth[x] - depth[y];
diff = log2(diff&-diff);
x = anc[diff][x];
}
if(x == y) return x;
for(int i = 24; i >= 0; i--){
if(anc[i][x] != anc[i][y]){
x = anc[i][x];
y = anc[i][y];
}
}
return anc[0][x];
}
int dist(int x, int y){
int l = LCA(x, y);
return depth[x] + depth[y] - 2 * depth[l];
}
};
bool operator< (ii a, ii b){
return a.first < b.first || (a.first == b.first && a.second < b.second);
}
vector<vector<int> > adj(500005);
vector<vector<int> > id(500005);
vector<int> val(500005);
vector<int> comp[500005];
vector<int> up(500005);
vector<int> sub(500005);
vector<bool> virt(500005, false);
int num(int x, int y){
return id[x][y];
}
void dfs(int x, int pre, vector<int>& depth, int id){
for(int i = 0; i < adj[x].size(); i++){
int y = adj[x][i];
if(y == pre) continue;
dfs(y, x, depth, i);
if(depth[up[x]] > depth[up[y]])
up[x] = up[y];
}
if(pre != 0 && up[x] != x){
virt[num(pre, id)] = true;
}
}
void get(int x, int pre, int& ans, int root, int id){
sub[x] = 0;
if(pre != 0 && virt[num(pre, id)] == false) sub[x] = 1;
for(int i = 0; i < adj[x].size(); i++){
int y = adj[x][i];
if(y == pre)continue;
get(y, x, ans, root, i);
sub[x] += sub[y];
}
if(pre != 0 && pre != root && virt[num(pre, id)] == false && sub[x] == 1) ans++;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, k;
cin >> n >> k;
if(n == 1){
cout << 0;
return 0;
}
for(int i = 0; i < n - 1; i++) {
int x, y;
cin >> x >> y;
adj[x].pb(y);
adj[y].pb(x);
id[x].pb(i);
id[y].pb(i);
}
for(int i = 1; i <= n; i++){
cin >> val[i];
comp[val[i]].pb(i);
up[i] = i;
}
Tree A(adj);
for(int i = 0; i < 500005; i++){
if(comp[i].empty()) continue;
int l = comp[i][0];
for(auto x : comp[i]){
l = A.LCA(x, l);
}
for(auto x : comp[i]){
up[x] = l;
}
}
dfs(1, 0, A.depth, -1);
int root = 0;
for(int i = 1; i <= n; i++) {
for(int j = 0; j < adj[i].size(); j++){
int x = i;
int y = adj[i][j];
int w = num(i, j);
if(virt[w]) continue;
if(A.depth[x] < A.depth[y]) swap(x, y);
if((root == 0 || A.depth[x] > A.depth[root])) root = x;
}
}
int ans = 0;
get(root, 0, ans, root, -1);
if(root) ans++;
cout << (ans + 1) / 2;
}
컴파일 시 표준 에러 (stderr) 메시지
mergers.cpp: In member function 'void Tree::init(int, int)':
mergers.cpp:40:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < adj[x].size(); i++){
~~^~~~~~~~~~~~~~~
mergers.cpp: In function 'void dfs(int, int, std::vector<int>&, int)':
mergers.cpp:116:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < adj[x].size(); i++){
~~^~~~~~~~~~~~~~~
mergers.cpp: In function 'void get(int, int, int&, int, int)':
mergers.cpp:131:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < adj[x].size(); i++){
~~^~~~~~~~~~~~~~~
mergers.cpp: In function 'int32_t main()':
mergers.cpp:179:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < adj[i].size(); j++){
~~^~~~~~~~~~~~~~~
# | 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... |