제출 #1352218

#제출 시각아이디문제언어결과실행 시간메모리
1352218the_commando_xKOVANICE (COI15_kovanice)C++17
100 / 100
84 ms28768 KiB
// #pragma GCC optimize("O3")
#include <bits/stdc++.h>

using namespace std;

using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;

using pii = pair<int, int>;
using vii = vector<pii>;
using vvii = vector<vii>;

using l = long long;
using vl = vector<l>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;

using pll = pair<l, l>;
using vll = vector<pll>;
using vvll = vector<vll>;

using d = double;
using vd = vector<d>;
using vvd = vector<vd>;
using vvvd = vector<vvd>;

using ld = long double;
using vld = vector<ld>;
using vvld = vector<vld>;
using vvvld = vector<vvld>;

using vb = vector<bool>;
using vvb = vector<vb>;
using pbb = pair<bool, bool>;
using vbb = vector<pbb>;

#define ff first
#define ss second

#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()

void setIO(string name = "")
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    if (!name.empty())
    {
        (void)freopen((name + ".in").c_str(), "r", stdin);
        (void)freopen((name + ".out").c_str(), "w", stdout);
    }
}

const ld pi = 3.14159265358979323846;

const l LINF = 1e18;
const l INF = 1e9;

const l MOD = 1e9 + 7;
// const l MOD = 998244353;

const l MAXN = 2e5 + 5;

struct DSU
{
    vi parent, rnk, sz;

    DSU(int n = MAXN)
    {
        parent.resize(n);
        rnk.assign(n, 0);
        sz.assign(n, 1);
        for (int i = 0; i < n; i++)
            parent[i] = i;
    }
    int find(int x)
    {
        // * find(x) → returns representative of set containing x (with path compression).
        if (parent[x] != x)
            parent[x] = find(parent[x]);
        return parent[x];
    }
    bool unite(int a, int b)
    {
        // * unite(a, b) → merges sets containing a and b. Returns false if already merged.
        a = find(a), b = find(b);
        if (a == b)
            return false;

        if (rnk[a] < rnk[b])
            swap(a, b);
        parent[b] = a;
        if (rnk[a] == rnk[b])
            ++rnk[a];
        sz[a] += sz[b];
        return true;
    }
    bool same(int a, int b)
    {
        // * same(a, b) → checks if two elements are in the same set.
        return find(a) == find(b);
    }
    int size(int x)
    {
        // * size(x) → gives the size of the connected component containing x.
        return sz[find(x)];
    }
};

vvi g;
vi dp, ans;

void dfs1(int u)
{
    if (dp[u])
        return;

    dp[u] = 1;

    for (auto v : g[u])
    {
        dfs1(v);
        dp[u] = max(dp[u], dp[v] + 1);
    }
}

void dfs2(int u, int dist)
{
    ans[u] = dist;
    for (auto v : g[u])
    {
        if (dp[v] == dp[u] - 1 && !ans[v])
            dfs2(v, dist + 1);
    }
}

void solve()
{
    int n, m, v;
    cin >> n >> m >> v;

    g = vvi(m + 1), dp = vi(m + 1, 0), ans = vi(m + 1, 0);

    DSU dsu(m + 1);

    vi a(v + 1), b(v + 1);
    vb c(v + 1); // * isEqual

    char _;
    for (int i = 1; i <= v && cin >> a[i] >> _ >> b[i]; ++i)
        if (c[i] = (_ == '='))
            dsu.unite(a[i], b[i]);

    for (int i = 1; i <= v; ++i)
    {
        if (!c[i])
        {
            int u = dsu.find(a[i]), v = dsu.find(b[i]);
            if (u != v)
                g[u].pb(v);
        }
    }

    for (int i = 1; i <= m; ++i)
    {
        int u = dsu.find(i);
        if (u == dsu.find(u) && !dp[u])
            dfs1(u);
    }

    for (int i = 1; i <= m; ++i)
    {
        int u = dsu.find(i);
        if (dp[u] == n && !ans[u])
            dfs2(u, 1);
    }

    for (int i = 1; i <= m; ++i)
    {
        int u = dsu.find(i);
        if (ans[u])
            cout << "K" << ans[u] << '\n';
        else
            cout << "?\n";
    }
}

int main()
{
    setIO("");

#ifndef ONLINE_JUDGE
    // setIO("filename");
#endif

    int t = 1;
    // cin >> t;
    while (t--)
        solve();

    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

kovanice.cpp: In function 'void setIO(std::string)':
kovanice.cpp:53:22: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   53 |         (void)freopen((name + ".in").c_str(), "r", stdin);
      |               ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
kovanice.cpp:54:22: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   54 |         (void)freopen((name + ".out").c_str(), "w", stdout);
      |               ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...