답안 #634667

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
634667 2022-08-24T16:41:32 Z mrkhan2000 Monthly railway pass (LMIO18_menesinis_bilietas) C++17
100 / 100
642 ms 71508 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> data;
	vector<int> deg;
public:
	union_find(int N) : N(N), components(N), data(N, -1), deg(N, 0) {}
	int find(int u) {
		if(u >= N) return -1;
		while(data[u] >= 0) u = data[u];
		return u;
	}
	bool unite(int u, int v) {
		if(u >= N || v >= N) return false;
		u = find(u);
		v = find(v);
		if(u == v) return false;
		if(data[u] > data[v]) swap(u, v);
		data[u] += data[v];
		data[v] = u;
		--components;
		return true;
	}
	int size(int u) { return -data[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 144 ms 10580 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 124 ms 27784 KB Output is correct
5 Correct 3 ms 980 KB Output is correct
6 Correct 39 ms 2680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 124 ms 27784 KB Output is correct
2 Correct 3 ms 980 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 4 ms 1108 KB Output is correct
6 Correct 334 ms 40828 KB Output is correct
7 Correct 642 ms 71508 KB Output is correct
8 Correct 11 ms 2900 KB Output is correct
9 Correct 25 ms 4336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 320 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 320 KB Output is correct
5 Correct 4 ms 1108 KB Output is correct
6 Correct 1 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 4 ms 860 KB Output is correct
11 Correct 3 ms 724 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 4 ms 1108 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 340 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 328 KB Output is correct
20 Correct 2 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 4 ms 860 KB Output is correct
6 Correct 3 ms 724 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 4 ms 1108 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 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 328 KB Output is correct
15 Correct 2 ms 468 KB Output is correct
16 Correct 1 ms 320 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 320 KB Output is correct
20 Correct 4 ms 1108 KB Output is correct
21 Correct 3 ms 980 KB Output is correct
22 Correct 11 ms 2900 KB Output is correct
23 Correct 25 ms 4336 KB Output is correct
24 Correct 39 ms 2680 KB Output is correct
25 Correct 11 ms 2344 KB Output is correct
26 Correct 131 ms 24564 KB Output is correct
27 Correct 48 ms 10532 KB Output is correct
28 Correct 131 ms 19112 KB Output is correct
29 Correct 38 ms 8196 KB Output is correct
30 Correct 110 ms 15424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 4 ms 860 KB Output is correct
6 Correct 3 ms 724 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 4 ms 1108 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 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 328 KB Output is correct
15 Correct 2 ms 468 KB Output is correct
16 Correct 11 ms 2344 KB Output is correct
17 Correct 131 ms 24564 KB Output is correct
18 Correct 48 ms 10532 KB Output is correct
19 Correct 131 ms 19112 KB Output is correct
20 Correct 38 ms 8196 KB Output is correct
21 Correct 110 ms 15424 KB Output is correct
22 Correct 144 ms 10580 KB Output is correct
23 Correct 1 ms 320 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 124 ms 27784 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 320 KB Output is correct
28 Correct 4 ms 1108 KB Output is correct
29 Correct 3 ms 980 KB Output is correct
30 Correct 334 ms 40828 KB Output is correct
31 Correct 642 ms 71508 KB Output is correct
32 Correct 11 ms 2900 KB Output is correct
33 Correct 25 ms 4336 KB Output is correct
34 Correct 39 ms 2680 KB Output is correct
35 Correct 30 ms 4236 KB Output is correct
36 Correct 213 ms 28596 KB Output is correct
37 Correct 71 ms 5808 KB Output is correct