Submission #583544

# Submission time Handle Problem Language Result Execution time Memory
583544 2022-06-25T14:28:00 Z Markomafko972 KOVANICE (COI15_kovanice) C++14
100 / 100
232 ms 30524 KB
#include <bits/stdc++.h>
using namespace std;

int n, m, q;
int a[300005];
int b[300005];
char w[300005];
int prt[300005];
vector<int> v[300005];
int dub[300005];
int sol[300005];

int find(int x) {
	if (prt[x] == x) return x;
	return prt[x] = find(prt[x]);
}

void unija(int x, int y) {
	x = find(x);
	y = find(y);
	if (x == y) return;
	prt[y] = x;
}

int dfs(int x) {
	if (dub[x] > 0) return dub[x];
	
	for (int i = 0; i < v[x].size(); i++) {
		dub[x] = max(dub[x], dfs(v[x][i]));
	}
	
	dub[x]++;
	return dub[x];
}

void rek(int x) {
	sol[x] = dub[x];
	
	for (int i = 0; i < v[x].size(); i++) {
		if (sol[v[x][i]] == 0 && dub[v[x][i]]+1 == dub[x]) rek(v[x][i]);
	}
}

int main () {
	
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	cin >> n >> m >> q;
	for (int i = 1; i <= m; i++) prt[i] = i;
	
	for (int i = 0; i < q; i++) {
		cin >> a[i] >> w[i] >> b[i];
		if (w[i] == '=') unija(a[i], b[i]);
	}
	
	for (int i = 0; i < q; i++) {
		int poc = find(a[i]), kr = find(b[i]);
		if (w[i] == '>') v[poc].push_back(kr);
		else if (w[i] == '<') v[kr].push_back(poc);
	}
	
	for (int i = 1; i <= m; i++) {
		if (dub[find(i)] == 0) dfs(find(i));
	}
	
	for (int i = 1; i <= m; i++) {
		if (sol[find(i)] == 0 && dub[find(i)] == n) rek(find(i));
	}
	
	for (int i = 1; i <= m; i++) {
		if (sol[find(i)] == 0) cout << "?\n";
		else cout << "K" << sol[find(i)] << "\n";
	}
	
	return 0;
}

Compilation message

kovanice.cpp: In function 'int dfs(int)':
kovanice.cpp:28:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |  for (int i = 0; i < v[x].size(); i++) {
      |                  ~~^~~~~~~~~~~~~
kovanice.cpp: In function 'void rek(int)':
kovanice.cpp:39:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |  for (int i = 0; i < v[x].size(); i++) {
      |                  ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7380 KB Output is correct
2 Correct 5 ms 7396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 13696 KB Output is correct
2 Correct 69 ms 13728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 9656 KB Output is correct
2 Correct 21 ms 9592 KB Output is correct
3 Correct 21 ms 9548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 180 ms 22760 KB Output is correct
2 Correct 186 ms 22408 KB Output is correct
3 Correct 159 ms 22316 KB Output is correct
4 Correct 172 ms 23772 KB Output is correct
5 Correct 232 ms 30524 KB Output is correct