답안 #1121044

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1121044 2024-11-28T10:58:12 Z raul2008487 KOVANICE (COI15_kovanice) C++17
100 / 100
165 ms 34008 KB
#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((!ans[to]) && 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
*/

Compilation message

kovanice.cpp: In function 'void solve()':
kovanice.cpp:61:17: warning: unused variable 'j' [-Wunused-variable]
   61 |     ll m, v, i, j;
      |                 ^
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 7556 KB Output is correct
2 Correct 6 ms 7520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 14696 KB Output is correct
2 Correct 47 ms 16200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 10064 KB Output is correct
2 Correct 19 ms 11156 KB Output is correct
3 Correct 20 ms 11088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 135 ms 23880 KB Output is correct
2 Correct 131 ms 27464 KB Output is correct
3 Correct 135 ms 27224 KB Output is correct
4 Correct 111 ms 29000 KB Output is correct
5 Correct 165 ms 34008 KB Output is correct