이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define in insert
#define fi first
#define se second
#define vl vector<ll>
#define all(v) v.begin(), v.end()
#define endl "\n"
using namespace std;
const int sz = 3e5 + 5; /// mind this
const int MAX = 2e6 + 123;
const int BS = 61;
const int mod = 998244353;
struct DSU
{
vector<int> e;
void init(int n)
{
e.assign(n + 1, -1);
}
int get(int x)
{
if (e[x] < 0) return x;
return e[x] = get(e[x]);
}
bool unite(int x, int y)
{
x = get(x);
y = get(y);
if (x == y) return false;
if (e[x] > e[y]) swap(x, y);
e[x] += e[y];
e[y] = x;
return true;
}
};
vl e[sz];
ll dp[sz], ans[sz], n;
void dfs(ll node){
dp[node] = 1;
for(auto to: e[node]){
if(!dp[to]){
dfs(to);
}
dp[node] = max(dp[node], dp[to] + 1);
}
}
void dfs2(ll node){
ans[node] = n - dp[node] + 1;
for(auto to: e[node]){
if(dp[to] == dp[node] - 1){
dfs2(to);
}
}
}
void solve(){
DSU dsu;
ll m, v, i, j;
cin >> n >> m >> v;
vector<char> comp(v);
vl x(v), y(v);
dsu.init(m);
for(i = 0; i < v; i++){
cin >> x[i] >> comp[i] >> y[i];
if(comp[i] == '='){
dsu.unite(x[i], y[i]);
}
}
for(i = 0; i < v; i++){
if(comp[i] == '<'){
e[dsu.get(x[i])].pb(dsu.get(y[i]));
}
}
for(i = 1; i <= m; i++){
if(!dp[i]){
dfs(i);
}
}
for(i = 1; i <= m; i++){
if(dp[i] == n && dsu.e[i] < 0){
dfs2(i);
}
}
for(i = 1; i <= m; i++){
if(!ans[dsu.get(i)]){
cout << '?';
}
else{
cout << 'K' << ans[dsu.get(i)];
}
cout << endl;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t = 1;
// cin >> t;
while(t--){
solve();
}
}
/*
3 5 3
1<2
2<4
3=5
*/
컴파일 시 표준 에러 (stderr) 메시지
kovanice.cpp: In function 'void solve()':
kovanice.cpp:61:17: warning: unused variable 'j' [-Wunused-variable]
61 | ll m, v, i, j;
| ^
# | 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... |