답안 #206509

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
206509 2020-03-03T16:09:27 Z StanCatalin KOVANICE (COI15_kovanice) C++14
10 / 100
506 ms 34660 KB
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

ifstream in("omg.in");

const int dim = 3e5;

int n,m,v,parent[dim],rg[dim],a[dim],componenta[dim],d[dim],r[dim];
vector < pair<int,int> > muchii;
vector <int> vec[dim];
vector <int> revec[dim];

int Find(int p)
{
    if (p == parent[p])
    {
        return p;
    }
    parent[p] = Find(parent[p]);
    return parent[p];
}

void Union(int x,int y)
{
    int rx = Find(x);
    int ry = Find(y);
    if (rg[rx] > rg[ry])
    {
        parent[ry] = rx;
        componenta[y] = componenta[rx];
    }
    else if (rg[rx] < rg[ry])
    {
        parent[rx] = ry;
        componenta[x] = componenta[ry];
    }
    else
    {
        parent[ry] = rx;
        componenta[y] = componenta[rx];
        rg[rx]++;
    }
}

int DFS1(int nod)
{
    if (d[nod] != 0) return d[nod];
    for (auto v:vec[nod])
    {
        d[nod] = max(d[nod], DFS1(v));
    }
    return ++d[nod];
}

int DFS2(int nod)
{
    if (r[nod] != 0) return r[nod];
    for (auto v:revec[nod])
    {
        r[nod] = max(r[nod], DFS2(v));
    }
    return ++r[nod];
}


int main()
{
    cin >> n >> m >> v;
    for (int i=1; i<=m; i++) parent[i] = i, componenta[i] = i;
    for (int i=1; i<=v; i++)
    {
        char comp;
        int a,b;
        cin >> a >> comp >> b;
        if (comp == '=')
        {
            Union(a,b);
        }
        else
        {
            muchii.push_back({a,b});
        }
    }
    for (auto e:muchii)
    {
        vec[componenta[e.first]].push_back(componenta[e.second]);
        revec[componenta[e.second]].push_back(componenta[e.first]);
    }
    for (int i=1; i<=m; i++)
        DFS1(componenta[i]);
    for (int i=1; i<=m; i++)
        DFS2(componenta[i]);
    ///cout << d[1] << " " << d[2] << "\n";
    for (int i=1; i<=m; i++)
    {
        if (d[componenta[i]] + r[componenta[i]] == n+1)
        {
            cout << "K" << r[componenta[i]] << "\n";
        }
        else cout << "?\n";
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 16 ms 14584 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 168 ms 23232 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 85 ms 17000 KB Output is correct
2 Correct 85 ms 17128 KB Output is correct
3 Correct 84 ms 17236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 506 ms 34660 KB Output isn't correct
2 Halted 0 ms 0 KB -