#include <bits/stdc++.h>
using namespace std;
#define lesgooo ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl '\n'
#define int long long
const int N = 3e5+5;
vector<int> adj[N], comp[N];
int idx[N], vis[N], cur, indeg[N], val[N], ans[N], dp[N];
void dfs(int i)
{
comp[cur].push_back(i), idx[i] = cur, vis[i] = 1;
for (auto nxt : adj[i]) if (!vis[nxt]) dfs(nxt);
}
int depth(int i)
{
if (~dp[i]) return dp[i];
dp[i] = 1;
for (auto nxt : adj[i]) dp[i] = max(dp[i], 1+depth(nxt));
return dp[i];
}
void assign(int i, int dep)
{
val[i] = dep;
for (auto nxt : adj[i]) assign(nxt, dep+1);
}
signed main()
{
lesgooo;
int n, m, v;
cin >> n >> m >> v;
pair<pair<int, char>, int> a[v];
for (int i = 0; i < v; i++)
{
cin >> a[i].first.first >> a[i].first.second >> a[i].second;
a[i].first.first--, a[i].second--;
if (a[i].first.second == '=') adj[a[i].first.first].push_back(a[i].second), adj[a[i].second].push_back(a[i].first.first);
}
for (int i = 0; i < m; i++) if (!vis[i]) dfs(i), cur++;
for (int i = 0; i < m; i++) adj[i].clear(), val[i] = -1;
for (int i = 0; i < v; i++) if (a[i].first.second != '=') adj[idx[a[i].first.first]].push_back(idx[a[i].second]), indeg[idx[a[i].second]] = 1;
// for (int i = 0; i < cur; i++)
// {
// cout << i << ": ";
// for (auto nxt : adj[i]) cout << nxt << " ";
// cout << endl;
// }
memset(dp, -1, sizeof dp);
for (int i = 0; i < cur; i++) if (!indeg[i] && depth(i) == n) assign(i, 1);
for (int i = 0; i < m; i++) ans[i] = val[idx[i]];
for (int i = 0; i < m; i++) cout << (ans[i] == -1? "?" : "K"+to_string(ans[i])) << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16940 KB |
Output is correct |
2 |
Correct |
9 ms |
16852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
35304 KB |
Output is correct |
2 |
Correct |
85 ms |
35368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2081 ms |
20436 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2089 ms |
46580 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |