#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, inv;
int par[MAXN], sz[MAXN], indeg[MAXN];
int dist[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];
}
vector<int> type;
vector<pair<int,int>> edge;
int main() {
fast
int N, M, V;
cin >> N >> M >> V;
graph = vector<vector<int>>(M+1, vector<int>());
inv = 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++) {
string s;
cin >> s;
int l = 0, r = 0;
char ch;
for (int i = s.length() - 1, now = 1; i >= 0; i--, now *= 10) {
if (s[i] == '=' || s[i] == '<') {
ch = s[i];
for (int j = i - 1, now2 = 1; j >= 0; j--, now2 *= 10)
l += now2 * (s[j] - '0');
break;
}
r += now * (s[i] - '0');
}
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);
}
sort(edge.begin(), edge.end());
edge.erase(unique(edge.begin(), edge.end()), edge.end());
for (int i = 0; i < edge.size(); i++) {
graph[edge[i].f].push_back(edge[i].s);
inv[edge[i].s].push_back(edge[i].f);
indeg[edge[i].s]++;
}
queue<int> q;
for (int i = 1; i <= M; i++) {
if (i == find(i) && indeg[i] == 0)
q.push(i);
}
while (!q.empty()) {
int node = q.front();
q.pop();
for (auto u : graph[node]) {
if (dist[u] >= dist[node] + 1)
continue;
dist[u] = dist[node] + 1;
q.push(u);
}
}
queue<pair<int,int>> q2;
for (int i = 1; i <= M; i++) {
if (i == find(i) && dist[i] + 1 == N)
q2.push({i, N});
}
while (!q2.empty()) {
int node = q2.front().f;
int t = q2.front().s;
q2.pop();
if (type[node] != -1 || dist[node] + 1 != t || t == 0)
continue;
type[node] = t;
for (auto u : inv[node])
q2.push({u, t-1});
}
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:67: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]
67 | for (int i = 0; i < edge.size(); i++) {
| ~~^~~~~~~~~~~~~
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++) {
| ~~^~~~~~~~~~~~~
kovanice.cpp:61:3: warning: 'ch' may be used uninitialized in this function [-Wmaybe-uninitialized]
61 | if (ch == '=')
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4440 KB |
Output is correct |
2 |
Correct |
2 ms |
4440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
250 ms |
18752 KB |
Output is correct |
2 |
Correct |
262 ms |
19056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
5464 KB |
Output is correct |
2 |
Correct |
17 ms |
5580 KB |
Output is correct |
3 |
Correct |
21 ms |
5720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2050 ms |
29384 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |