답안 #484184

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
484184 2021-11-02T10:22:41 Z mars4 Mergers (JOI19_mergers) C++17
100 / 100
759 ms 106504 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)
{
	ll nc=(ll)v[child].size();
	ll np=(ll)v[par].size();
	if(nc<np)
	{
		v[par].insert(v[par].end(),all(v[child]));
		v[child].clear();
	}
	else
	{
		v[child].insert(v[child].end(),all(v[par]));
		v[par].clear();
		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)
{
	ll cnt=0;
	for(ll i:v[cur])
	{
		ll u=find(i);
		if(u!=find(cur) and u!=find(prev))
		{
			cnt++;
			calc(i,cur);
		}
	}
	if((cur and !cnt) or (!cur and cnt==1))
		leaves++;
}

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:105:5: warning: unused variable 'z' [-Wunused-variable]
  105 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |     ^
mergers.cpp:105:9: warning: unused variable 'm' [-Wunused-variable]
  105 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |         ^
mergers.cpp:105:11: warning: unused variable 't' [-Wunused-variable]
  105 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |           ^
mergers.cpp:105:15: warning: unused variable 'i' [-Wunused-variable]
  105 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |               ^
mergers.cpp:105:17: warning: unused variable 'j' [-Wunused-variable]
  105 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                 ^
mergers.cpp:105:23: warning: unused variable 'h' [-Wunused-variable]
  105 |  ll z,n,m,t,k,i,j,l,d,h,r;
      |                       ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 0 ms 204 KB Output is correct
24 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 0 ms 204 KB Output is correct
24 Correct 0 ms 204 KB Output is correct
25 Correct 0 ms 204 KB Output is correct
26 Correct 2 ms 716 KB Output is correct
27 Correct 2 ms 708 KB Output is correct
28 Correct 2 ms 844 KB Output is correct
29 Correct 2 ms 716 KB Output is correct
30 Correct 2 ms 744 KB Output is correct
31 Correct 0 ms 204 KB Output is correct
32 Correct 2 ms 964 KB Output is correct
33 Correct 0 ms 204 KB Output is correct
34 Correct 1 ms 716 KB Output is correct
35 Correct 3 ms 708 KB Output is correct
36 Correct 2 ms 716 KB Output is correct
37 Correct 2 ms 716 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 2 ms 716 KB Output is correct
40 Correct 2 ms 716 KB Output is correct
41 Correct 2 ms 716 KB Output is correct
42 Correct 2 ms 716 KB Output is correct
43 Correct 2 ms 844 KB Output is correct
44 Correct 1 ms 332 KB Output is correct
45 Correct 2 ms 840 KB Output is correct
46 Correct 2 ms 716 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 2 ms 716 KB Output is correct
49 Correct 2 ms 716 KB Output is correct
50 Correct 2 ms 864 KB Output is correct
51 Correct 2 ms 712 KB Output is correct
52 Correct 1 ms 716 KB Output is correct
53 Correct 2 ms 716 KB Output is correct
54 Correct 2 ms 716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 0 ms 204 KB Output is correct
24 Correct 0 ms 204 KB Output is correct
25 Correct 0 ms 204 KB Output is correct
26 Correct 48 ms 12828 KB Output is correct
27 Correct 70 ms 14152 KB Output is correct
28 Correct 2 ms 716 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 0 ms 204 KB Output is correct
31 Correct 67 ms 13816 KB Output is correct
32 Correct 2 ms 716 KB Output is correct
33 Correct 69 ms 17404 KB Output is correct
34 Correct 65 ms 13764 KB Output is correct
35 Correct 2 ms 716 KB Output is correct
36 Correct 70 ms 13636 KB Output is correct
37 Correct 2 ms 716 KB Output is correct
38 Correct 2 ms 716 KB Output is correct
39 Correct 47 ms 12544 KB Output is correct
40 Correct 2 ms 844 KB Output is correct
41 Correct 60 ms 13732 KB Output is correct
42 Correct 67 ms 15228 KB Output is correct
43 Correct 0 ms 332 KB Output is correct
44 Correct 67 ms 20156 KB Output is correct
45 Correct 65 ms 16280 KB Output is correct
46 Correct 2 ms 656 KB Output is correct
47 Correct 2 ms 700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 11192 KB Output is correct
2 Correct 53 ms 14380 KB Output is correct
3 Correct 3 ms 716 KB Output is correct
4 Correct 2 ms 716 KB Output is correct
5 Correct 1 ms 312 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 716 KB Output is correct
8 Correct 92 ms 16944 KB Output is correct
9 Correct 2 ms 716 KB Output is correct
10 Correct 69 ms 14424 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 68 ms 13904 KB Output is correct
13 Correct 90 ms 15808 KB Output is correct
14 Correct 66 ms 16044 KB Output is correct
15 Correct 45 ms 12460 KB Output is correct
16 Correct 2 ms 708 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 53 ms 16776 KB Output is correct
19 Correct 75 ms 20968 KB Output is correct
20 Correct 2 ms 712 KB Output is correct
21 Correct 0 ms 332 KB Output is correct
22 Correct 59 ms 14792 KB Output is correct
23 Correct 2 ms 716 KB Output is correct
24 Correct 76 ms 14504 KB Output is correct
25 Correct 76 ms 20036 KB Output is correct
26 Correct 2 ms 716 KB Output is correct
27 Correct 2 ms 844 KB Output is correct
28 Correct 2 ms 716 KB Output is correct
29 Correct 1 ms 716 KB Output is correct
30 Correct 2 ms 716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 0 ms 204 KB Output is correct
24 Correct 0 ms 204 KB Output is correct
25 Correct 0 ms 204 KB Output is correct
26 Correct 2 ms 716 KB Output is correct
27 Correct 2 ms 708 KB Output is correct
28 Correct 2 ms 844 KB Output is correct
29 Correct 2 ms 716 KB Output is correct
30 Correct 2 ms 744 KB Output is correct
31 Correct 0 ms 204 KB Output is correct
32 Correct 2 ms 964 KB Output is correct
33 Correct 0 ms 204 KB Output is correct
34 Correct 1 ms 716 KB Output is correct
35 Correct 3 ms 708 KB Output is correct
36 Correct 2 ms 716 KB Output is correct
37 Correct 2 ms 716 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 2 ms 716 KB Output is correct
40 Correct 2 ms 716 KB Output is correct
41 Correct 2 ms 716 KB Output is correct
42 Correct 2 ms 716 KB Output is correct
43 Correct 2 ms 844 KB Output is correct
44 Correct 1 ms 332 KB Output is correct
45 Correct 2 ms 840 KB Output is correct
46 Correct 2 ms 716 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 2 ms 716 KB Output is correct
49 Correct 2 ms 716 KB Output is correct
50 Correct 2 ms 864 KB Output is correct
51 Correct 2 ms 712 KB Output is correct
52 Correct 1 ms 716 KB Output is correct
53 Correct 2 ms 716 KB Output is correct
54 Correct 2 ms 716 KB Output is correct
55 Correct 0 ms 204 KB Output is correct
56 Correct 48 ms 12828 KB Output is correct
57 Correct 70 ms 14152 KB Output is correct
58 Correct 2 ms 716 KB Output is correct
59 Correct 1 ms 204 KB Output is correct
60 Correct 0 ms 204 KB Output is correct
61 Correct 67 ms 13816 KB Output is correct
62 Correct 2 ms 716 KB Output is correct
63 Correct 69 ms 17404 KB Output is correct
64 Correct 65 ms 13764 KB Output is correct
65 Correct 2 ms 716 KB Output is correct
66 Correct 70 ms 13636 KB Output is correct
67 Correct 2 ms 716 KB Output is correct
68 Correct 2 ms 716 KB Output is correct
69 Correct 47 ms 12544 KB Output is correct
70 Correct 2 ms 844 KB Output is correct
71 Correct 60 ms 13732 KB Output is correct
72 Correct 67 ms 15228 KB Output is correct
73 Correct 0 ms 332 KB Output is correct
74 Correct 67 ms 20156 KB Output is correct
75 Correct 65 ms 16280 KB Output is correct
76 Correct 2 ms 656 KB Output is correct
77 Correct 2 ms 700 KB Output is correct
78 Correct 44 ms 11192 KB Output is correct
79 Correct 53 ms 14380 KB Output is correct
80 Correct 3 ms 716 KB Output is correct
81 Correct 2 ms 716 KB Output is correct
82 Correct 1 ms 312 KB Output is correct
83 Correct 0 ms 204 KB Output is correct
84 Correct 1 ms 716 KB Output is correct
85 Correct 92 ms 16944 KB Output is correct
86 Correct 2 ms 716 KB Output is correct
87 Correct 69 ms 14424 KB Output is correct
88 Correct 1 ms 332 KB Output is correct
89 Correct 68 ms 13904 KB Output is correct
90 Correct 90 ms 15808 KB Output is correct
91 Correct 66 ms 16044 KB Output is correct
92 Correct 45 ms 12460 KB Output is correct
93 Correct 2 ms 708 KB Output is correct
94 Correct 1 ms 332 KB Output is correct
95 Correct 53 ms 16776 KB Output is correct
96 Correct 75 ms 20968 KB Output is correct
97 Correct 2 ms 712 KB Output is correct
98 Correct 0 ms 332 KB Output is correct
99 Correct 59 ms 14792 KB Output is correct
100 Correct 2 ms 716 KB Output is correct
101 Correct 76 ms 14504 KB Output is correct
102 Correct 76 ms 20036 KB Output is correct
103 Correct 2 ms 716 KB Output is correct
104 Correct 2 ms 844 KB Output is correct
105 Correct 2 ms 716 KB Output is correct
106 Correct 1 ms 716 KB Output is correct
107 Correct 2 ms 716 KB Output is correct
108 Correct 452 ms 70820 KB Output is correct
109 Correct 634 ms 76900 KB Output is correct
110 Correct 664 ms 91532 KB Output is correct
111 Correct 727 ms 106504 KB Output is correct
112 Correct 759 ms 104272 KB Output is correct
113 Correct 396 ms 83908 KB Output is correct
114 Correct 511 ms 70464 KB Output is correct
115 Correct 512 ms 67948 KB Output is correct
116 Correct 676 ms 77120 KB Output is correct
117 Correct 578 ms 79800 KB Output is correct
118 Correct 589 ms 70596 KB Output is correct
119 Correct 631 ms 79940 KB Output is correct
120 Correct 616 ms 101316 KB Output is correct
121 Correct 660 ms 79996 KB Output is correct
122 Correct 678 ms 86216 KB Output is correct
123 Correct 369 ms 80804 KB Output is correct
124 Correct 733 ms 79812 KB Output is correct