답안 #1017673

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1017673 2024-07-09T09:26:10 Z ByeWorld Mergers (JOI19_mergers) C++14
100 / 100
1476 ms 194612 KB
#include <bits/stdc++.h>
// #pragma GCC optimize("O3", "unroll-loops")
#define ll long long
#define int long long
#define pb push_back
#define fi first
#define se second
#define lf (id<<1)
#define rg ((id<<1)|1)
#define md ((l+r)>>1)
#define ld long double
using namespace std;
typedef pair<int,int> pii;
typedef pair<pii, int> ipii;
const int MAXN = 5e5+10;
const int MAXA = 9e3+20;
const ll INF = 1e9+10;
const int LOG = 13;
const int SQRT = 450;
const vector<int> NOL = {};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const vector<int> dx = {1, -1, 0, 0};
const vector<int> dy = {0, 0, 1, -1};
vector <int> key = {29, 31};
vector <int> mod = {998244353, 1000000007};
void chmx(int &a, int b){ a = max(a, b); }

int n, k;
vector <int> adj[MAXN], vec[MAXN];
int a[MAXN], tot[MAXN], u[MAXN], v[MAXN];
vector <int> tree[MAXN];

struct dsu {
	int par[MAXN], siz[MAXN];
	void bd(){
		for(int i=1; i<=n; i++){
			par[i] = i; siz[i] = 1;
		}
	}
	int f(int x){
		if(par[x]==x) return x;
		return par[x] = f(par[x]);
	}
	bool con(int x, int y){ return f(x) == f(y); }
	void mer(int x, int y){
		x = f(x); y = f(y);
		if(x==y) return;
		if(siz[x] > siz[y]) swap(x, y);
		par[x] = y; siz[y] += siz[x];
	}
} DSU;

int sub[MAXN], dep[MAXN], in[MAXN], tim, par[MAXN];
void dfs(int nw, int pa){
	dep[nw] = dep[pa]+1; par[nw] = pa; 
	sub[nw] = 1;
	for(auto nx : adj[nw]){
		if(nx==pa) continue;
		tree[nw].pb(nx);
		dfs(nx, nw);
		sub[nw] += sub[nx];
	}
}

int ANS[MAXN];
struct segtree {
	int st[4*MAXN], laz[4*MAXN];
	void bnc(int id, int l, int r){
		if(laz[id] == 0) return;
		st[lf] += (md-l+1); laz[lf] += laz[id];
		st[rg] += (r-md); laz[rg] += laz[id];
		laz[id] = 0;
	}
	int upd(int id, int l, int r, int x, int y){
		if(r<x || y<l) return st[id];
		if(x<=l && r<=y){ 
			laz[id]++; return st[id] += (r-l+1);
		}
		bnc(id, l, r);
		return st[id] = upd(lf, l, md, x, y) + upd(rg, md+1, r, x, y);
	}
	void out(int id, int l, int r){
		if(l==r){ ANS[l] = st[id]; return; }
		bnc(id, l, r);
		out(lf, l, md); out(rg, md+1, r);
	}
} ST;

int root[MAXN], arr[MAXN];
void bd(int nw, int roo){
	root[nw] = roo; in[nw] = ++tim; arr[tim] = nw;
	if(tree[nw].size() == 0) return;
	bd(tree[nw][0], roo);
	for(int j=1; j<tree[nw].size(); j++){
		bd(tree[nw][j], tree[nw][j]);
	}
}

void query(int x, int y){
	// cout << x << ' ' << y << " xyquery\n";
	// cout << root[x] << ' ' << root[y] <<" root xy\n";
	while(root[x] != root[y]){
		if(dep[root[x]] > dep[root[y]]) swap(x, y); // y bawah
		// root[y]+1 - y
		// root->nyimpen siapa root, in[root]+1 --> idxnya
		if(in[root[y]] <= in[y]){
			// cout << in[root[y]] << ' ' << in[y] << " in\n";
			ST.upd(1, 1, n, in[root[y]], in[y]);
		}
		y = par[root[y]];
	}
	if(dep[x] > dep[y]) swap(x, y); // y bawah
	if(in[x]+1 <= in[y]){
		// cout << in[x]+1 << ' ' << in[y] << " in\n";
		ST.upd(1, 1, n, in[x]+1, in[y]);
	}
	// cout << '\n';
}
vector <int> edge[MAXN];

