답안 #168491

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
168491 2019-12-13T11:01:50 Z egekabas KOVANICE (COI15_kovanice) C++14
40 / 100
192 ms 14584 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];
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(g[y].size() > g[x].size())
        swap(x, y);
    for(auto u : g[y]){
        g[x].pb(u);
    }
    g[y].clear();
    prt[y] = x;
}
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;
    
    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(int j = 0; j < g[i].size(); ++j)
            g[i][j] = find(g[i][j]);
        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";
    }
}

Compilation message

kovanice.cpp: In function 'int main()':
kovanice.cpp:77:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int j = 0; j < g[i].size(); ++j)
                        ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 8 ms 7416 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 72 ms 11640 KB Output is correct
2 Correct 80 ms 11804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 28 ms 7544 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 192 ms 14584 KB Output isn't correct
2 Halted 0 ms 0 KB -