답안 #634677

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
634677 2022-08-24T17:04:27 Z mrkhan2000 Monthly railway pass (LMIO18_menesinis_bilietas) C++17
100 / 100
542 ms 65284 KB
#include <bits/stdc++.h>
#define ll long long
#define MOD 1000000007
using namespace std;

class union_find {
private:
	int N;
	int components;
	vector<int> sz, pa;
public:
	union_find(int N) : N(N), components(N), sz(vector<int>(N,1)), pa(vector<int>(N)) {
		iota(pa.begin(), pa.end(), 0);
	}
	void unite(int u, int v) {
		if(u >= N || v >= N) return;
		u = find(u);
		v = find(v);
		if(u == v) return;
		if(sz[u] < sz[v]) swap(u, v);
		sz[u] += sz[v];
		pa[v] = u;
		--components;
	}
	int find(int u) {
		if(u >= N) return -1;
		while(pa[u]^u) u = pa[u];
		return u;
	}
	int _size(int u) {
		return sz[find(u)];
	}
	int getComponentsCount() {
		return components;
	}
};


void solve() {
	int N, E; cin >> N >> E;
	union_find UF(N);
	vector<vector<int>> bcns;
	bcns.reserve(E);
	for(int i = 0; i < E; i++) {
		int U, V; cin >> U >> V;
		char T; cin >> T;
		--U, --V;
		if(T == 'T') UF.unite(U, V);
		else bcns.push_back({U,V});
	}
	set<int> rep;
	for(int i = 0; i < N; i++) {
		rep.insert(UF.find(i));
	}
	vector<int> deg(N);
	set<pair<int,int>> done;
	for(auto &cns : bcns) if(
		UF.find(cns[0])^UF.find(cns[1]) && 
		done.find({UF.find(cns[0]), UF.find(cns[1])}) == done.end()
	) {
		deg[UF.find(cns[0])]++;
		deg[UF.find(cns[1])]++;
		done.insert({UF.find(cns[0]), UF.find(cns[1])});
	}
	set<int> work;
	for(auto city : rep) {
		if(deg[city] == UF.getComponentsCount() - 1) {
			work.insert(city);
		}
	}
	int ans = 0;
	for(int i = 0; i < N; i++) {
		if(work.find(UF.find(i)) != work.end()) {
			++ans;
		}
	}
	cout << ans << '\n';
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int test_cases = 1;
	// cin >> test_cases;
	for(int test_case = 1; test_case <= test_cases; test_case++) {
		// cout << "Case #" << test_case << ": ";
		solve();
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 109 ms 3688 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 119 ms 27788 KB Output is correct
5 Correct 2 ms 980 KB Output is correct
6 Correct 35 ms 392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 119 ms 27788 KB Output is correct
2 Correct 2 ms 980 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 4 ms 1108 KB Output is correct
6 Correct 303 ms 36576 KB Output is correct
7 Correct 542 ms 65284 KB Output is correct
8 Correct 11 ms 2644 KB Output is correct
9 Correct 19 ms 4052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 4 ms 1108 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 2 ms 596 KB Output is correct
10 Correct 3 ms 800 KB Output is correct
11 Correct 3 ms 724 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 4 ms 980 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 2 ms 344 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 2 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 2 ms 596 KB Output is correct
5 Correct 3 ms 800 KB Output is correct
6 Correct 3 ms 724 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 4 ms 980 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 2 ms 344 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 2 ms 468 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 4 ms 1108 KB Output is correct
21 Correct 2 ms 980 KB Output is correct
22 Correct 11 ms 2644 KB Output is correct
23 Correct 19 ms 4052 KB Output is correct
24 Correct 35 ms 392 KB Output is correct
25 Correct 12 ms 2040 KB Output is correct
26 Correct 127 ms 20040 KB Output is correct
27 Correct 45 ms 9032 KB Output is correct
28 Correct 116 ms 17132 KB Output is correct
29 Correct 37 ms 7096 KB Output is correct
30 Correct 97 ms 13288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 2 ms 596 KB Output is correct
5 Correct 3 ms 800 KB Output is correct
6 Correct 3 ms 724 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 4 ms 980 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 2 ms 344 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 2 ms 468 KB Output is correct
16 Correct 12 ms 2040 KB Output is correct
17 Correct 127 ms 20040 KB Output is correct
18 Correct 45 ms 9032 KB Output is correct
19 Correct 116 ms 17132 KB Output is correct
20 Correct 37 ms 7096 KB Output is correct
21 Correct 97 ms 13288 KB Output is correct
22 Correct 109 ms 3688 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 119 ms 27788 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 4 ms 1108 KB Output is correct
29 Correct 2 ms 980 KB Output is correct
30 Correct 303 ms 36576 KB Output is correct
31 Correct 542 ms 65284 KB Output is correct
32 Correct 11 ms 2644 KB Output is correct
33 Correct 19 ms 4052 KB Output is correct
34 Correct 35 ms 392 KB Output is correct
35 Correct 28 ms 3412 KB Output is correct
36 Correct 206 ms 22668 KB Output is correct
37 Correct 63 ms 2868 KB Output is correct