#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <climits>
#include <cmath>
#include <fstream>
#include <queue>
using namespace std;
#define MAXN 300005
vector<int> eq[MAXN];
vector<pair<int, int>> rest;
int n, m, k;
vector<int> adjList[MAXN];
int counts[MAXN];
bool checked[MAXN];
int compress[MAXN];
int ans[MAXN];
void fill(int src, int val) {
compress[src] = val;
for (auto i : eq[src]) {
if (compress[i] == -1) {
fill(i, val);
}
}
}
void dfs(int src, int depth) {
checked[src] = true;
for (auto i : adjList[src]) {
if (!checked[i]) {
dfs(i, depth + 1);
}
}
if (depth == n) {
ans[src] = 1;
return;
}
for (auto i : adjList[src]) {
if (ans[i] != -1) {
ans[src] = ans[i] + 1;
return;
}
}
}
int main() {
cin.tie();
ios_base::sync_with_stdio(0);
cin >> n >> m >> k;
for (int i = 0; i <= m; i++) {
counts[i] = 0;
compress[i] = -1;
ans[i] = -1;
checked[i] = false;
}
for (int i = 0; i < k; i++) {
string s;
cin >> s;
int node1 = 0;
int node2 = 0;
bool found = false;
char mark;
for (int a = 0; a < s.size(); a++) {
if (s[a] == '=' || s[a] == '<' || s[a] == '>') {
found = true;
mark = s[a];
continue;
}
if (found == false) {
node1 = node1 * 10 + (s[a] - 48);
}
else {
node2 = node2 * 10 + (s[a] - 48);
}
}
if (mark == '=') {
eq[node1].push_back(node2);
eq[node2].push_back(node1);
continue;
}
if (mark == '<') {
swap(node1, node2);
}
rest.push_back({ node1, node2 });
}
int counter = 1;
for (int i = 1; i <= m; i++) {
if (compress[i] == -1) {
fill(i, counter);
counter++;
}
}
for (int i = 0; i < rest.size(); i++) {
adjList[compress[rest[i].first]].push_back(compress[rest[i].second]);
counts[compress[rest[i].second]]++;
}
for (int i = 1; i <= m; i++) {
if (counts[i] == 0) {
dfs(i, 1);
}
}
for (int i = 1; i <= m; i++) {
if (ans[compress[i]] != -1) {
cout << "K";
cout << ans[compress[i]] << endl;
}
else {
cout << "?" << endl;
}
}
}
Compilation message
kovanice.cpp: In function 'int main()':
kovanice.cpp:69:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for (int a = 0; a < s.size(); a++) {
| ~~^~~~~~~~~~
kovanice.cpp:99:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
99 | for (int i = 0; i < rest.size(); i++) {
| ~~^~~~~~~~~~~~~
kovanice.cpp:87:3: warning: 'mark' may be used uninitialized in this function [-Wmaybe-uninitialized]
87 | if (mark == '<') {
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
14464 KB |
Output is correct |
2 |
Correct |
13 ms |
14464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
501 ms |
21492 KB |
Output is correct |
2 |
Correct |
511 ms |
21524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
27 ms |
15904 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
851 ms |
29436 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |