Submission #806874

# Submission time Handle Problem Language Result Execution time Memory
806874 2023-08-04T10:43:14 Z oscar1f Mergers (JOI19_mergers) C++17
100 / 100
1066 ms 117864 KB
#include<bits/stdc++.h>
using namespace std;

//#define int long long

const int MAX_SOM=500*1000+5,MAX_LOG=20;
int nbSom,nbGroupe,deb,fin,nbCompo,rep;
vector<int> adja[MAX_SOM];
int nbOccu[MAX_SOM],numGroupe[MAX_SOM],pere[MAX_SOM][MAX_LOG],numCompo[MAX_SOM];
int nbVois[MAX_SOM];
int lcaGroupe[MAX_SOM];
int prof[MAX_SOM];
int cumu[MAX_SOM];
vector<pair<int,int>> listeAre;

void marque(int pos,int idCompo) {
	numCompo[pos]=idCompo;
	for (int i:adja[pos]) {
		if (i!=pere[pos][0] and numCompo[i]==0) {
			marque(i,idCompo);
		}
	}
}

void DFS(int pos,int anci) {
	pere[pos][0]=anci;
	prof[pos]=prof[pere[pos][0]]+1;
	for (int i:adja[pos]) {
		if (i!=pere[pos][0]) {
			DFS(i,pos);
		}
	}
}

int calcLca(int som1,int som2) {
	if (prof[som1]>prof[som2]) {
		swap(som1,som2);
	}
	for (int i=MAX_LOG-1;i>=0;i--) {
		if (prof[pere[som2][i]]>=prof[som1]) {
			som2=pere[som2][i];
		}
	}
	if (som1==som2) {
		return som1;
	}
	for (int i=MAX_LOG-1;i>=0;i--) {
		if (pere[som1][i]!=pere[som2][i]) {
			som1=pere[som1][i];
			som2=pere[som2][i];
		}
	}
	return pere[som1][0];
}

void DFS2(int pos) {
	for (int i:adja[pos]) {
		if (i!=pere[pos][0]) {
			DFS2(i);
			cumu[pos]+=cumu[i];
		}
	} 
	if (cumu[pos]==0) {
		nbCompo++;
		marque(pos,nbCompo);
	}
}

signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>nbSom>>nbGroupe;
	for (int i=1;i<nbSom;i++) {
		cin>>deb>>fin;
		adja[deb].push_back(fin);
		adja[fin].push_back(deb);
		listeAre.push_back({deb,fin});
	}
	DFS(1,0);
	for (int j=1;j<MAX_LOG;j++) {
		for (int i=1;i<=nbSom;i++) {
			pere[i][j]=pere[pere[i][j-1]][j-1];
		}
	}
	for (int i=1;i<=nbSom;i++) {
		cin>>numGroupe[i];
		numGroupe[i]--;
		nbOccu[numGroupe[i]]++;
		if (nbOccu[numGroupe[i]]==1) {
			lcaGroupe[numGroupe[i]]=i;
		}
		else {
			lcaGroupe[numGroupe[i]]=calcLca(lcaGroupe[numGroupe[i]],i);
		}
	}
	for (int i=1;i<=nbSom;i++) {
		cumu[i]++;
		cumu[lcaGroupe[numGroupe[i]]]--;
	}
	DFS2(1);
	for (auto j:listeAre) {
		deb=j.first;
		fin=j.second;
		if (numCompo[deb]!=numCompo[fin]) {
			nbVois[numCompo[deb]]++;
			nbVois[numCompo[fin]]++;
		}
	}
	/*cout<<nbCompo<<endl;
	for (int i=1;i<=nbSom;i++) {
		cout<<i<<" : "<<numCompo[i]<<endl;
	}*/
	for (int i=1;i<=nbCompo;i++) {
		if (nbVois[i]==1) {
			rep++;
		}
	}
	if (rep==1) {
		cout<<0<<endl;
	}
	else {
		cout<<(rep+1)/2<<endl;
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 12064 KB Output is correct
2 Correct 5 ms 12116 KB Output is correct
3 Correct 6 ms 12072 KB Output is correct
4 Correct 6 ms 12060 KB Output is correct
5 Correct 5 ms 12016 KB Output is correct
6 Correct 6 ms 12116 KB Output is correct
7 Correct 6 ms 12072 KB Output is correct
8 Correct 6 ms 12116 KB Output is correct
9 Correct 6 ms 12116 KB Output is correct
10 Correct 6 ms 12116 KB Output is correct
11 Correct 8 ms 12136 KB Output is correct
12 Correct 6 ms 12116 KB Output is correct
13 Correct 6 ms 12116 KB Output is correct
14 Correct 8 ms 12072 KB Output is correct
15 Correct 6 ms 12116 KB Output is correct
16 Correct 6 ms 12072 KB Output is correct
17 Correct 7 ms 12068 KB Output is correct
18 Correct 6 ms 12116 KB Output is correct
19 Correct 5 ms 12068 KB Output is correct
20 Correct 5 ms 12116 KB Output is correct
21 Correct 5 ms 12072 KB Output is correct
22 Correct 9 ms 12072 KB Output is correct
23 Correct 6 ms 12056 KB Output is correct
24 Correct 7 ms 12116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 12064 KB Output is correct
2 Correct 5 ms 12116 KB Output is correct
3 Correct 6 ms 12072 KB Output is correct
4 Correct 6 ms 12060 KB Output is correct
5 Correct 5 ms 12016 KB Output is correct
6 Correct 6 ms 12116 KB Output is correct
7 Correct 6 ms 12072 KB Output is correct
8 Correct 6 ms 12116 KB Output is correct
9 Correct 6 ms 12116 KB Output is correct
10 Correct 6 ms 12116 KB Output is correct
11 Correct 8 ms 12136 KB Output is correct
12 Correct 6 ms 12116 KB Output is correct
13 Correct 6 ms 12116 KB Output is correct
14 Correct 8 ms 12072 KB Output is correct
15 Correct 6 ms 12116 KB Output is correct
16 Correct 6 ms 12072 KB Output is correct
17 Correct 7 ms 12068 KB Output is correct
18 Correct 6 ms 12116 KB Output is correct
19 Correct 5 ms 12068 KB Output is correct
20 Correct 5 ms 12116 KB Output is correct
21 Correct 5 ms 12072 KB Output is correct
22 Correct 9 ms 12072 KB Output is correct
23 Correct 6 ms 12056 KB Output is correct
24 Correct 7 ms 12116 KB Output is correct
25 Correct 6 ms 12116 KB Output is correct
26 Correct 9 ms 12592 KB Output is correct
27 Correct 7 ms 12500 KB Output is correct
28 Correct 8 ms 12628 KB Output is correct
29 Correct 8 ms 12500 KB Output is correct
30 Correct 6 ms 12500 KB Output is correct
31 Correct 8 ms 12072 KB Output is correct
32 Correct 9 ms 12764 KB Output is correct
33 Correct 7 ms 12068 KB Output is correct
34 Correct 10 ms 12468 KB Output is correct
35 Correct 7 ms 12520 KB Output is correct
36 Correct 9 ms 12464 KB Output is correct
37 Correct 10 ms 12472 KB Output is correct
38 Correct 6 ms 12064 KB Output is correct
39 Correct 7 ms 12500 KB Output is correct
40 Correct 7 ms 12468 KB Output is correct
41 Correct 10 ms 12464 KB Output is correct
42 Correct 10 ms 12548 KB Output is correct
43 Correct 8 ms 12756 KB Output is correct
44 Correct 7 ms 12116 KB Output is correct
45 Correct 9 ms 12560 KB Output is correct
46 Correct 7 ms 12500 KB Output is correct
47 Correct 6 ms 12068 KB Output is correct
48 Correct 8 ms 12564 KB Output is correct
49 Correct 10 ms 12628 KB Output is correct
50 Correct 8 ms 12628 KB Output is correct
51 Correct 9 ms 12500 KB Output is correct
52 Correct 7 ms 12500 KB Output is correct
53 Correct 7 ms 12552 KB Output is correct
54 Correct 7 ms 12464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 12064 KB Output is correct
2 Correct 5 ms 12116 KB Output is correct
3 Correct 6 ms 12072 KB Output is correct
4 Correct 6 ms 12060 KB Output is correct
5 Correct 5 ms 12016 KB Output is correct
6 Correct 6 ms 12116 KB Output is correct
7 Correct 6 ms 12072 KB Output is correct
8 Correct 6 ms 12116 KB Output is correct
9 Correct 6 ms 12116 KB Output is correct
10 Correct 6 ms 12116 KB Output is correct
11 Correct 8 ms 12136 KB Output is correct
12 Correct 6 ms 12116 KB Output is correct
13 Correct 6 ms 12116 KB Output is correct
14 Correct 8 ms 12072 KB Output is correct
15 Correct 6 ms 12116 KB Output is correct
16 Correct 6 ms 12072 KB Output is correct
17 Correct 7 ms 12068 KB Output is correct
18 Correct 6 ms 12116 KB Output is correct
19 Correct 5 ms 12068 KB Output is correct
20 Correct 5 ms 12116 KB Output is correct
21 Correct 5 ms 12072 KB Output is correct
22 Correct 9 ms 12072 KB Output is correct
23 Correct 6 ms 12056 KB Output is correct
24 Correct 7 ms 12116 KB Output is correct
25 Correct 7 ms 12072 KB Output is correct
26 Correct 55 ms 27228 KB Output is correct
27 Correct 88 ms 26880 KB Output is correct
28 Correct 8 ms 12500 KB Output is correct
29 Correct 7 ms 12116 KB Output is correct
30 Correct 7 ms 12116 KB Output is correct
31 Correct 92 ms 26872 KB Output is correct
32 Correct 8 ms 12464 KB Output is correct
33 Correct 120 ms 34140 KB Output is correct
34 Correct 83 ms 26932 KB Output is correct
35 Correct 10 ms 12488 KB Output is correct
36 Correct 111 ms 27676 KB Output is correct
37 Correct 8 ms 12504 KB Output is correct
38 Correct 7 ms 12500 KB Output is correct
39 Correct 75 ms 27128 KB Output is correct
40 Correct 8 ms 12720 KB Output is correct
41 Correct 85 ms 26880 KB Output is correct
42 Correct 108 ms 29288 KB Output is correct
43 Correct 8 ms 12036 KB Output is correct
44 Correct 121 ms 34336 KB Output is correct
45 Correct 118 ms 30584 KB Output is correct
46 Correct 7 ms 12500 KB Output is correct
47 Correct 9 ms 12536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 27164 KB Output is correct
2 Correct 65 ms 28696 KB Output is correct
3 Correct 7 ms 12500 KB Output is correct
4 Correct 7 ms 12500 KB Output is correct
5 Correct 6 ms 12068 KB Output is correct
6 Correct 6 ms 12116 KB Output is correct
7 Correct 8 ms 12484 KB Output is correct
8 Correct 75 ms 27672 KB Output is correct
9 Correct 8 ms 12472 KB Output is correct
10 Correct 82 ms 27000 KB Output is correct
11 Correct 6 ms 12116 KB Output is correct
12 Correct 116 ms 27148 KB Output is correct
13 Correct 78 ms 27600 KB Output is correct
14 Correct 67 ms 28384 KB Output is correct
15 Correct 61 ms 27204 KB Output is correct
16 Correct 7 ms 12520 KB Output is correct
17 Correct 6 ms 12116 KB Output is correct
18 Correct 53 ms 28564 KB Output is correct
19 Correct 89 ms 32376 KB Output is correct
20 Correct 12 ms 12584 KB Output is correct
21 Correct 6 ms 12136 KB Output is correct
22 Correct 68 ms 27844 KB Output is correct
23 Correct 7 ms 12500 KB Output is correct
24 Correct 82 ms 27164 KB Output is correct
25 Correct 79 ms 30960 KB Output is correct
26 Correct 8 ms 12596 KB Output is correct
27 Correct 7 ms 12672 KB Output is correct
28 Correct 7 ms 12500 KB Output is correct
29 Correct 7 ms 12464 KB Output is correct
30 Correct 7 ms 12500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 12064 KB Output is correct
2 Correct 5 ms 12116 KB Output is correct
3 Correct 6 ms 12072 KB Output is correct
4 Correct 6 ms 12060 KB Output is correct
5 Correct 5 ms 12016 KB Output is correct
6 Correct 6 ms 12116 KB Output is correct
7 Correct 6 ms 12072 KB Output is correct
8 Correct 6 ms 12116 KB Output is correct
9 Correct 6 ms 12116 KB Output is correct
10 Correct 6 ms 12116 KB Output is correct
11 Correct 8 ms 12136 KB Output is correct
12 Correct 6 ms 12116 KB Output is correct
13 Correct 6 ms 12116 KB Output is correct
14 Correct 8 ms 12072 KB Output is correct
15 Correct 6 ms 12116 KB Output is correct
16 Correct 6 ms 12072 KB Output is correct
17 Correct 7 ms 12068 KB Output is correct
18 Correct 6 ms 12116 KB Output is correct
19 Correct 5 ms 12068 KB Output is correct
20 Correct 5 ms 12116 KB Output is correct
21 Correct 5 ms 12072 KB Output is correct
22 Correct 9 ms 12072 KB Output is correct
23 Correct 6 ms 12056 KB Output is correct
24 Correct 7 ms 12116 KB Output is correct
25 Correct 6 ms 12116 KB Output is correct
26 Correct 9 ms 12592 KB Output is correct
27 Correct 7 ms 12500 KB Output is correct
28 Correct 8 ms 12628 KB Output is correct
29 Correct 8 ms 12500 KB Output is correct
30 Correct 6 ms 12500 KB Output is correct
31 Correct 8 ms 12072 KB Output is correct
32 Correct 9 ms 12764 KB Output is correct
33 Correct 7 ms 12068 KB Output is correct
34 Correct 10 ms 12468 KB Output is correct
35 Correct 7 ms 12520 KB Output is correct
36 Correct 9 ms 12464 KB Output is correct
37 Correct 10 ms 12472 KB Output is correct
38 Correct 6 ms 12064 KB Output is correct
39 Correct 7 ms 12500 KB Output is correct
40 Correct 7 ms 12468 KB Output is correct
41 Correct 10 ms 12464 KB Output is correct
42 Correct 10 ms 12548 KB Output is correct
43 Correct 8 ms 12756 KB Output is correct
44 Correct 7 ms 12116 KB Output is correct
45 Correct 9 ms 12560 KB Output is correct
46 Correct 7 ms 12500 KB Output is correct
47 Correct 6 ms 12068 KB Output is correct
48 Correct 8 ms 12564 KB Output is correct
49 Correct 10 ms 12628 KB Output is correct
50 Correct 8 ms 12628 KB Output is correct
51 Correct 9 ms 12500 KB Output is correct
52 Correct 7 ms 12500 KB Output is correct
53 Correct 7 ms 12552 KB Output is correct
54 Correct 7 ms 12464 KB Output is correct
55 Correct 7 ms 12072 KB Output is correct
56 Correct 55 ms 27228 KB Output is correct
57 Correct 88 ms 26880 KB Output is correct
58 Correct 8 ms 12500 KB Output is correct
59 Correct 7 ms 12116 KB Output is correct
60 Correct 7 ms 12116 KB Output is correct
61 Correct 92 ms 26872 KB Output is correct
62 Correct 8 ms 12464 KB Output is correct
63 Correct 120 ms 34140 KB Output is correct
64 Correct 83 ms 26932 KB Output is correct
65 Correct 10 ms 12488 KB Output is correct
66 Correct 111 ms 27676 KB Output is correct
67 Correct 8 ms 12504 KB Output is correct
68 Correct 7 ms 12500 KB Output is correct
69 Correct 75 ms 27128 KB Output is correct
70 Correct 8 ms 12720 KB Output is correct
71 Correct 85 ms 26880 KB Output is correct
72 Correct 108 ms 29288 KB Output is correct
73 Correct 8 ms 12036 KB Output is correct
74 Correct 121 ms 34336 KB Output is correct
75 Correct 118 ms 30584 KB Output is correct
76 Correct 7 ms 12500 KB Output is correct
77 Correct 9 ms 12536 KB Output is correct
78 Correct 59 ms 27164 KB Output is correct
79 Correct 65 ms 28696 KB Output is correct
80 Correct 7 ms 12500 KB Output is correct
81 Correct 7 ms 12500 KB Output is correct
82 Correct 6 ms 12068 KB Output is correct
83 Correct 6 ms 12116 KB Output is correct
84 Correct 8 ms 12484 KB Output is correct
85 Correct 75 ms 27672 KB Output is correct
86 Correct 8 ms 12472 KB Output is correct
87 Correct 82 ms 27000 KB Output is correct
88 Correct 6 ms 12116 KB Output is correct
89 Correct 116 ms 27148 KB Output is correct
90 Correct 78 ms 27600 KB Output is correct
91 Correct 67 ms 28384 KB Output is correct
92 Correct 61 ms 27204 KB Output is correct
93 Correct 7 ms 12520 KB Output is correct
94 Correct 6 ms 12116 KB Output is correct
95 Correct 53 ms 28564 KB Output is correct
96 Correct 89 ms 32376 KB Output is correct
97 Correct 12 ms 12584 KB Output is correct
98 Correct 6 ms 12136 KB Output is correct
99 Correct 68 ms 27844 KB Output is correct
100 Correct 7 ms 12500 KB Output is correct
101 Correct 82 ms 27164 KB Output is correct
102 Correct 79 ms 30960 KB Output is correct
103 Correct 8 ms 12596 KB Output is correct
104 Correct 7 ms 12672 KB Output is correct
105 Correct 7 ms 12500 KB Output is correct
106 Correct 7 ms 12464 KB Output is correct
107 Correct 7 ms 12500 KB Output is correct
108 Correct 401 ms 92168 KB Output is correct
109 Correct 1054 ms 104752 KB Output is correct
110 Correct 1066 ms 115996 KB Output is correct
111 Correct 763 ms 117864 KB Output is correct
112 Correct 734 ms 107284 KB Output is correct
113 Correct 388 ms 95652 KB Output is correct
114 Correct 662 ms 86732 KB Output is correct
115 Correct 590 ms 86704 KB Output is correct
116 Correct 634 ms 89392 KB Output is correct
117 Correct 562 ms 94860 KB Output is correct
118 Correct 601 ms 86264 KB Output is correct
119 Correct 523 ms 94968 KB Output is correct
120 Correct 622 ms 108984 KB Output is correct
121 Correct 520 ms 94948 KB Output is correct
122 Correct 641 ms 91640 KB Output is correct
123 Correct 346 ms 96480 KB Output is correct
124 Correct 706 ms 92468 KB Output is correct