This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mem(a,v) memset((a), (v), sizeof (a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%lld", &(n))
#define nai(a, n) for (int i = 0; i < (n); i++) ni(a[i])
#define nal(a, n) for (int i = 0; i < (n); i++) nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%lld\n", (n))
#define pii pair<int, int>
#define pil pair<int, long long>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vil vector<pil>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const ll INF = 1e16 + 7;
const int MAXN = 1e6 + 5;
const double eps = 1e-9;
int deg[MAXN], col[MAXN], lo[MAXN], disc[MAXN];
int ind[MAXN];
pii edg[MAXN];
vii adj[MAXN];
int t = 0, vis[MAXN], cnt = 0, br[MAXN];
void dfs(int u, int p) {
disc[u] = lo[u] = t++;
vis[u] = 1;
for (pii nx: adj[u]) {
int v, id; tie(v, id) = nx;
if (!vis[v]) {
dfs(v, id);
lo[u] = min(lo[u], lo[v]);
if (disc[u] < lo[v])
br[id] = 1;
} else if (id != p)
lo[u] = min(lo[u], disc[v]);
}
}
int curC = 0;
void bfs(int cur) {
deque<int> pq;
pq.pb(cur);
vis[cur] = 1;
while (!pq.empty()) {
int u = pq.front(); pq.pop_front();
ind[u] = curC;
for (pii nx: adj[u]) {
int v, id; tie(v, id) = nx;
if (!br[id] && !vis[v]) {
pq.pb(v);
vis[v] = 1;
}
}
}
curC++;
}
int main() {
int n, k;
scanf("%d %d", &n, &k);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d %d", &u, &v);
edg[i] = {--u, --v};
}
for (int i = 0; i < n; i++) {
ni(col[i]); --col[i];
}
for (int i = 1; i < n; i++) {
int u, v; tie(u, v) = edg[i];
u = col[u], v = col[v];
if (u == v) continue;
adj[u].pb({v, cnt});
adj[v].pb({u, cnt});
cnt++;
}
dfs(0, -1);
mem(vis,0);
for (int i = 0; i < k; i++)
if (!vis[i])
bfs(i);
for (int i = 0; i < k; i++) {
for (pii nx: adj[i]) {
int u, id; tie(u, id) = nx;
if (!br[id]) continue;
deg[ind[u]]++;
}
}
int lvs = 0;
for (int i = 0; i < curC; i++)
if (deg[i] == 1)
lvs++;
pri((lvs+1)/2);
return 0;
}
Compilation message (stderr)
mergers.cpp: In function 'int main()':
mergers.cpp:79:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &k);
~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:82:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &u, &v);
~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:8:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
#define ni(n) scanf("%d", &(n))
~~~~~^~~~~~~~~~~~
mergers.cpp:86:3: note: in expansion of macro 'ni'
ni(col[i]); --col[i];
^~
# | 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... |