Submission #846220

#TimeUsernameProblemLanguageResultExecution timeMemory
846220vjudge1KOVANICE (COI15_kovanice)C++98
50 / 100
2060 ms16452 KiB
#include <iostream>
#include <vector>
using namespace std;


int n;
int* valueofparent;
int* parentindsu;
bool* visited;
vector<vector<int>> children;


int getpindsu(int n){
    if (parentindsu[n - 1] == n) return n;
    parentindsu[n - 1] = getpindsu(parentindsu[n - 1]);
    return parentindsu[n - 1];
}


void dsumerge(int a, int b){
    int pa = getpindsu(a);
    int pb = getpindsu(b);
    if (pa == pb) return;
    parentindsu[a - 1] = pb;
    parentindsu[pa - 1] = pb;
}



bool fill(int depth, int node){
    if (depth == n){
        valueofparent[node - 1] = n;
        return true;
    }
    if (visited[node - 1]){
        if (valueofparent[node - 1] != -1) return true;
    }
    visited[node - 1] = true;
    bool isdetermined = false;
    for (int i = 0; i < children[node - 1].size(); i++){
        if (fill(depth + 1, children[node - 1][i])) isdetermined = true;
    }
    if (isdetermined) valueofparent[node - 1] = depth;
    return isdetermined;
}



int main(){
    int m, v, a, b;
    char c;
    string weigh;
    cin >> n >> m >> v;
    int equalcount = 0;
    int unequalcount = 0;
    int* equal1 = new int[v];
    int* equal2 = new int[v];
    int* less = new int[v];
    int* more = new int[v];
    for (int i = 0; i < v; i++){
        cin >> a >> c >> b;
        if (c == '='){
            equal1[equalcount] = a;
            equal2[equalcount] = b;
            equalcount++;
        } else {
            less[unequalcount] = a;
            more[unequalcount] = b;
            unequalcount++;
        }
    }
    parentindsu = new int[m];
    for (int i = 0; i < m; i++) parentindsu[i] = i + 1;
    for (int i = 0; i < equalcount; i++){
        dsumerge(equal1[i], equal2[i]);
    }
    bool *isattop = new bool[m];
    for (int i = 1; i <= m; i++){
        isattop[i - 1] = (getpindsu(i) == i); 
    }
    children.resize(m);
    for (int i = 0; i < unequalcount; i++){
        isattop[getpindsu(more[i]) - 1] = false;
        children[getpindsu(less[i]) - 1].push_back(getpindsu(more[i]));
    }
    valueofparent = new int[m];
    visited = new bool[m];
    for (int i = 0; i < m; i++){
        valueofparent[i] = -1;
        visited[i] = false;
    }
    for (int i = 1; i <= m; i++){
        if (isattop[i - 1]) fill(1, i);
    }
    for (int i = 1; i <= m; i++){
        if (valueofparent[getpindsu(i) - 1] != -1){
            cout << "K" << valueofparent[getpindsu(i) - 1] << endl;
        } else cout << "?\n";
    }
}

Compilation message (stderr)

kovanice.cpp: In function 'bool fill(int, int)':
kovanice.cpp:40:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |     for (int i = 0; i < children[node - 1].size(); i++){
      |                     ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...