Submission #1084602

#TimeUsernameProblemLanguageResultExecution timeMemory
1084602vjudge1KOVANICE (COI15_kovanice)C++17
100 / 100
112 ms32848 KiB
#include <bits/stdc++.h> using namespace std; int n, m, v; int parent[300005], sz[300005], rez[300005]; vector<int> G[300005]; int Find(int x) { if (x==parent[x]) return x; return parent[x]=Find(parent[x]); } void Union(int a, int b) { a=Find(a), b=Find(b); if (a==b) return; if (sz[a]<sz[b]) swap(a, b); sz[a]+=sz[b]; parent[b]=a; } int dolz[300005]; int dfs1(int teme) { if (dolz[teme]!=-1) return dolz[teme]; dolz[teme]=1; for (auto next:G[teme]) dolz[teme]=max(dolz[teme], dfs1(next)+1); return dolz[teme]; } void dfs2(int teme, int tezina) { if (rez[teme]) return; rez[teme]=tezina; for (auto next:G[teme]) { if (rez[next]==0&&dolz[teme]==dolz[next]+1) dfs2(next, tezina+1); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m >> v; int br1[v], br2[v]; char znak[v]; for (int i=1;i<=m;i++) parent[i]=i, sz[i]=1; for (int i=0;i<v;i++) { cin >> br1[i] >> znak[i] >> br2[i]; if (znak[i]=='=') Union(br1[i], br2[i]); } int inDegree[m+1]={0}; for (int i=0;i<v;i++) { if (znak[i]=='<') { G[Find(br1[i])].push_back(Find(br2[i])); inDegree[Find(br2[i])]++; } } vector<int> roots; memset(dolz, -1, sizeof(dolz)); for (int i=1;i<=m;i++) { if (inDegree[i]==0&&i==Find(i)) { int d=dfs1(i); if (d==n) dfs2(i,1); } } for (int i=1;i<=m;i++) rez[i]=rez[Find(i)]; for (int i=1;i<=m;i++) { if (rez[i]>0) cout << 'K' << rez[i] << '\n'; else cout << '?' << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...