Submission #920906

# Submission time Handle Problem Language Result Execution time Memory
920906 2024-02-03T07:32:47 Z theghostking1 Monthly railway pass (LMIO18_menesinis_bilietas) C++17
100 / 100
677 ms 86968 KB
#include <bits/stdc++.h>
using namespace std;

struct DSU{
	vector<int> parent,sz;
	DSU(int n){
		parent.resize(n);
		for (int i = 0; i<n; i++){
			parent[i] = i;
		}
		sz.resize(n,1);
	}
	int find(int x){
		if (x != parent[x]){
			return parent[x] = find(parent[x]);	
		}
		return x;
	}
	bool merge(int a, int b){
		a = find(a);
		b = find(b);
		if (a == b) return false;
		if (sz[a] < sz[b]) swap(a,b);
		parent[b] = a;
		sz[a] += sz[b];
		return true;
	}
	int getsz(int x){
		x = find(x);
		return sz[x];
	}
};

vector<set<int>> adj;

int main(){
	int n,m;
	cin >> n >> m;
	adj.resize(n);
	DSU dsu = DSU(n);
	DSU ck = DSU(n);
	vector<array<int,2>> all;
	for (int i = 0; i<m; i++){
		int a,b;
		char c;
		cin >> a >> b >> c;
		a--;b--;
		if (c == 'T'){
			dsu.merge(a,b);
		}
		ck.merge(a,b);
		if (c == 'T') continue;
		all.push_back({a,b});
	}
	if (ck.getsz(0) != n){
		cout << 0 << '\n';
	}
	else{
		set<int> tot;
		for (int i = 0; i<n; i++){
			tot.insert(dsu.find(i));
		}
		int num = tot.size();
		if (num <= 2){
			cout << n << '\n';
			return 0;
		}
		for (int i = 0; i<(int)all.size(); i++){
			int one = all[i][0];
			int two = all[i][1];
			one = dsu.find(one);
			two = dsu.find(two);
			if (one == two) continue;
			adj[one].insert(two);
			adj[two].insert(one);
		}
		int cnt = 0;
		for (int i = 0; i<n; i++){
			if (adj[i].size() == num-1){
				cnt += dsu.getsz(i);
			}
		}
		cout << cnt << '\n';
	}
	return 0;
}

Compilation message

menesinis_bilietas.cpp: In function 'int main()':
menesinis_bilietas.cpp:79:22: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   79 |    if (adj[i].size() == num-1){
      |        ~~~~~~~~~~~~~~^~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 239 ms 19036 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 11 ms 29784 KB Output is correct
5 Correct 1 ms 1112 KB Output is correct
6 Correct 83 ms 2388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 29784 KB Output is correct
2 Correct 1 ms 1112 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 4 ms 1116 KB Output is correct
6 Correct 459 ms 39656 KB Output is correct
7 Correct 677 ms 86968 KB Output is correct
8 Correct 12 ms 3420 KB Output is correct
9 Correct 21 ms 4840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 4 ms 1116 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 2 ms 348 KB Output is correct
10 Correct 5 ms 588 KB Output is correct
11 Correct 3 ms 548 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 440 KB Output is correct
14 Correct 5 ms 604 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 2 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 2 ms 348 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 5 ms 588 KB Output is correct
6 Correct 3 ms 548 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 440 KB Output is correct
9 Correct 5 ms 604 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 4 ms 1116 KB Output is correct
21 Correct 1 ms 1112 KB Output is correct
22 Correct 12 ms 3420 KB Output is correct
23 Correct 21 ms 4840 KB Output is correct
24 Correct 83 ms 2388 KB Output is correct
25 Correct 14 ms 1116 KB Output is correct
26 Correct 197 ms 8296 KB Output is correct
27 Correct 68 ms 3556 KB Output is correct
28 Correct 145 ms 14504 KB Output is correct
29 Correct 55 ms 2500 KB Output is correct
30 Correct 130 ms 12988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 2 ms 348 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 5 ms 588 KB Output is correct
6 Correct 3 ms 548 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 440 KB Output is correct
9 Correct 5 ms 604 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 14 ms 1116 KB Output is correct
17 Correct 197 ms 8296 KB Output is correct
18 Correct 68 ms 3556 KB Output is correct
19 Correct 145 ms 14504 KB Output is correct
20 Correct 55 ms 2500 KB Output is correct
21 Correct 130 ms 12988 KB Output is correct
22 Correct 239 ms 19036 KB Output is correct
23 Correct 0 ms 344 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 11 ms 29784 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 4 ms 1116 KB Output is correct
29 Correct 1 ms 1112 KB Output is correct
30 Correct 459 ms 39656 KB Output is correct
31 Correct 677 ms 86968 KB Output is correct
32 Correct 12 ms 3420 KB Output is correct
33 Correct 21 ms 4840 KB Output is correct
34 Correct 83 ms 2388 KB Output is correct
35 Correct 40 ms 4580 KB Output is correct
36 Correct 288 ms 16376 KB Output is correct
37 Correct 129 ms 14932 KB Output is correct