#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;
int res[MXN];
int ucount[MXN];
int dcount[MXN];
void dfs1(int node)
{
visited[node] = true;
dcount[node] = 0;
for (int c : g[node])
{
if (!visited[c])
dfs1(c);
dcount[node] = max(dcount[node], dcount[c] + 1);
}
}
void dfs2(int node)
{
visited[node] = true;
ucount[node] = 0;
for (int c : rg[node])
{
if (!visited[c])
dfs2(c);
ucount[node] = max(ucount[node], ucount[c] + 1);
}
}
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);
visited.reset();
for (int i = 0; i < m; i++)
if (i == node[i].head && !visited[i])
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] = (ucount[i] + dcount[i] == n-1) ? dcount[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:63:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | scanf("%i%c%i", &a, &c, &b);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
18776 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
24524 KB |
Output is correct |
2 |
Incorrect |
54 ms |
24776 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
18 ms |
20944 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
152 ms |
32640 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |