#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);
#define s second
#define f first
typedef long long ll;
const ll MOD = 998244353;
const ll LOGN = 20;
const ll MAXN = 3e5 + 5;
vector<vector<int>> graph;
int N, M, V;
int par[MAXN], sz[MAXN], indeg[MAXN];
int dist[MAXN], lng[MAXN];
int find(int node) {
if (node == par[node])
return node;
return par[node] = find(par[node]);
}
void unite(int a, int b) {
a = find(a);
b = find(b);
if (a == b)
return ;
if (sz[b] > sz[a])
swap(a, b);
par[b] = a;
sz[a] += sz[b];
}
void dfs(int node) {
if (graph[node].size() == 0)
lng[node] = 0;
for (auto u : graph[node]) {
if (lng[u] == -1)
dfs(u);
lng[node] = max(lng[node], lng[u] + 1);
}
}
vector<int> type;
void dfs2(int node, int need) {
if (lng[node] != need - 1)
return ;
type[node] = N-need+1;
for (auto u : graph[node])
dfs2(u, need-1);
}
vector<pair<int,int>> edge;
int main() {
fast
memset(lng, -1, sizeof(lng));
cin >> N >> M >> V;
graph = vector<vector<int>>(M+1, vector<int>());
type = vector<int>(M+1, -1);
for (int i = 1; i <= M; i++)
par[i] = i, sz[i] = 1;
for (int i = 0; i < V; i++) {
int l, r;
char ch;
cin >> l >> ch >> r;
if (ch == '=')
unite(l, r);
else
edge.push_back({l, r});
}
for (int i = 0; i < edge.size(); i++) {
edge[i].f = find(edge[i].f);
edge[i].s = find(edge[i].s);
graph[edge[i].f].push_back(edge[i].s);
indeg[edge[i].s]++;
}
for (int i = 1; i <= M; i++) {
if (i == find(i) && indeg[i] == 0)
dfs(i);
}
for (int i = 1; i <= M; i++) {
if (i == find(i) && lng[i] == N-1)
dfs2(i, N);
}
for (int i = 1; i <= M; i++) {
if (type[find(i)] == -1)
cout << "?" << endl;
else
cout << "K" << type[find(i)] << endl;
}
}
Compilation message
kovanice.cpp: In function 'int main()':
kovanice.cpp:74: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]
74 | for (int i = 0; i < edge.size(); i++) {
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4696 KB |
Output is correct |
2 |
Correct |
2 ms |
4700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
278 ms |
13264 KB |
Output is correct |
2 |
Correct |
265 ms |
13472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2050 ms |
5844 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2027 ms |
19912 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |