#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
/* DEFINES */
#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define ld long double
#define npos ULLONG_MAX
#define INF LLONG_MAX
#define vv(a) vector<a>
#define pp(a, b) pair<a, b>
#define pq(a) priority_queue<a>
#define qq(a) queue<a>
#define ss(a) set<a>
#define mm(a, b) map<a, b>
#define ump(a, b) unordered_map<a, b>
#define sync \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define elif else if
#define endl "\n"
#define allc(a) begin(a), end(a)
#define all(a) a, a + sizeof(a) / sizeof(a[0])
#define pb push_back
#define logi(a) __lg(a)
#define sqrt(a) sqrtl(a)
#define mpr make_pair
#define ins insert
using namespace std;
using namespace __gnu_pbds;
using namespace __cxx11;
typedef char chr;
typedef basic_string<chr> str;
template <typename T, typename key = less<T>>
using ordered_set = tree<T, null_type, key, rb_tree_tag, tree_order_statistics_node_update>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll N = 3e5 + 5;
vv(ll) g[N], gr[N];
struct Dsu
{
ll n;
vv(ll) dsu;
ll cur;
Dsu(ll sz = 1)
{
n = sz;
cur = n;
dsu.resize(n + 1, 0);
}
void init()
{
for (ll i = 1; i <= n; i++)
dsu[i] = i;
}
bool Union(ll x, ll y)
{
x = repr(x), y = repr(y);
if (x == y)
return false;
dsu[x] = y;
cur--;
return true;
}
ll repr(ll x)
{
if (dsu[x] == x)
return x;
return dsu[x] = repr(dsu[x]);
}
ll query()
{
return cur;
}
};
ll calc[N], K[N];
void dfs(ll v)
{
calc[v] = 1;
for (ll to : g[v])
{
if (!calc[to])
dfs(to);
calc[v] = max(calc[v], calc[to] + 1);
}
}
void dfs1(ll v)
{
for (ll to : g[v])
if (calc[to] + 1 == calc[v] and !K[to])
K[to] = K[v] + 1, dfs1(to);
}
void solve()
{
ll n, m, v;
cin >> n >> m >> v;
tuple<ll, chr, ll> f[v];
Dsu dsu(m);
dsu.init();
for (ll i = 0; i < v; i++)
{
ll a, b;
chr c;
cin >> a >> c >> b;
f[i] = {a, c, b};
if (c == '=')
dsu.Union(a, b);
}
for (ll i = 0; i < v; i++)
{
ll a, b;
chr c;
tie(a, c, b) = f[i];
a = dsu.repr(a);
b = dsu.repr(b);
if (c != '=')
{
if (c == '>')
swap(a, b);
g[a].pb(b);
gr[b].pb(a);
}
}
for (ll i = 1; i <= m; i++)
{
// cout << dsu.repr(i) << endl;
if (!gr[dsu.repr(i)].size() and !calc[dsu.repr(i)])
dfs(dsu.repr(i));
}
for (ll i = 1; i <= m; i++)
if (!gr[dsu.repr(i)].size() and calc[dsu.repr(i)] == n)
K[dsu.repr(i)] = 1, dfs1(dsu.repr(i));
for (ll i = 1; i <= m; i++)
if (K[dsu.repr(i)] == 0)
cout << "?\n";
else
cout << "K" << K[dsu.repr(i)] << endl;
}
/*
*/
int main()
{
sync
// precomp();
ll t = 1;
// cin >> t;
for (ll i = 1; i <= t; i++)
// cout << "Case " << i << ": ",
solve();
cerr << "\nTime elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
14576 KB |
Output is correct |
2 |
Correct |
14 ms |
14508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
24608 KB |
Output is correct |
2 |
Correct |
78 ms |
24884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
19280 KB |
Output is correct |
2 |
Correct |
31 ms |
20048 KB |
Output is correct |
3 |
Correct |
29 ms |
19780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
187 ms |
37960 KB |
Output is correct |
2 |
Correct |
195 ms |
41588 KB |
Output is correct |
3 |
Correct |
207 ms |
41464 KB |
Output is correct |
4 |
Correct |
247 ms |
45384 KB |
Output is correct |
5 |
Correct |
336 ms |
49412 KB |
Output is correct |