bool cmp(int a, int b){
	return in[a] < in[b];
}
signed main(){
	// ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin >> n >> k; DSU.bd();
	for(int i=1; i<=n-1; i++){
		int x, y; cin >> x >> y; u[i] = x; v[i] = y;
		adj[x].pb(y); adj[y].pb(x);
	}
	for(int i=1; i<=n; i++){
		cin >> a[i]; tot[a[i]]++;
		vec[a[i]].pb(i);
	}
	// dfs hld
	dfs(1, 0);

	// build hld
	for(int i=1; i<=n; i++){
		if(tree[i].size() == 0) continue;
		for(int j=1; j<tree[i].size(); j++){
			if(sub[tree[i][0]] < sub[tree[i][j]])
				swap(tree[i][0], tree[i][j]);
		}
	}
	tim = 0; bd(1, 1);

	// per state 
	for(int i=1; i<=k; i++){
		if(vec[i].size() <= 1) continue;
		// cout << i << " done\n";
		sort(vec[i].begin(), vec[i].end(), cmp);
		for(int j=1; j<vec[i].size(); j++) query(vec[i][j-1], vec[i][j]);
		query(vec[i][0], vec[i].back());
	}
	// cout << " pp\n";
 	
 	// OUT segtree
 	ST.out(1, 1, n);
 	for(int i=1; i<=n; i++)
 		if(ANS[i]) DSU.mer(par[arr[i]], arr[i]);
 	// for(int i=1; i<=n; i++) cout << i << ' ' << ANS[i] << ' '<< arr[i] << " pp\n";
 	
 	// ANSwer
	for(int i=1; i<=n-1; i++){
		int x = DSU.f(u[i]), y = DSU.f(v[i]);
		if(x == y) continue;
		edge[x].pb(y); edge[y].pb(x);
	}
	int tot = 0;
	for(int i=1; i<=n; i++){
		if(edge[i].size() == 1) tot++;
	}
	cout << (tot+1)/2 << '\n';
}

Compilation message

mergers.cpp: In function 'void bd(long long int, long long int)':
mergers.cpp:94:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   94 |  for(int j=1; j<tree[nw].size(); j++){
      |               ~^~~~~~~~~~~~~~~~
mergers.cpp: In function 'int main()':
mergers.cpp:141:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  141 |   for(int j=1; j<tree[i].size(); j++){
      |                ~^~~~~~~~~~~~~~~
mergers.cpp:153:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  153 |   for(int j=1; j<vec[i].size(); j++) query(vec[i][j-1], vec[i][j]);
      |                ~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 69980 KB Output is correct
2 Correct 11 ms 69976 KB Output is correct
3 Correct 11 ms 69980 KB Output is correct
4 Correct 12 ms 70036 KB Output is correct
5 Correct 10 ms 69980 KB Output is correct
6 Correct 11 ms 68092 KB Output is correct
7 Correct 10 ms 67996 KB Output is correct
8 Correct 10 ms 67932 KB Output is correct
9 Correct 11 ms 67932 KB Output is correct
10 Correct 10 ms 69980 KB Output is correct
11 Correct 12 ms 68092 KB Output is correct
12 Correct 11 ms 70128 KB Output is correct
13 Correct 13 ms 67932 KB Output is correct
14 Correct 10 ms 70056 KB Output is correct
15 Correct 11 ms 69976 KB Output is correct
16 Correct 11 ms 69980 KB Output is correct
17 Correct 11 ms 67932 KB Output is correct
18 Correct 11 ms 69976 KB Output is correct
19 Correct 11 ms 67932 KB Output is correct
20 Correct 11 ms 69980 KB Output is correct
21 Correct 10 ms 68048 KB Output is correct
22 Correct 10 ms 69980 KB Output is correct
23 Correct 10 ms 69976 KB Output is correct
24 Correct 10 ms 69980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 69980 KB Output is correct
2 Correct 11 ms 69976 KB Output is correct
3 Correct 11 ms 69980 KB Output is correct
4 Correct 12 ms 70036 KB Output is correct
5 Correct 10 ms 69980 KB Output is correct
6 Correct 11 ms 68092 KB Output is correct
7 Correct 10 ms 67996 KB Output is correct
8 Correct 10 ms 67932 KB Output is correct
9 Correct 11 ms 67932 KB Output is correct
10 Correct 10 ms 69980 KB Output is correct
11 Correct 12 ms 68092 KB Output is correct
12 Correct 11 ms 70128 KB Output is correct
13 Correct 13 ms 67932 KB Output is correct
14 Correct 10 ms 70056 KB Output is correct
15 Correct 11 ms 69976 KB Output is correct
16 Correct 11 ms 69980 KB Output is correct
17 Correct 11 ms 67932 KB Output is correct
18 Correct 11 ms 69976 KB Output is correct
19 Correct 11 ms 67932 KB Output is correct
20 Correct 11 ms 69980 KB Output is correct
21 Correct 10 ms 68048 KB Output is correct
22 Correct 10 ms 69980 KB Output is correct
23 Correct 10 ms 69976 KB Output is correct
24 Correct 10 ms 69980 KB Output is correct
25 Correct 11 ms 67996 KB Output is correct
26 Correct 13 ms 70544 KB Output is correct
27 Correct 13 ms 70492 KB Output is correct
28 Correct 13 ms 68444 KB Output is correct
29 Correct 14 ms 70492 KB Output is correct
30 Correct 14 ms 68284 KB Output is correct
31 Correct 10 ms 67932 KB Output is correct
32 Correct 13 ms 70572 KB Output is correct
33 Correct 10 ms 70072 KB Output is correct
34 Correct 15 ms 70232 KB Output is correct
35 Correct 18 ms 70572 KB Output is correct
36 Correct 13 ms 70332 KB Output is correct
37 Correct 14 ms 70492 KB Output is correct
38 Correct 10 ms 69980 KB Output is correct
39 Correct 14 ms 70512 KB Output is correct
40 Correct 13 ms 70236 KB Output is correct
41 Correct 13 ms 70420 KB Output is correct
42 Correct 14 ms 70344 KB Output is correct
43 Correct 13 ms 70716 KB Output is correct
44 Correct 11 ms 69976 KB Output is correct
45 Correct 13 ms 70488 KB Output is correct
46 Correct 15 ms 70492 KB Output is correct
47 Correct 11 ms 69980 KB Output is correct
48 Correct 15 ms 70324 KB Output is correct
49 Correct 16 ms 70452 KB Output is correct
50 Correct 13 ms 70488 KB Output is correct
51 Correct 13 ms 70492 KB Output is correct
52 Correct 12 ms 68456 KB Output is correct
53 Correct 13 ms 70232 KB Output is correct
54 Correct 13 ms 68444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 69980 KB Output is correct
2 Correct 11 ms 69976 KB Output is correct
3 Correct 11 ms 69980 KB Output is correct
4 Correct 12 ms 70036 KB Output is correct
5 Correct 10 ms 69980 KB Output is correct
6 Correct 11 ms 68092 KB Output is correct
7 Correct 10 ms 67996 KB Output is correct
8 Correct 10 ms 67932 KB Output is correct
9 Correct 11 ms 67932 KB Output is correct
10 Correct 10 ms 69980 KB Output is correct
11 Correct 12 ms 68092 KB Output is correct
12 Correct 11 ms 70128 KB Output is correct
13 Correct 13 ms 67932 KB Output is correct
14 Correct 10 ms 70056 KB Output is correct
15 Correct 11 ms 69976 KB Output is correct
16 Correct 11 ms 69980 KB Output is correct
17 Correct 11 ms 67932 KB Output is correct
18 Correct 11 ms 69976 KB Output is correct
19 Correct 11 ms 67932 KB Output is correct
20 Correct 11 ms 69980 KB Output is correct
21 Correct 10 ms 68048 KB Output is correct
22 Correct 10 ms 69980 KB Output is correct
23 Correct 10 ms 69976 KB Output is correct
24 Correct 10 ms 69980 KB Output is correct
25 Correct 10 ms 67928 KB Output is correct
26 Correct 100 ms 80772 KB Output is correct
27 Correct 159 ms 83796 KB Output is correct
28 Correct 13 ms 68440 KB Output is correct
29 Correct 10 ms 69980 KB Output is correct
30 Correct 10 ms 68048 KB Output is correct
31 Correct 111 ms 83372 KB Output is correct
32 Correct 13 ms 68444 KB Output is correct
33 Correct 117 ms 89764 KB Output is correct
34 Correct 141 ms 85076 KB Output is correct
35 Correct 13 ms 68444 KB Output is correct
36 Correct 110 ms 84640 KB Output is correct
37 Correct 14 ms 70488 KB Output is correct
38 Correct 14 ms 70492 KB Output is correct
39 Correct 100 ms 82096 KB Output is correct
40 Correct 13 ms 68700 KB Output is correct
41 Correct 117 ms 84964 KB Output is correct
42 Correct 109 ms 86100 KB Output is correct
43 Correct 11 ms 69976 KB Output is correct
44 Correct 103 ms 91340 KB Output is correct
45 Correct 114 ms 86608 KB Output is correct
46 Correct 13 ms 68440 KB Output is correct
47 Correct 14 ms 68456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 95 ms 82748 KB Output is correct
2 Correct 115 ms 83012 KB Output is correct
3 Correct 15 ms 70488 KB Output is correct
4 Correct 13 ms 70492 KB Output is correct
5 Correct 12 ms 69980 KB Output is correct
6 Correct 10 ms 67932 KB Output is correct
7 Correct 13 ms 70236 KB Output is correct
8 Correct 136 ms 82956 KB Output is correct
9 Correct 13 ms 70236 KB Output is correct
10 Correct 110 ms 81736 KB Output is correct
11 Correct 10 ms 69980 KB Output is correct
12 Correct 100 ms 82652 KB Output is correct
13 Correct 123 ms 83224 KB Output is correct
14 Correct 105 ms 83540 KB Output is correct
15 Correct 119 ms 82660 KB Output is correct
16 Correct 13 ms 70492 KB Output is correct
17 Correct 10 ms 68028 KB Output is correct
18 Correct 101 ms 87520 KB Output is correct
19 Correct 120 ms 89848 KB Output is correct
20 Correct 13 ms 68444 KB Output is correct
21 Correct 11 ms 69980 KB Output is correct
22 Correct 131 ms 84204 KB Output is correct
23 Correct 13 ms 70232 KB Output is correct
24 Correct 107 ms 83028 KB Output is correct
25 Correct 124 ms 86996 KB Output is correct
26 Correct 13 ms 70488 KB Output is correct
27 Correct 16 ms 70492 KB Output is correct
28 Correct 13 ms 68444 KB Output is correct
29 Correct 13 ms 68444 KB Output is correct
30 Correct 14 ms 70288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 69980 KB Output is correct
2 Correct 11 ms 69976 KB Output is correct
3 Correct 11 ms 69980 KB Output is correct
4 Correct 12 ms 70036 KB Output is correct
5 Correct 10 ms 69980 KB Output is correct
6 Correct 11 ms 68092 KB Output is correct
7 Correct 10 ms 67996 KB Output is correct
8 Correct 10 ms 67932 KB Output is correct
9 Correct 11 ms 67932 KB Output is correct
10 Correct 10 ms 69980 KB Output is correct
11 Correct 12 ms 68092 KB Output is correct
12 Correct 11 ms 70128 KB Output is correct
13 Correct 13 ms 67932 KB Output is correct
14 Correct 10 ms 70056 KB Output is correct
15 Correct 11 ms 69976 KB Output is correct
16 Correct 11 ms 69980 KB Output is correct
17 Correct 11 ms 67932 KB Output is correct
18 Correct 11 ms 69976 KB Output is correct
19 Correct 11 ms 67932 KB Output is correct
20 Correct 11 ms 69980 KB Output is correct
21 Correct 10 ms 68048 KB Output is correct
22 Correct 10 ms 69980 KB Output is correct
23 Correct 10 ms 69976 KB Output is correct
24 Correct 10 ms 69980 KB Output is correct
25 Correct 11 ms 67996 KB Output is correct
26 Correct 13 ms 70544 KB Output is correct
27 Correct 13 ms 70492 KB Output is correct
28 Correct 13 ms 68444 KB Output is correct
29 Correct 14 ms 70492 KB Output is correct
30 Correct 14 ms 68284 KB Output is correct
31 Correct 10 ms 67932 KB Output is correct
32 Correct 13 ms 70572 KB Output is correct
33 Correct 10 ms 70072 KB Output is correct
34 Correct 15 ms 70232 KB Output is correct
35 Correct 18 ms 70572 KB Output is correct
36 Correct 13 ms 70332 KB Output is correct
37 Correct 14 ms 70492 KB Output is correct
38 Correct 10 ms 69980 KB Output is correct
39 Correct 14 ms 70512 KB Output is correct
40 Correct 13 ms 70236 KB Output is correct
41 Correct 13 ms 70420 KB Output is correct
42 Correct 14 ms 70344 KB Output is correct
43 Correct 13 ms 70716 KB Output is correct
44 Correct 11 ms 69976 KB Output is correct
45 Correct 13 ms 70488 KB Output is correct
46 Correct 15 ms 70492 KB Output is correct
47 Correct 11 ms 69980 KB Output is correct
48 Correct 15 ms 70324 KB Output is correct
49 Correct 16 ms 70452 KB Output is correct
50 Correct 13 ms 70488 KB Output is correct
51 Correct 13 ms 70492 KB Output is correct
52 Correct 12 ms 68456 KB Output is correct
53 Correct 13 ms 70232 KB Output is correct
54 Correct 13 ms 68444 KB Output is correct
55 Correct 10 ms 67928 KB Output is correct
56 Correct 100 ms 80772 KB Output is correct
57 Correct 159 ms 83796 KB Output is correct
58 Correct 13 ms 68440 KB Output is correct
59 Correct 10 ms 69980 KB Output is correct
60 Correct 10 ms 68048 KB Output is correct
61 Correct 111 ms 83372 KB Output is correct
62 Correct 13 ms 68444 KB Output is correct
63 Correct 117 ms 89764 KB Output is correct
64 Correct 141 ms 85076 KB Output is correct
65 Correct 13 ms 68444 KB Output is correct
66 Correct 110 ms 84640 KB Output is correct
67 Correct 14 ms 70488 KB Output is correct
68 Correct 14 ms 70492 KB Output is correct
69 Correct 100 ms 82096 KB Output is correct
70 Correct 13 ms 68700 KB Output is correct
71 Correct 117 ms 84964 KB Output is correct
72 Correct 109 ms 86100 KB Output is correct
73 Correct 11 ms 69976 KB Output is correct
74 Correct 103 ms 91340 KB Output is correct
75 Correct 114 ms 86608 KB Output is correct
76 Correct 13 ms 68440 KB Output is correct
77 Correct 14 ms 68456 KB Output is correct
78 Correct 95 ms 82748 KB Output is correct
79 Correct 115 ms 83012 KB Output is correct
80 Correct 15 ms 70488 KB Output is correct
81 Correct 13 ms 70492 KB Output is correct
82 Correct 12 ms 69980 KB Output is correct
83 Correct 10 ms 67932 KB Output is correct
84 Correct 13 ms 70236 KB Output is correct
85 Correct 136 ms 82956 KB Output is correct
86 Correct 13 ms 70236 KB Output is correct
87 Correct 110 ms 81736 KB Output is correct
88 Correct 10 ms 69980 KB Output is correct
89 Correct 100 ms 82652 KB Output is correct
90 Correct 123 ms 83224 KB Output is correct
91 Correct 105 ms 83540 KB Output is correct
92 Correct 119 ms 82660 KB Output is correct
93 Correct 13 ms 70492 KB Output is correct
94 Correct 10 ms 68028 KB Output is correct
95 Correct 101 ms 87520 KB Output is correct
96 Correct 120 ms 89848 KB Output is correct
97 Correct 13 ms 68444 KB Output is correct
98 Correct 11 ms 69980 KB Output is correct
99 Correct 131 ms 84204 KB Output is correct
100 Correct 13 ms 70232 KB Output is correct
101 Correct 107 ms 83028 KB Output is correct
102 Correct 124 ms 86996 KB Output is correct
103 Correct 13 ms 70488 KB Output is correct
104 Correct 16 ms 70492 KB Output is correct
105 Correct 13 ms 68444 KB Output is correct
106 Correct 13 ms 68444 KB Output is correct
107 Correct 14 ms 70288 KB Output is correct
108 Correct 735 ms 154584 KB Output is correct
109 Correct 650 ms 173920 KB Output is correct
110 Correct 663 ms 185236 KB Output is correct
111 Correct 694 ms 194612 KB Output is correct
112 Correct 736 ms 183712 KB Output is correct
113 Correct 556 ms 175908 KB Output is correct
114 Correct 670 ms 151120 KB Output is correct
115 Correct 794 ms 151140 KB Output is correct
116 Correct 1376 ms 156588 KB Output is correct
117 Correct 828 ms 170528 KB Output is correct
118 Correct 780 ms 151892 KB Output is correct
119 Correct 860 ms 170932 KB Output is correct
120 Correct 916 ms 187392 KB Output is correct
121 Correct 915 ms 177172 KB Output is correct
122 Correct 1476 ms 165164 KB Output is correct
123 Correct 518 ms 167144 KB Output is correct
124 Correct 745 ms 179664 KB Output is correct