This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = (1 << 20) + 12, MOD = (int)1e9 + 7;
int n, m, a[N], vis[N], timer, p[N];
vector<int> g[N];
int f(int d) {
timer++;
vis[d] = timer;
int lst = d;
queue<int> q;
q.push(d);
while(!q.empty()) {
int v = q.front();
lst = v;
q.pop();
for(int to : g[v]) {
if(vis[to] != timer) {
q.push(to);
p[to] = v;
vis[to] = timer;
}
}
}
int x = lst;
timer++;
vis[d] = timer;
while(x != d) {
vis[x] = timer;
x = p[x];
}
return lst;
}
int dist[N], col[N];
void test() {
cin >> n >> m;
for(int i = 1; i <= n - 1; i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
for(int i = 1; i <= n; i++)
cin >> a[i];
int d = f(1), d1 = f(d);
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
dist[j] = col[j] = 0;
}
dist[i] = 1;
queue<int> q;
q.push(i);
set<int> st;
while(!q.empty()) {
int v = q.front();
q.pop();
col[dist[v]]++;
for(int to : g[v]) {
if(dist[to] == 0) {
dist[to] = dist[v] + 1;
q.push(to);
}
}
}
for(int j = 1; j <= n; j++) if(j != i) {
if(col[dist[j]] == 1) {
st.insert(a[j]);
}
}
cout << (int)st.size() << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t = 1;
// cin >> t;
while(t--)
test();
}
Compilation message (stderr)
joi2019_ho_t5.cpp: In function 'void test()':
joi2019_ho_t5.cpp:49:19: warning: unused variable 'd1' [-Wunused-variable]
49 | int d = f(1), d1 = f(d);
| ^~
# | 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... |