답안 #484163

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
484163 2021-11-02T08:51:29 Z mars4 Mergers (JOI19_mergers) C++17
0 / 100
48 ms 16104 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp> 

using namespace std;
using namespace __gnu_pbds; 

#define ff              first
#define ss              second
#define ll              int64_t
#define ld              long double
#define nl              cout<<"\n"
#define all(v)          v.begin(),v.end()
#define mset(a,v)       memset((a),(v),sizeof(a))
#define forn(i,a,b)     for(int64_t i=int64_t(a);i<int64_t(b);++i)
#define forb(i,a,b)     for(int64_t i=int64_t(a);i>=int64_t(b);--i)
#define fastio()        ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

#define mod         1'000'000'007
#define mod2        998'244'353 
#define inf         1'000'000'000'000'007
#define pi          3.14159265358979323846

template<class key,class cmp=std::less<key>>
using ordered_set=tree<key,null_type,cmp,rb_tree_tag,tree_order_statistics_node_update>;

template<class L,class R> ostream& operator<<(ostream& out,pair<L,R> &p)        {return out<<"("<<p.ff<<", "<<p.ss<<")";}
template<class T> ostream& operator<<(ostream& out,vector<T> &v)                {out<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())out<<", ";out<<*it;}return out<<"]";}
template<class T> ostream& operator<<(ostream& out,deque<T> &v)                 {out<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())out<<", ";out<<*it;}return out<<"]";}
template<class T> ostream& operator<<(ostream& out,set<T> &s)                   {out<<"{";for(auto it=s.begin();it!=s.end();++it){if(it!=s.begin())out<<", ";out<<*it;}return out<<"}";}
template<class L,class R> ostream& operator<<(ostream& out,map<L,R> &m)         {out<<"{";for(auto it=m.begin();it!=m.end();++it){if(it!=m.begin())out<<", ";out<<*it;}return out<<"}";}

void dbg_out() {cerr<<"]\n";}
template<typename Head,typename... Tail> 
void dbg_out(Head H,Tail... T) {cerr<<H;if(sizeof...(Tail))cerr<<", ";dbg_out(T...);}
#ifdef LOCAL
	#define dbg(...) cerr<<"["<<#__VA_ARGS__<<"] = [",dbg_out(__VA_ARGS__)
#else
	#define dbg(...)
#endif

//---------------------------------mars4---------------------------------

vector<ll> parent;
vector<ll> depth;
vector<ll> tp;
vector<vector<ll>> v;
ll leaves=0;

ll find(ll i)
{
	return i==parent[i]?i:parent[i]=find(parent[i]);
}

void merge(ll child,ll par)
{
	dbg(child, par);
	ll nc=(ll)v[child].size();
	ll np=(ll)v[par].size();
	if(nc<np)
	{
		v[par].insert(v[par].end(),all(v[child]));
	}
	else
	{
		v[child].insert(v[child].end(),all(v[par]));
		swap(v[par],v[child]);
	}
	parent[child]=par;
}

void dfs(ll cur,ll prev)
{
	tp[cur]=prev;
	for(ll i:v[cur])
	{
		if(i!=prev)
		{
			depth[i]=depth[cur]+1;
			dfs(i,cur);
		}
	}
}

void calc(ll cur,ll prev)
{
	if(cur and (ll)v[cur].size()==1)
	{
		leaves++;
	}
	for(ll i:v[cur])
	{
		ll u=find(i);
		if(u!=cur and u!=prev)
		{
			calc(i,cur);
		}
	}
}

int main()
{
	fastio();
	ll z,n,m,t,k,i,j,l,d,h,r;
	cin>>n>>k;
	v=vector<vector<ll>>(n);
	depth=vector<ll>(n);
	tp=vector<ll>(n);
	parent=vector<ll>(n);
	iota(all(parent),0ll);
	vector<vector<ll>> ind(k);
	forn(i,0,n-1)
	{
		cin>>l>>r;
		l--,r--;
		v[l].push_back(r);
		v[r].push_back(l);
	}
	forn(i,0,n)
	{
		cin>>d;
		ind[d-1].push_back(i);
	}
	dfs(0,0);
	forn(i,0,k)
	{
		if((ll)ind[i].size()<2)
			continue;
		ll cur=find(ind[i].front());
		for(ll j:ind[i])
		{
			j=find(j);
			while(cur!=j)
			{
				if(depth[cur]<depth[j])
					swap(cur,j);
				ll nxt=find(tp[cur]);
				merge(cur,nxt);
				cur=nxt;
			}
		}
	}
	calc(0,0);
	ll ans=(leaves+1)/2;
	cout<<ans<<"\n";
	cerr<<"\nTime elapsed: "<<1000*clock()/CLOCKS_PER_SEC<<"ms\n";
	return 0;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:104:5: warning: unused variable 'z' [-Wunused-variable]
  104 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |     ^
mergers.cpp:104:9: warning: unused variable 'm' [-Wunused-variable]
  104 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |         ^
mergers.cpp:104:11: warning: unused variable 't' [-Wunused-variable]
  104 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |           ^
mergers.cpp:104:15: warning: unused variable 'i' [-Wunused-variable]
  104 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |               ^
mergers.cpp:104:17: warning: unused variable 'j' [-Wunused-variable]
  104 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                 ^
mergers.cpp:104:23: warning: unused variable 'h' [-Wunused-variable]
  104 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                       ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Incorrect 0 ms 332 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Incorrect 0 ms 332 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Incorrect 0 ms 332 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 12676 KB Output is correct
2 Incorrect 46 ms 16104 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Incorrect 0 ms 332 KB Output isn't correct
3 Halted 0 ms 0 KB -