This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("-O3")
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define vl vector<ll>
#define vi vector<int>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb push_back
#define sz(v) (ll)(v.size())
#define f first
#define s second
#define pii pair<int, int>
#define pll pair<ll, ll>
using namespace std;
const ll sz = 1e6+5;
vl g[sz];
ll par[sz], usize[sz], indeg[sz], lv[sz];
void makeset(ll v)
{
usize[v] = 1;
par[v] = v;
}
ll findpar(ll v)
{
if(par[v] == v)
return v;
return par[v] = findpar(par[v]);
}
void unionsets(ll a, ll b)
{
a = findpar(a);
b = findpar(b);
if(a == b)
return;
if(usize[a] < usize[b])
swap(a, b);
par[b] = a;
usize[a] += b;
}
set<ll>st;
void topological_sort(ll m)
{
queue<ll>q;
for(ll i = 1; i <= m; i++)
{
if(!indeg[i] && st.count(i))
{
lv[i] = 1;
q.push(i);
}
}
while(!q.empty())
{
ll x = q.front();
q.pop();
for(auto u : g[x])
{
indeg[u]--;
if(!indeg[u])
{
lv[u] = lv[x] + 1;
q.push(u);
}
}
}
}
void solve()
{
ll n, m, v, i, j;
cin >> n >> m >> v;
for(i = 1; i <= m; i++)
makeset(i);
for(i = 1; i <= v; i++)
{
string s;
cin >> s;
ll x = 0, y = 0;
bool ok = false;
char ch;
for(j = 0; j < sz(s); j++)
{
if(s[j] == '>' || s[j] == '=' || s[j] == '<')
{
ok = true;
ch = s[j];
}
else if(!ok)
x = x * 10 + (s[j] - '0');
else
y = y * 10 + (s[j] - '0');
}
if(ch == '=')
unionsets(x, y);
else if(ch == '<'){
g[x].pb(y);
st.insert(x);
indeg[y]++;
}
else{
g[y].pb(x);
st.insert(y);
indeg[x]++;
}
}
topological_sort(m);
map<ll, ll>mp;
for(i = 1; i <= m; i++)
{
if(lv[i])
{
ll g = i;
g = findpar(g);
mp[g] = lv[i];
}
}
for(i = 1; i <= m; i++)
{
if(!lv[i])
{
ll f = i;
f = findpar(f);
if(mp.find(f) != mp.end())
lv[i] = mp[f];
}
}
for(i = 1; i <= m; i++)
{
if(!lv[i])
cout << "?\n";
else
cout << "K" << lv[i] << "\n";
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll tests = 1;
//cin >> tests;
while(tests--)
{
solve();
}
}
/*
5 5
1 2 3 4 5
2 3 5 4
3 3 5
1 2 5
2 1 3 3
1 1 3
*/
Compilation message (stderr)
kovanice.cpp: In function 'void solve()':
kovanice.cpp:95:17: warning: 'ch' may be used uninitialized in this function [-Wmaybe-uninitialized]
95 | else if(ch == '<'){
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |