# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
236394 | Charis02 | Mergers (JOI19_mergers) | C++14 | 3078 ms | 22248 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<iostream>
#include<stdio.h>
#include<vector>
#include<cmath>
#include<queue>
#include<string.h>
#include<map>
#include<set>
#include<algorithm>
#define ll long long
#define pi pair < ll,ll >
#define mp(a,b) make_pair(a,b)
#define mid (low+high)/2
#define rep(i,a,b) for(int i = a;i < b;i++)
#define N 300004
#define INF 1e9+7
using namespace std;
struct dsu{
ll par[N];
ll sz[N];
ll col[N];
ll get(ll x)
{
return (par[x] == x) ? x : get(par[x]);
}
void join(ll x,ll y)
{
ll fx=get(x);
ll fy = get(y);
if(fx==fy)
return;
if(sz[fx]<sz[fy])
swap(fx,fy);
sz[fx]+=sz[fy];
par[fy]=fx;
}
ll get_col(ll x)
{
return col[get(x)];
}
};
ll n,k,state[N],answer;
ll ar[N];
vector < ll > graph[N];
ll cnt = 1;
dsu comp;
vector < ll > fin[N];
ll deg[N];
bool check(ll cur,ll par,ll col)
{
bool res = false;
if(state[cur] == col)
res = true;
rep(i,0,graph[cur].size())
{
ll v =graph[cur][i];
if(v==par)
continue;
if(check(v,cur,col))
{
comp.join(cur,v);
res=true;
}
}
return res;
}
void process_paths()
{
rep(i,1,n+1)
{
if(ar[state[i]] == 0)
ar[state[i]] = cnt++;
check(i,i,state[i]);
}
rep(i,1,n+1)
{
if(comp.get_col(i) == 0)
comp.col[comp.get(i)] = cnt++;
ar[i] = comp.get_col(i);
}
rep(i,1,n+1)
{
rep(j,0,graph[i].size())
{
ll v = graph[i][j];
if(ar[i] != ar[v])
{
deg[ar[i]]++;
fin[ar[i]].push_back(ar[v]);
}
}
}
ll leaves = 0;
rep(i,1,cnt)
{
if(deg[i] == 1)
leaves++;
}
answer = (leaves+1)/2;
return;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
rep(i,0,n-1)
{
ll a,b;
cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
rep(i,1,n+1)
{
comp.par[i]=i;
comp.sz[i]=1;
cin >> state[i];
}
process_paths();
cout << answer << 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... |