Submission #168490

# Submission time Handle Problem Language Result Execution time Memory
168490 2019-12-13T10:54:02 Z egekabas KOVANICE (COI15_kovanice) C++14
40 / 100
199 ms 20088 KB
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long   ll;
typedef unsigned long long   ull;
typedef long double ld;
typedef pair<ll, ll>    pll;
typedef pair<ull, ull>    pull;
typedef pair<int, int>  pii;
typedef pair<ld, ld>  pld;
int n, m, v;
int ans[300009];
int depth[300009];
int prt[300009];
int sz[300009];
vector<int> g[300009];
int find(int x){
    if(prt[x] == x)
        return x;
    return prt[x] = find(prt[x]);
}
void merge(int x, int y){
    x = find(x);
    y = find(y);
    if(sz[y] > sz[x])
        swap(x, y);
    
    for(auto u : g[y]){
        g[x].pb(u);
    }
    g[y].clear();
    prt[y] = x;
    sz[x] += sz[y];
}
int dfs1(int v){
    if(depth[v] != 0)
        return depth[v];
    for(auto u : g[v]){
        depth[v] = max(depth[v], dfs1(u));
    }
    depth[v]++;
    return depth[v];
}
void dfs2(int v, int d){
    ans[v] = d;
    for(auto u : g[v]){
        if(dfs1(u) == d-1)
            dfs2(u, d-1);
    }
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    
    cin >> n >> m >> v;
    for(int i = 1; i <= m; ++i){
        prt[i] = i;
        sz[i] = 1;
    }
    while(v--){
        int t1, t2;
        char c;
        cin >> t1 >> c >> t2;
        if(c == '=')
            merge(t1, t2);
        else{
            t1 = find(t1);
            t2 = find(t2);
            g[t2].pb(t1);
        }
    }
    for(int i = 1; i <= m; ++i){
        if(i != find(i))
            continue;
        for(auto &u : g[i])
            u = find(u);
        sort(g[i].begin(), g[i].end());
        g[i].resize(unique(g[i].begin(), g[i].end())-g[i].begin());
    }
    for(int i = 1; i <= m; ++i){
        if(i != find(i))
            continue;
        if(dfs1(i) == n)
            dfs2(i, n);
    }
    for(int i = 1; i <= m; ++i){
        int ret = ans[find(i)];
        if(ret)
            cout << 'K' << ret << "\n";
        else
            cout << "?\n";
    }
}
# Verdict Execution time Memory Grader output
1 Incorrect 9 ms 7416 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 76 ms 13688 KB Output is correct
2 Correct 76 ms 13816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 29 ms 8312 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 199 ms 20088 KB Output isn't correct
2 Halted 0 ms 0 KB -