#include <bits/stdc++.h>
#define pb push_back
using namespace std;
constexpr static int MXN = 3e5 + 5;
struct Node
{
int head, tail, size, nxt;
Node() {}
void init(int i) { head = i, tail = i, size = 1, nxt = -1; }
};
Node node[MXN];
vector<int> g[MXN];
vector<int> rg[MXN];
bitset<MXN> visited;
bitset<MXN> valid;
int res[MXN];
int depth[MXN];
void dfs1(int node)
{
visited[node] = true;
depth[node] = 0;
for (int c : g[node])
{
if (!visited[c])
dfs1(c);
depth[node] = max(depth[node], depth[c] + 1);
}
}
void dfs2(int node)
{
valid[node] = true;
for (int c : g[node])
{
if (valid[c])
continue;
depth[c] = depth[node] - 1;
dfs2(c);
}
for (int c : rg[node])
{
if (valid[c])
continue;
depth[c] = depth[node] + 1;
dfs2(c);
}
}
void merge(int a, int b);
int n, m;
int main()
{
int v;
cin >> n >> m >> v;
vector<array<int, 2>> edges;
for (int i = 0; i < m; i++)
node[i].init(i);
while (v--)
{
int a, b;
char c;
scanf("%i%c%i", &a, &c, &b);
a--,b--;
if (c == '=' && node[a].head != node[b].head)
merge(node[a].head, node[b].head);
else
edges.pb({b, a});
}
for (auto [a, b] : edges)
{
g[node[a].head].pb(node[b].head);
rg[node[b].head].pb(node[a].head);
}
for (int i = 0; i < m; i++)
if (i == node[i].head && !visited[i])
dfs1(i);
for (int i = 0; i < m; i++)
if (i == node[i].head && depth[i] == n-1)
dfs2(i);
for (int i = 0; i < m; i++)
if (i == node[i].head)
for (int j = i; j != -1; j = node[j].nxt)
res[j] = valid[i] ? depth[i] + 1 : -1;
for (int i = 0; i < m; i++)
{
if (res[i] != -1)
cout << "K" << res[i] << "\n";
else
cout << "?\n";
}
}
void merge(int a, int b)
{
if (node[b].size > node[a].size) swap(a, b);
node[a].size += node[b].size;
node[node[a].tail].nxt = b;
node[a].tail = node[b].tail;
for (;b != -1; b = node[b].nxt)
node[b].head = a;
}
Compilation message
kovanice.cpp: In function 'int main()':
kovanice.cpp:70:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
70 | scanf("%i%c%i", &a, &c, &b);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
16732 KB |
Output is correct |
2 |
Correct |
4 ms |
16732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
24516 KB |
Output is correct |
2 |
Correct |
52 ms |
24776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
20 ms |
18896 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
218 ms |
32804 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |