Submission #1296967

#TimeUsernameProblemLanguageResultExecution timeMemory
1296967okahak71KOVANICE (COI15_kovanice)C++20
100 / 100
401 ms67272 KiB
#include <bits/stdc++.h> #define ll long long #define vec vector #define all(X) X.begin(), X.end() #define pb push_back #define endl '\n' using namespace std; const ll inf = 1e17; vec<vec<ll>>g; vec<array<ll, 2>>ans; struct DSU{ vector<ll>e; DSU(ll n){ e.assign(n + 1, -1); } ll find(ll x){ if(e[x] < 0) return x; return e[x] = find(e[x]); } bool unite(ll a, ll b){ a = find(a), b = find(b); if(a == b) return 0; if(e[a] > e[b]) swap(a, b); e[a] += e[b]; e[b] = a; return 1; } }; void _(ll n, ll m, ll q){ DSU dsu(m); vector<pair<char, pair<ll, ll>>>vv(q); for(ll i = 0; i < q; i++){ ll a, b; char c; cin >> a >> c >> b; vv[i] = {c, {a, b}}; if(c != '=') continue; dsu.unite(a, b); } ll id = 0; unordered_map<ll, ll>mp; for(ll i = 1; i <= m; i++){ ll k = dsu.find(i); if(!mp.count(k)) mp[k] = id++; } for(ll i = 1; i <= m; i++) mp[i] = mp[dsu.find(i)]; ans.assign(id, {1, n}); g.assign(id, {}); vec<ll>cnt(id, 0); set<array<ll, 2>>st; for(ll i = 0; i < q; i++){ if(vv[i].first == '=') continue; ll a = vv[i].second.first; a = dsu.find(a); ll b = vv[i].second.second; b = dsu.find(b); if(a == b){cout << -1 ; return ;} //impossible if(vv[i].first == '>') swap(a, b); a = mp[a], b = mp[b]; if(st.insert({a, b}).second) g[a].pb(b), cnt[b]++; } queue<ll>dq; vec<ll>ord; for(ll i = 0; i < id; i++) if(!cnt[i]) dq.push(i); while(!dq.empty()){ ll u = dq.front(); dq.pop(); ord.pb(u); for(ll v : g[u]){ if(!(--cnt[v])) dq.push(v); } } if(ord.size() != id){ cout << -2 << endl; return ; } for(ll &u : ord){ for(ll &v : g[u]) ans[v][0] = max(ans[v][0], ans[u][0] + 1); } for(ll i = id - 1; i >= 0; i--){ for(ll &v : g[ord[i]]) ans[ord[i]][1] = min(ans[ord[i]][1], ans[v][1] - 1); } for(ll i = 1; i <= m; i++){ ll k = mp[i]; //cout << ans[k][0] << ' ' << ans[k][1] << endl; if(ans[k][0] == ans[k][1]) cout << "K" << ans[k][0]; else cout << "?"; cout << endl; } } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n, m, v; cin >> n >> m >> v; _(n, m, v); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...