# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
133232 | ekrem | Mergers (JOI19_mergers) | C++98 | 214 ms | 111628 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define sol (k+k)
#define sag (k+k+1)
#define orta ((bas+son)/2)
#define coc g[node][i]
#define LOGN 20
#define mod 1000000007
#define inf 1000000009
#define N 1000005
using namespace std;
typedef long long ll;
typedef pair < int , int > ii;
int n, k, ans, l[N], a[N], der[N], par[LOGN + 5][N];
vector < int > g[N], cik[N];
set < int > s[N];
set < int > :: iterator it;
void hazirla(int node, int pr, int dr){
der[node] = dr;
par[0][node] = pr;
for(int i = 0; i < g[node].size(); i++)
if(coc != pr)
hazirla(coc, node, dr + 1);
}
int lca(int u, int v){
if(!u or !v)
return u + v;
if(der[u] < der[v])
swap(u, v);
for(int i = LOGN; i >= 0; i--)
if(der[par[i][u]] >= der[v])
u = par[i][u];
if(u == v)
return u;
for(int i = LOGN; i >= 0; i--)
if(par[i][u] != par[i][v]){
u = par[i][u];
v = par[i][v];
}
return par[0][u];
}
bool dfs(int node, int par){
bool don = 0;
for(int i = 0; i < g[node].size(); i++)
if(coc != par){
don |= dfs(coc, node);
if(s[coc].size() > s[node].size())
swap(s[node], s[coc]);
for(it = s[coc].begin(); it != s[coc].end(); it++)
s[node].insert(*it);
}
// cout << node << " " << don << endl;
s[node].insert(a[node]);
for(int i = 0; i < cik[node].size(); i++)
s[node].erase(cik[node][i]);
if(!don and s[node].empty() and par){
// cout << node << endl;
don = 1;
ans++;
}
return don;
}
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
scanf("%d %d",&n ,&k);
for(int i = 1; i < n; i++){
int u, v;
scanf("%d %d",&u ,&v);
g[v].pb(u);
g[u].pb(v);
}
hazirla(1, 0, 1);
for(int i = 1; i <= LOGN; i++)
for(int j = 1; j <= n; j++)
par[i][j] = par[i - 1][par[i - 1][j]];
for(int i = 1; i <= n; i++){
scanf("%d",a + i);
l[a[i]] = lca(l[a[i]], i);
}
for(int i = 1; i <= k; i++)
cik[l[i]].pb(i);
dfs(1, 0);
printf("%d",ans/2 + ans%2);
// cout << ans << endl;
return 0;
}
컴파일 시 표준 에러 (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... |