답안 #109248

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
109248 2019-05-05T20:16:05 Z hugo_pm Mergers (JOI19_mergers) C++17
100 / 100
1762 ms 81400 KB
#include <bits/stdc++.h>
using namespace std;

#define form2(i, a, b) for (int i = (a); i < (b); ++i)
#define ford2(i, a, b) for (int i = (a-1); i >= (b); --i)
#define form(i, n) form2(i, 0, n)
#define ford(i, n) ford2(i, n, 0)

#define chmax(x, v) x = max(x, (v))
#define chmin(x, v) x = min(x, (v))
#define fi first
#define se second

const long long BIG = 1000000000000000000LL;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

void solve();
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	solve();
	return 0;
}

const int borne = 1000*1000 + 15;
int nbNoeuds, nbAretes, nbGroupes;
stack<int> dfsPile;

pii noeudsArete[borne];
vector<int> adjAr[borne];

int voisin(int ar, int nod)
{
	pii arete = noeudsArete[ar];

	if (arete.fi == nod) return arete.se;
	else { return arete.fi; }
}

int dfsNum[borne], dfsLow[borne];
int arFrom[borne];
bitset<borne> estPont;
int curTemps = 0;

void dfsTarjan(int dep)
{
	dfsPile.push(dep);
	while (!dfsPile.empty()) {
		int nod = dfsPile.top();
		dfsPile.pop();
		if (dfsNum[nod] == 0) {
			dfsPile.push(nod);

			dfsNum[nod] = ++curTemps;
			dfsLow[nod] = dfsNum[nod];
			for (int arete : adjAr[nod]) {
				int vo = voisin(arete, nod);
				if (dfsNum[vo] > 0 && arete != arFrom[nod]) {
					chmin(dfsLow[nod], dfsLow[vo]);
				}
				if (dfsNum[vo] == 0) {
					arFrom[vo] = arete;
					if (nod == noeudsArete[arete].se) swap(noeudsArete[arete].fi, noeudsArete[arete].se);
					dfsPile.push(vo);
				}
			}

		} else {
			for (int arete : adjAr[nod]) {
				int vo = voisin(arete, nod);
				if (arFrom[vo] == arete) {
					chmin(dfsLow[nod], dfsLow[vo]);
				}
			}
		}
	}	
}

void finaliserPonts()
{
	form(i, nbAretes) {
		if (dfsNum[noeudsArete[i].fi] < dfsLow[noeudsArete[i].se]) estPont[i] = true;
	}
}

//vector<int> bridgeTree[borne];
char degBT[borne];

int nbComp = 0;
int indComp[borne];
bool vuFinaliser[borne];
int lst[borne];

void compresser(int dep)
{
	dfsPile.push(dep);
	indComp[dep] = nbComp;
	while (! dfsPile.empty()) {
		int nod = dfsPile.top();
		dfsPile.pop();

		for (int arete : adjAr[nod]) if (!estPont[arete]) {
			int vo = voisin(arete, nod);
			if (indComp[vo] == -1) {
				indComp[vo] = nbComp;
				dfsPile.push(vo);
			}
		}
	}
}

void finaliser(int dep)
{
	stack<int> dfsPile;
	dfsPile.push(dep);
	vuFinaliser[dep] = true;

	while (!dfsPile.empty()) {
		int nod = dfsPile.top();
		dfsPile.pop();

		int maComp = indComp[nod];
		for (int arete : adjAr[nod]) if (estPont[arete]) {
			int voRaw = voisin(arete, nod);
			int voComp = indComp[voRaw];
			if (lst[voComp] == maComp) continue;
			lst[voComp] = maComp;

			//bridgeTree[maComp].push_back(voComp);
			if (degBT[maComp] < 2) ++degBT[maComp];
			// Ajouté dans l'autre sens lors de la finalisation de voComp
		}

		for (int arete : adjAr[nod]) {
			if (!estPont[arete]) {
				int vo = voisin(arete, nod);
				if (!vuFinaliser[vo]) {
					vuFinaliser[vo] = true;
					dfsPile.push(vo);
				}
			}
		}
	}
}

int lstGroupe[borne];

void solve()
{
	fill_n(&indComp[0], borne, -1);
	fill_n(&lst[0], borne, -1);
	fill_n(&lstGroupe[0], borne, -1);

	cin >> nbNoeuds >> nbGroupes;
	nbAretes = nbNoeuds-1;
	form(i, nbAretes) {
		int u, v; cin >> u >> v; --u; --v;
		noeudsArete[i] = {u,v};
		adjAr[u].push_back(i);
		adjAr[v].push_back(i);
	}

	form(nod, nbNoeuds) {
		int groupe; cin >> groupe; --groupe;
		if (lstGroupe[groupe] != -1) {
			int prev = lstGroupe[groupe];

			noeudsArete[nbAretes] = {nod, prev};
			adjAr[nod].push_back(nbAretes);
			adjAr[prev].push_back(nbAretes);
			++nbAretes;
		}
		lstGroupe[groupe] = nod;
	}

	arFrom[0] = -1;
	dfsTarjan(0);
	finaliserPonts();

	form(nod, nbNoeuds) if (indComp[nod] == -1) {
		compresser(nod);
		++nbComp;
	}

	form(nod, nbNoeuds) if (!vuFinaliser[nod]) finaliser(nod);

	int nbFeuilles = 0;
	form(nod, nbComp) {
		//int deg = bridgeTree[nod].size();
		int deg = degBT[nod];
		if (deg == 1) ++nbFeuilles;
	}
	cout << (nbFeuilles+1) / 2 << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 35576 KB Output is correct
2 Correct 33 ms 35576 KB Output is correct
3 Correct 40 ms 35576 KB Output is correct
4 Correct 35 ms 35584 KB Output is correct
5 Correct 34 ms 35576 KB Output is correct
6 Correct 37 ms 35556 KB Output is correct
7 Correct 35 ms 35576 KB Output is correct
8 Correct 34 ms 35704 KB Output is correct
9 Correct 37 ms 35612 KB Output is correct
10 Correct 36 ms 35608 KB Output is correct
11 Correct 35 ms 35584 KB Output is correct
12 Correct 34 ms 35576 KB Output is correct
13 Correct 34 ms 35604 KB Output is correct
14 Correct 40 ms 35576 KB Output is correct
15 Correct 33 ms 35576 KB Output is correct
16 Correct 33 ms 35584 KB Output is correct
17 Correct 34 ms 35576 KB Output is correct
18 Correct 34 ms 35584 KB Output is correct
19 Correct 37 ms 35676 KB Output is correct
20 Correct 34 ms 35584 KB Output is correct
21 Correct 35 ms 35576 KB Output is correct
22 Correct 37 ms 35604 KB Output is correct
23 Correct 34 ms 35576 KB Output is correct
24 Correct 40 ms 35576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 35576 KB Output is correct
2 Correct 33 ms 35576 KB Output is correct
3 Correct 40 ms 35576 KB Output is correct
4 Correct 35 ms 35584 KB Output is correct
5 Correct 34 ms 35576 KB Output is correct
6 Correct 37 ms 35556 KB Output is correct
7 Correct 35 ms 35576 KB Output is correct
8 Correct 34 ms 35704 KB Output is correct
9 Correct 37 ms 35612 KB Output is correct
10 Correct 36 ms 35608 KB Output is correct
11 Correct 35 ms 35584 KB Output is correct
12 Correct 34 ms 35576 KB Output is correct
13 Correct 34 ms 35604 KB Output is correct
14 Correct 40 ms 35576 KB Output is correct
15 Correct 33 ms 35576 KB Output is correct
16 Correct 33 ms 35584 KB Output is correct
17 Correct 34 ms 35576 KB Output is correct
18 Correct 34 ms 35584 KB Output is correct
19 Correct 37 ms 35676 KB Output is correct
20 Correct 34 ms 35584 KB Output is correct
21 Correct 35 ms 35576 KB Output is correct
22 Correct 37 ms 35604 KB Output is correct
23 Correct 34 ms 35576 KB Output is correct
24 Correct 40 ms 35576 KB Output is correct
25 Correct 34 ms 35656 KB Output is correct
26 Correct 36 ms 35832 KB Output is correct
27 Correct 36 ms 35840 KB Output is correct
28 Correct 35 ms 35832 KB Output is correct
29 Correct 44 ms 35856 KB Output is correct
30 Correct 40 ms 35832 KB Output is correct
31 Correct 41 ms 35580 KB Output is correct
32 Correct 34 ms 35840 KB Output is correct
33 Correct 35 ms 35556 KB Output is correct
34 Correct 38 ms 35864 KB Output is correct
35 Correct 37 ms 35780 KB Output is correct
36 Correct 33 ms 35840 KB Output is correct
37 Correct 36 ms 35836 KB Output is correct
38 Correct 32 ms 35584 KB Output is correct
39 Correct 34 ms 35832 KB Output is correct
40 Correct 34 ms 35832 KB Output is correct
41 Correct 33 ms 35832 KB Output is correct
42 Correct 32 ms 35840 KB Output is correct
43 Correct 33 ms 35832 KB Output is correct
44 Correct 31 ms 35584 KB Output is correct
45 Correct 33 ms 35832 KB Output is correct
46 Correct 38 ms 35812 KB Output is correct
47 Correct 34 ms 35576 KB Output is correct
48 Correct 32 ms 35840 KB Output is correct
49 Correct 32 ms 35960 KB Output is correct
50 Correct 32 ms 35832 KB Output is correct
51 Correct 34 ms 35832 KB Output is correct
52 Correct 39 ms 35832 KB Output is correct
53 Correct 41 ms 35840 KB Output is correct
54 Correct 33 ms 35832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 35576 KB Output is correct
2 Correct 33 ms 35576 KB Output is correct
3 Correct 40 ms 35576 KB Output is correct
4 Correct 35 ms 35584 KB Output is correct
5 Correct 34 ms 35576 KB Output is correct
6 Correct 37 ms 35556 KB Output is correct
7 Correct 35 ms 35576 KB Output is correct
8 Correct 34 ms 35704 KB Output is correct
9 Correct 37 ms 35612 KB Output is correct
10 Correct 36 ms 35608 KB Output is correct
11 Correct 35 ms 35584 KB Output is correct
12 Correct 34 ms 35576 KB Output is correct
13 Correct 34 ms 35604 KB Output is correct
14 Correct 40 ms 35576 KB Output is correct
15 Correct 33 ms 35576 KB Output is correct
16 Correct 33 ms 35584 KB Output is correct
17 Correct 34 ms 35576 KB Output is correct
18 Correct 34 ms 35584 KB Output is correct
19 Correct 37 ms 35676 KB Output is correct
20 Correct 34 ms 35584 KB Output is correct
21 Correct 35 ms 35576 KB Output is correct
22 Correct 37 ms 35604 KB Output is correct
23 Correct 34 ms 35576 KB Output is correct
24 Correct 40 ms 35576 KB Output is correct
25 Correct 31 ms 35604 KB Output is correct
26 Correct 840 ms 43860 KB Output is correct
27 Correct 250 ms 44792 KB Output is correct
28 Correct 39 ms 35840 KB Output is correct
29 Correct 33 ms 35576 KB Output is correct
30 Correct 33 ms 35576 KB Output is correct
31 Correct 284 ms 44152 KB Output is correct
32 Correct 36 ms 35832 KB Output is correct
33 Correct 232 ms 43896 KB Output is correct
34 Correct 242 ms 44716 KB Output is correct
35 Correct 45 ms 35832 KB Output is correct
36 Correct 205 ms 43200 KB Output is correct
37 Correct 39 ms 35832 KB Output is correct
38 Correct 35 ms 35940 KB Output is correct
39 Correct 469 ms 43972 KB Output is correct
40 Correct 33 ms 35832 KB Output is correct
41 Correct 144 ms 44536 KB Output is correct
42 Correct 140 ms 43592 KB Output is correct
43 Correct 31 ms 35584 KB Output is correct
44 Correct 128 ms 43644 KB Output is correct
45 Correct 248 ms 45432 KB Output is correct
46 Correct 34 ms 35832 KB Output is correct
47 Correct 42 ms 35832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 724 ms 43892 KB Output is correct
2 Correct 132 ms 43632 KB Output is correct
3 Correct 34 ms 35832 KB Output is correct
4 Correct 36 ms 35832 KB Output is correct
5 Correct 30 ms 35576 KB Output is correct
6 Correct 32 ms 35576 KB Output is correct
7 Correct 37 ms 35840 KB Output is correct
8 Correct 183 ms 43484 KB Output is correct
9 Correct 39 ms 35960 KB Output is correct
10 Correct 258 ms 44224 KB Output is correct
11 Correct 41 ms 35576 KB Output is correct
12 Correct 260 ms 43356 KB Output is correct
13 Correct 223 ms 43384 KB Output is correct
14 Correct 185 ms 42760 KB Output is correct
15 Correct 544 ms 43888 KB Output is correct
16 Correct 34 ms 35832 KB Output is correct
17 Correct 35 ms 35584 KB Output is correct
18 Correct 177 ms 43504 KB Output is correct
19 Correct 238 ms 42876 KB Output is correct
20 Correct 38 ms 35840 KB Output is correct
21 Correct 41 ms 35704 KB Output is correct
22 Correct 149 ms 43728 KB Output is correct
23 Correct 35 ms 35836 KB Output is correct
24 Correct 230 ms 43768 KB Output is correct
25 Correct 240 ms 42872 KB Output is correct
26 Correct 36 ms 35832 KB Output is correct
27 Correct 41 ms 35832 KB Output is correct
28 Correct 40 ms 35960 KB Output is correct
29 Correct 47 ms 35840 KB Output is correct
30 Correct 35 ms 35840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 35576 KB Output is correct
2 Correct 33 ms 35576 KB Output is correct
3 Correct 40 ms 35576 KB Output is correct
4 Correct 35 ms 35584 KB Output is correct
5 Correct 34 ms 35576 KB Output is correct
6 Correct 37 ms 35556 KB Output is correct
7 Correct 35 ms 35576 KB Output is correct
8 Correct 34 ms 35704 KB Output is correct
9 Correct 37 ms 35612 KB Output is correct
10 Correct 36 ms 35608 KB Output is correct
11 Correct 35 ms 35584 KB Output is correct
12 Correct 34 ms 35576 KB Output is correct
13 Correct 34 ms 35604 KB Output is correct
14 Correct 40 ms 35576 KB Output is correct
15 Correct 33 ms 35576 KB Output is correct
16 Correct 33 ms 35584 KB Output is correct
17 Correct 34 ms 35576 KB Output is correct
18 Correct 34 ms 35584 KB Output is correct
19 Correct 37 ms 35676 KB Output is correct
20 Correct 34 ms 35584 KB Output is correct
21 Correct 35 ms 35576 KB Output is correct
22 Correct 37 ms 35604 KB Output is correct
23 Correct 34 ms 35576 KB Output is correct
24 Correct 40 ms 35576 KB Output is correct
25 Correct 34 ms 35656 KB Output is correct
26 Correct 36 ms 35832 KB Output is correct
27 Correct 36 ms 35840 KB Output is correct
28 Correct 35 ms 35832 KB Output is correct
29 Correct 44 ms 35856 KB Output is correct
30 Correct 40 ms 35832 KB Output is correct
31 Correct 41 ms 35580 KB Output is correct
32 Correct 34 ms 35840 KB Output is correct
33 Correct 35 ms 35556 KB Output is correct
34 Correct 38 ms 35864 KB Output is correct
35 Correct 37 ms 35780 KB Output is correct
36 Correct 33 ms 35840 KB Output is correct
37 Correct 36 ms 35836 KB Output is correct
38 Correct 32 ms 35584 KB Output is correct
39 Correct 34 ms 35832 KB Output is correct
40 Correct 34 ms 35832 KB Output is correct
41 Correct 33 ms 35832 KB Output is correct
42 Correct 32 ms 35840 KB Output is correct
43 Correct 33 ms 35832 KB Output is correct
44 Correct 31 ms 35584 KB Output is correct
45 Correct 33 ms 35832 KB Output is correct
46 Correct 38 ms 35812 KB Output is correct
47 Correct 34 ms 35576 KB Output is correct
48 Correct 32 ms 35840 KB Output is correct
49 Correct 32 ms 35960 KB Output is correct
50 Correct 32 ms 35832 KB Output is correct
51 Correct 34 ms 35832 KB Output is correct
52 Correct 39 ms 35832 KB Output is correct
53 Correct 41 ms 35840 KB Output is correct
54 Correct 33 ms 35832 KB Output is correct
55 Correct 31 ms 35604 KB Output is correct
56 Correct 840 ms 43860 KB Output is correct
57 Correct 250 ms 44792 KB Output is correct
58 Correct 39 ms 35840 KB Output is correct
59 Correct 33 ms 35576 KB Output is correct
60 Correct 33 ms 35576 KB Output is correct
61 Correct 284 ms 44152 KB Output is correct
62 Correct 36 ms 35832 KB Output is correct
63 Correct 232 ms 43896 KB Output is correct
64 Correct 242 ms 44716 KB Output is correct
65 Correct 45 ms 35832 KB Output is correct
66 Correct 205 ms 43200 KB Output is correct
67 Correct 39 ms 35832 KB Output is correct
68 Correct 35 ms 35940 KB Output is correct
69 Correct 469 ms 43972 KB Output is correct
70 Correct 33 ms 35832 KB Output is correct
71 Correct 144 ms 44536 KB Output is correct
72 Correct 140 ms 43592 KB Output is correct
73 Correct 31 ms 35584 KB Output is correct
74 Correct 128 ms 43644 KB Output is correct
75 Correct 248 ms 45432 KB Output is correct
76 Correct 34 ms 35832 KB Output is correct
77 Correct 42 ms 35832 KB Output is correct
78 Correct 724 ms 43892 KB Output is correct
79 Correct 132 ms 43632 KB Output is correct
80 Correct 34 ms 35832 KB Output is correct
81 Correct 36 ms 35832 KB Output is correct
82 Correct 30 ms 35576 KB Output is correct
83 Correct 32 ms 35576 KB Output is correct
84 Correct 37 ms 35840 KB Output is correct
85 Correct 183 ms 43484 KB Output is correct
86 Correct 39 ms 35960 KB Output is correct
87 Correct 258 ms 44224 KB Output is correct
88 Correct 41 ms 35576 KB Output is correct
89 Correct 260 ms 43356 KB Output is correct
90 Correct 223 ms 43384 KB Output is correct
91 Correct 185 ms 42760 KB Output is correct
92 Correct 544 ms 43888 KB Output is correct
93 Correct 34 ms 35832 KB Output is correct
94 Correct 35 ms 35584 KB Output is correct
95 Correct 177 ms 43504 KB Output is correct
96 Correct 238 ms 42876 KB Output is correct
97 Correct 38 ms 35840 KB Output is correct
98 Correct 41 ms 35704 KB Output is correct
99 Correct 149 ms 43728 KB Output is correct
100 Correct 35 ms 35836 KB Output is correct
101 Correct 230 ms 43768 KB Output is correct
102 Correct 240 ms 42872 KB Output is correct
103 Correct 36 ms 35832 KB Output is correct
104 Correct 41 ms 35832 KB Output is correct
105 Correct 40 ms 35960 KB Output is correct
106 Correct 47 ms 35840 KB Output is correct
107 Correct 35 ms 35840 KB Output is correct
108 Correct 1230 ms 78080 KB Output is correct
109 Correct 1636 ms 75552 KB Output is correct
110 Correct 1483 ms 76684 KB Output is correct
111 Correct 1567 ms 73100 KB Output is correct
112 Correct 1524 ms 72596 KB Output is correct
113 Correct 981 ms 76704 KB Output is correct
114 Correct 1531 ms 80160 KB Output is correct
115 Correct 1572 ms 81400 KB Output is correct
116 Correct 1651 ms 78224 KB Output is correct
117 Correct 1422 ms 72800 KB Output is correct
118 Correct 1579 ms 77856 KB Output is correct
119 Correct 1274 ms 72856 KB Output is correct
120 Correct 1762 ms 73260 KB Output is correct
121 Correct 1316 ms 72860 KB Output is correct
122 Correct 1677 ms 77180 KB Output is correct
123 Correct 810 ms 76212 KB Output is correct
124 Correct 1302 ms 71416 KB Output is correct