#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
//#define int long long
const int MAXN = 3e5 + 5;
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
#define OPEN freopen(".in", "r", stdin); \
freopen(".out", "w", stdout);
#else
#define OPEN void(23);
#endif
struct DSU
{
vector <int> par;
DSU(int n)
{
par.resize(n);
iota(par.begin(), par.end(), 0ll);
}
inline int get(int a)
{
return par[a] = (par[a] == a ? a : get(par[a]));
}
bool same(int u, int v)
{
return get(u) == get(v);
}
bool unite(int u, int v)
{
if(same(u, v)) return false;
u = get(u), v = get(v);
par[v] = u;
return true;
}
};
vector <int> adj[MAXN];
vector <int> compos[MAXN];
void solve2(int n)
{
int m, v; cin >> m >> v;
vector <pair <int, int>> que;
DSU dsu(m +1);
for(int i = 1; i <= v; i++)
{
int u = 0, v = 0; char eq;
cin >> u >> eq >> v;
if(eq == '>') swap(u, v);
if(eq == '=') dsu.unite(u, v);
else que.emplace_back(u, v);
}
for(auto &[a, b] : que)
{
adj[dsu.get(a)].emplace_back(dsu.get(b));
}
vector <int> dists(m +1, -1);
function <int(int)> dfsDist = [&](int node) -> int
{
if(dists[node] != -1) return dists[node];
dists[node] = 1;
for(const int &child : adj[node])
{
dists[node] = max(dfsDist(child) +1, dists[node]);
}
return dists[node];
};
for(int i = 1; i <= m; i++) dfsDist(i);
vector <int> colors(m +1, -1);
function <void(int, int)> dfsCol = [&](int node, int col) -> void
{
colors[node] = col;
sort(adj[node].begin(), adj[node].end(), [&](int a, int b)
{
return dists[a] >= dists[b];
});
for(const int &child : adj[node])
{
if(colors[child] == -1) dfsCol(child, col +1);
}
};
for(int i = 1; i <= m; i++)
{
if(dists[i] == n) dfsCol(i, 1);
}
for(int i = 1; i <= m; i++) compos[dsu.get(i)].emplace_back(i);
vector <int> ans(m +1, -1);
for(int i = 1; i <= m; i++) ans[i] = colors[i];
for(int i = 1; i <= m; i++)
{
if(dsu.get(i) != i) continue;
int col = colors[i];
for(int &j : compos[i]) ans[j] = col;
}
for(int i = 1; i <= m; i++)
{
if(ans[i] == -1) cout << '?';
else cout << 'K' << ans[i];
cout << "\n";
}
return;
}
void solve1(int n)
{
int m, v; cin >> m >> v;
vector <int> colors(m +1, 0);
DSU dsu(m +1);
for(int i = 1; i <= v; i++)
{
string str; cin >> str;
int u = 0, v = 0; char eq;
bool ok = false;
for(char &ch : str)
{
if(ch == '<' || ch == '=' || ch == '>') eq = ch, ok = true;
else
{
if(!ok) u = u * 10 + (ch - '0');
else v = v * 10 + (ch - '0');
}
}
if(eq == '>') swap(u, v);
if(eq == '=') dsu.unite(u, v);
else colors[u] = 1, colors[v] = 2;
}
for(int i = 1; i <= m; i++) compos[dsu.get(i)].emplace_back(i);
vector <int> ans(m +1);
for(int i = 1; i <= m; i++)
{
if(compos[i].size() == 0) continue;
int color = -1;
for(int &j : compos[i])
{
if(colors[j] != 0) color = colors[j];
}
for(int &j : compos[i]) ans[j] = color;
}
for(int i = 1; i <= m; i++)
{
if(ans[i] == -1) cout << '?';
else cout << 'K' << ans[i];
cout << "\n";
}
return;
return;
}
int32_t main()
{
OPEN;
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t = 1; //cin >> t;
while(t--)
{
int n; cin >> n;
if(n == 2) solve1(n);
else solve2(n);
}
}
Compilation message
kovanice.cpp: In function 'void solve1(int)':
kovanice.cpp:147:9: warning: 'eq' may be used uninitialized in this function [-Wmaybe-uninitialized]
147 | if(eq == '=') dsu.unite(u, v);
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
14424 KB |
Output is correct |
2 |
Correct |
5 ms |
14424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
22356 KB |
Output is correct |
2 |
Correct |
38 ms |
22360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
36 ms |
31692 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
163 ms |
52872 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |