#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);
}
}
}
int d[MAXN], r[MAXN];
vector<int> gd[MAXN], gr[MAXN];
int dfsx(int v) {
if (d[v]) return d[v];
for (int i : gd[v])
d[v] = max(d[v], dfsx(i));
return ++d[v];
}
int dfsy(int v) {
if (r[v]) return r[v];
for (int i : gr[v])
r[v] = max(r[v], dfsy(i));
return ++r[v];
}
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++) {
int node1, node2; char mark;
cin >> node1 >> mark >> node2;
if (mark == '=') {
eq[node1].push_back(node2);
eq[node2].push_back(node1);
continue;
}
rest.push_back({ node1, node2 });
}
for (int i = 1; i <= m; i++) {
if (compress[i] == -1) {
fill(i, i);
}
}
for (int i = 0; i < rest.size(); i++) {
gd[compress[rest[i].first]].push_back(compress[rest[i].second]);
gr[compress[rest[i].second]].push_back(compress[rest[i].first]);
counts[compress[rest[i].second]]++;
}
for (int i = 1; i <= m; i++) {
dfsx(compress[i]);
dfsy(compress[i]);
}
for (int i = 1; i <= m; i++) {
if (d[compress[i]] + r[compress[i]] == n + 1)cout << "K" << r[compress[i]] << endl;
else cout << "?" << endl;
}
/*
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:91: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]
91 | for (int i = 0; i < rest.size(); i++) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
28672 KB |
Output is correct |
2 |
Correct |
23 ms |
28672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
631 ms |
39540 KB |
Output is correct |
2 |
Correct |
552 ms |
39536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
30488 KB |
Output is correct |
2 |
Correct |
66 ms |
30616 KB |
Output is correct |
3 |
Correct |
47 ms |
30616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1008 ms |
50852 KB |
Output is correct |
2 |
Correct |
1024 ms |
52836 KB |
Output is correct |
3 |
Correct |
1020 ms |
52692 KB |
Output is correct |
4 |
Correct |
1062 ms |
53508 KB |
Output is correct |
5 |
Correct |
1071 ms |
54204 KB |
Output is correct |